英文:
How to consume a Windows DLL method
问题
我正在尝试使用GetPhysicallyInstalledSystemMemory
方法,该方法位于kernel32.dll中。
它需要一个类型为PULONGLONG的单个参数,但我不知道如何将其映射到golang变量。以下是我目前的尝试,结果显示"err: The parameter is incorrect"。
有人可以解释如何做到这一点吗?
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)
}
英文:
I am trying to use the GetPhysicallyInstalledSystemMemory
method which is present in kernel32.dll.
It requires a single argument of the type PULONGLONG, but I have no idea how to map this into a golang variable. Here is my current attempt which results in "err: The parameter is incorrect".
Can anyone explain how to do this?
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
得分: 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)
}
英文:
- The
PULONGLONG
parameter type translates to*uint64
- You must cast the the address of the
memory
variable to theunsafe.Pointer
type and then touintptr
<!-- -->
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论