如何调用 Windows DLL 方法

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

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

  1. PULONGLONG 参数类型翻译为 *uint64
  2. 您必须将 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)
}
英文:
  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

<!-- -->

package main

import (
    &quot;fmt&quot;
    &quot;syscall&quot;
	&quot;unsafe&quot;
)

func main() {
    kernel32 := syscall.NewLazyDLL(&quot;kernel32.dll&quot;)
    getPhysicallyInstalledSystemMemoryProc := kernel32.NewProc(&quot;GetPhysicallyInstalledSystemMemory&quot;)

	var memory uint64

    ret, _, err := getPhysicallyInstalledSystemMemoryProc.Call(uintptr(unsafe.Pointer(&amp;memory)))
    fmt.Printf(&quot;GetPhysicallyInstalledSystemMemory, return: %+v\n&quot;, ret)
    fmt.Printf(&quot;GetPhysicallyInstalledSystemMemory, memory: %d\n&quot;, memory)
    fmt.Printf(&quot;GetPhysicallyInstalledSystemMemory, err: %s\n&quot;, err)

}

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:

确定