問題描述
確定當前進程是否在 WOW64 中運行或不在 Go 中運行 (Determining if current process runs in WOW64 or not in Go)
我做錯了什麼?如何進行測試以確定我們的 32 位 Go 程序是在 64 位 CPU (WOW64) 上的模擬 32 位上運行,還是在真正的 32 位 Windows 上運行?</p>
參考解法
方法 1:
I believe the issue is the handle parameter on your proc.Call
. The expected parameter for IsWow64Process
is a HANDLE which is not the same as a pid
. Which is why it is indicating that the handle is invalid.
The following SO question How to get process handle from process id indicates that you need to call OpenProcess
passsing in the pid and it returns the handle.
EDIT: GetCurrentProcess is defined in syscall. So I think you can replace the Getpid
call with the following:
handle, err := syscall.GetCurrentProcess()
方法 2:
OK, so here is a working code:
package main
import (
"syscall"
"fmt"
"unsafe"
)
func main() {
dll, err := syscall.LoadDLL("kernel32.dll")
if err != nil {
fmt.Println("Can't load kernel32")
fmt.Println(err)
}
defer dll.Release()
proc, err := dll.FindProc("IsWow64Process")
if err != nil {
fmt.Println("Proc not found")
fmt.Println(err)
}
fmt.Printf("%v\n",proc)
handle, err := syscall.GetCurrentProcess()
if err != nil {
fmt.Println("Handle not found")
fmt.Println(err)
}
fmt.Printf("%v\n",handle)
var result bool
v, x, y := proc.Call(uintptr(handle), uintptr(unsafe.Pointer(&result)))
fmt.Printf("%v %v %v\n",v,x,y)
fmt.Printf("%v\n",result)
}
The result
var will be true for a WOW64 system and false for a 32 bit system.
方法 3:
You can also use golang.org/x/sys/windows
package main
import (
"fmt"
"golang.org/x/sys/windows"
)
func main() {
handle := windows.CurrentProcess()
var isWow64 bool
err := windows.IsWow64Process(handle, &isWow64)
if err != nil {
panic(err)
}
fmt.Println(isWow64)
}
(by Lomanic、miltonb、Lomanic、Erfan Azhdari)