go语言库是封装了windows的api吗
go语言库是封装了windows的api。根据查询相关公开信息显示,Go开发WindowsApp要做的第一件事情就是封装这些windowsapi。这个项目已经实现了对winapi的封装。
go语言如何调用底层api???
Google Go语言通过syscall,unsafe模块访问windows api,ShellExecute
package main
import "syscall"
import "unsafe"
func main() {
var hand uintptr = uintptr(0);
var operator uintptr = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("open")));
var fpath uintptr = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("D:\Program Files\TTPlayer\TTPlayer.exe")));
var param uintptr = uintptr(0);
var dirpath uintptr = uintptr(0);
var ncmd uintptr = uintptr(1);
shell32 := syscall.NewLazyDLL("shell32.dll");
ShellExecuteW := shell32.NewProc("ShellExecuteW");
_,_,_ = ShellExecuteW.Call(hand,operator,fpath,param,dirpath,ncmd);
golang 怎么调用windowsapi
window下调用API列出所有运行的进程
[Google Go]代码
package main
import (
"fmt"
"syscall"
"unsafe"
"strconv"
)
type ulong int32
type ulong_ptr uintptr
type PROCESSENTRY32 struct {
dwSize ulong
cntUsage ulong
th32ProcessID ulong
th32DefaultHeapID ulong_ptr
th32ModuleID ulong
cntThreads ulong
th32ParentProcessID ulong
pcPriClassBase ulong
dwFlags ulong
szExeFile [260]byte
}
func main() {
kernel32 := syscall.NewLazyDLL("kernel32.dll");
CreateToolhelp32Snapshot := kernel32.NewProc("CreateToolhelp32Snapshot");
pHandle,_,_ := CreateToolhelp32Snapshot.Call(uintptr(0x2),uintptr(0x0));
if int(pHandle)==-1 {
return;
}
Process32Next := kernel32.NewProc("Process32Next");
for {
var proc PROCESSENTRY32;
proc.dwSize = ulong(unsafe.Sizeof(proc));
if rt,_,_ := Process32Next.Call(uintptr(pHandle),uintptr(unsafe.Pointer(proc)));int(rt)==1 {
fmt.Println("ProcessName : "+string(proc.szExeFile[0:]));
fmt.Println("ProcessID : "+strconv.Itoa(int(proc.th32ProcessID)));
}else{
break;
}
}
CloseHandle := kernel32.NewProc("CloseHandle");
_,_,_ = CloseHandle.Call(pHandle);
}