如何调用 Windows DLL 方法

huangapple go评论117阅读模式
英文:

How to consume a Windows DLL method

问题

我正在尝试使用GetPhysicallyInstalledSystemMemory方法,该方法位于kernel32.dll中。

它需要一个类型为PULONGLONG的单个参数,但我不知道如何将其映射到golang变量。以下是我目前的尝试,结果显示"err: The parameter is incorrect"。

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

  1. package main
  2. import (
  3. "fmt"
  4. "syscall"
  5. )
  6. var memory uintptr
  7. func main() {
  8. kernel32 := syscall.NewLazyDLL("kernel32.dll")
  9. getPhysicallyInstalledSystemMemoryProc := kernel32.NewProc("GetPhysicallyInstalledSystemMemory")
  10. ret, _, err := getPhysicallyInstalledSystemMemoryProc.Call(uintptr(memory))
  11. fmt.Printf("GetPhysicallyInstalledSystemMemory, return: %+v\n", ret)
  12. fmt.Printf("GetPhysicallyInstalledSystemMemory, memory: %d\n", memory)
  13. fmt.Printf("GetPhysicallyInstalledSystemMemory, err: %s\n", err)
  14. }
英文:

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?

  1. package main
  2. import (
  3. "fmt"
  4. "syscall"
  5. )
  6. var memory uintptr
  7. func main() {
  8. kernel32 := syscall.NewLazyDLL("kernel32.dll")
  9. getPhysicallyInstalledSystemMemoryProc := kernel32.NewProc("GetPhysicallyInstalledSystemMemory")
  10. ret, _, err := getPhysicallyInstalledSystemMemoryProc.Call(uintptr(memory))
  11. fmt.Printf("GetPhysicallyInstalledSystemMemory, return: %+v\n", ret)
  12. fmt.Printf("GetPhysicallyInstalledSystemMemory, memory: %d\n", memory)
  13. fmt.Printf("GetPhysicallyInstalledSystemMemory, err: %s\n", err)
  14. }

答案1

得分: 2

  1. PULONGLONG 参数类型翻译为 *uint64
  2. 您必须将 memory 变量的地址转换为 unsafe.Pointer 类型,然后再转换为 uintptr
  1. package main
  2. import (
  3. "fmt"
  4. "syscall"
  5. "unsafe"
  6. )
  7. func main() {
  8. kernel32 := syscall.NewLazyDLL("kernel32.dll")
  9. getPhysicallyInstalledSystemMemoryProc := kernel32.NewProc("GetPhysicallyInstalledSystemMemory")
  10. var memory uint64
  11. ret, _, err := getPhysicallyInstalledSystemMemoryProc.Call(uintptr(unsafe.Pointer(&memory)))
  12. fmt.Printf("GetPhysicallyInstalledSystemMemory, return: %+v\n", ret)
  13. fmt.Printf("GetPhysicallyInstalledSystemMemory, memory: %d\n", memory)
  14. fmt.Printf("GetPhysicallyInstalledSystemMemory, err: %s\n", err)
  15. }
英文:
  1. The PULONGLONG parameter type translates to *uint64
  2. You must cast the the address of the memory variable to the unsafe.Pointer type and then to uintptr

<!-- -->

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;syscall&quot;
  5. &quot;unsafe&quot;
  6. )
  7. func main() {
  8. kernel32 := syscall.NewLazyDLL(&quot;kernel32.dll&quot;)
  9. getPhysicallyInstalledSystemMemoryProc := kernel32.NewProc(&quot;GetPhysicallyInstalledSystemMemory&quot;)
  10. var memory uint64
  11. ret, _, err := getPhysicallyInstalledSystemMemoryProc.Call(uintptr(unsafe.Pointer(&amp;memory)))
  12. fmt.Printf(&quot;GetPhysicallyInstalledSystemMemory, return: %+v\n&quot;, ret)
  13. fmt.Printf(&quot;GetPhysicallyInstalledSystemMemory, memory: %d\n&quot;, memory)
  14. fmt.Printf(&quot;GetPhysicallyInstalledSystemMemory, err: %s\n&quot;, err)
  15. }

huangapple
  • 本文由 发表于 2017年9月12日 02:33:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/46162245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定