【发布时间】:2020-02-03 07:21:24
【问题描述】:
我正在尝试了解 Golangs 系统调用包的一些低级细节。特别是,我对特定于 Windows 的系统调用感兴趣(参见下面的示例)。
我可以找到基于 UNIX 系统的syscall.Syscall() 的定义:
- https://golang.org/src/syscall/syscall_unix.go?s=496:562#L18
- https://golang.org/src/syscall/asm_unix_amd64.s
但是,对于基于 Windows 的系统,我找不到任何此类定义,例如 asm_windows_amd64.s。
特别是,asm_unix_amd64.s 具有以下构建指令,因此它的 ·Syscall(SB),NOSPLIT,$0-56 定义不能是 Windows 系统调用也调用的那个:
1// +build netbsd freebsd openbsd dragonfly
在哪里为基于 Windows 的系统定义了syscall.Syscall()?
示例: https://godoc.org/golang.org/x/sys/windows#example-LoadLibrary
h, err := windows.LoadLibrary("kernel32.dll")
if err != nil {
abort("LoadLibrary", err)
}
defer windows.FreeLibrary(h)
proc, err := windows.GetProcAddress(h, "GetVersion")
if err != nil {
abort("GetProcAddress", err)
}
r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
major := byte(r)
minor := uint8(r >> 8)
build := uint16(r >> 16)
print("windows version ", major, ".", minor, " (Build ", build, ")\n")
【问题讨论】:
-
因为 Windows renumbers 系统每隔一小时左右(好吧,也许是每个月左右)调用一次,没有人在他头脑正常的情况下实际上直接使用它们。调用总是通过一些 DLL,该 DLL 使用未发布的 Microsoft 机密将名称映射到系统调用号。 Go 似乎在做同样的事情,而且,在构建时生成代码以调用 DLL,使用 github.com/golang/sys/blob/master/windows/mkwinsyscall/…
-
@torek - 你能找到上面例子中调用的
syscall.Syscall()的定义吗:r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)?另外,这里我没有使用系统调用号,而是函数的 DLL 地址:windows.GetProcAddress(h, "GetVersion") -
... 我想。窗户是个谜;我建议不要自己使用它,也不要拥有它。啊哈:不,它在
runtime/syscall_windows.go。 -
Linkname pragma 导出具有给定名称的函数