確定當前進程是否在 WOW64 中運行或不在 Go 中運行 (Determining if current process runs in WOW64 or not in Go)


問題描述

確定當前進程是否在 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 LomanicmiltonbLomanicErfan Azhdari)

參考文件

  1. Determining if current process runs in WOW64 or not in Go (CC BY‑SA 2.5/3.0/4.0)

#32bit-64bit #DLL #GO #wow64 #Windows






相關問題

Visual Studio 將非託管 64 位 DLL 複製到 SysWOW64 而不是 System32 (Unmanaged 64-bit DLL copied to SysWOW64 and not System32 by Visual Studio)

將 R 包從 32 位安裝到 64 位 (install R package from 32bit to 64bit)

x32 ABI - гэта інструмент, як ім карыстацца (x32 ABI is this a tool ,how to use this)

在 64 位應用程序中使用 32 位 .lib 庫 (Using 32bit .lib library in a 64bit application)

當構建配置為“AnyCPU”或“x64”時,Castle Windsor 無法解決 (Castle Windsor not resolving when build configuration is "AnyCPU" or "x64")

32 位 SPARC V8 應用程序可以在 64 位 SPARC V9 上運行嗎? (Can 32-bit SPARC V8 application run on 64-bit SPARC V9?)

Eclipse 內存分析器:無法附加到 32 位進程 (Eclipse Memory Analyzer: Unable to attach to 32-bit process)

如何知道 libpython27.a 是 32 位還是 64 位? (How to know whether libpython27.a is 32-bit or 64-bit?)

確定當前進程是否在 WOW64 中運行或不在 Go 中運行 (Determining if current process runs in WOW64 or not in Go)

確保在 Windows Server 2003 64 位上安裝了 ASP.NET 3.5 Framework (Ensure that ASP.NET 3.5 Framework is installed on Windows Server 2003 64-bit)

為什麼我可以從在 IIS7 中以 64 位模式運行的 AnyCPU .NET Web 應用程序調用 32 位 COM 庫? (Why am I able to call a 32 bit COM library from an AnyCPU .NET web application running in IIS7 in 64 bit mode?)

編譯為 x86 時檢測 OS x86 或 x64 (Detect OS x86 or x64, when compiled as x86)







留言討論