我正在尝试使用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)
}
答案 0 :(得分:2)
PULONGLONG
参数类型转换为*uint64
memory
变量的地址转换为unsafe.Pointer
类型,然后转换为uintptr
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)
}