如何使用Windows DLL方法

时间:2017-09-11 18:33:00

标签: windows go

我正在尝试使用kernel32.dll中存在的GetPhysicallyInstalledSystemMemory方法。

它需要PULONGLONG类型的单个参数,但我不知道如何将其映射到golang变量。这是我当前的尝试导致“错误:参数不正确”。

任何人都可以解释如何做到这一点吗?

package main

import (
    "fmt"
    "syscall"
)

var memory uintptr

func main() {
    kernel32 := syscall.NewLazyDLL("kernel32.dll")
    getPhysicallyInstalledSystemMemoryProc := kernel32.NewProc("GetPhysicallyInstalledSystemMemory")

    ret, _, err := getPhysicallyInstalledSystemMemoryProc.Call(uintptr(memory))
    fmt.Printf("GetPhysicallyInstalledSystemMemory, return: %+v\n", ret)
    fmt.Printf("GetPhysicallyInstalledSystemMemory, memory: %d\n", memory)
    fmt.Printf("GetPhysicallyInstalledSystemMemory, err: %s\n", err)

}

1 个答案:

答案 0 :(得分:2)

  1. PULONGLONG参数类型转换为*uint64
  2. 您必须将memory变量的地址转换为unsafe.Pointer类型,然后转换为uintptr
  3. package main
    
    import (
        "fmt"
        "syscall"
        "unsafe"
    )
    
    func main() {
        kernel32 := syscall.NewLazyDLL("kernel32.dll")
        getPhysicallyInstalledSystemMemoryProc := kernel32.NewProc("GetPhysicallyInstalledSystemMemory")
    
        var memory uint64
    
        ret, _, err := getPhysicallyInstalledSystemMemoryProc.Call(uintptr(unsafe.Pointer(&memory)))
        fmt.Printf("GetPhysicallyInstalledSystemMemory, return: %+v\n", ret)
        fmt.Printf("GetPhysicallyInstalledSystemMemory, memory: %d\n", memory)
        fmt.Printf("GetPhysicallyInstalledSystemMemory, err: %s\n", err)
    
    }