doumi1937
2017-09-11 18:33
浏览 132
已采纳

如何使用Windows DLL方法

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
", ret)
    fmt.Printf("GetPhysicallyInstalledSystemMemory, memory: %d
", memory)
    fmt.Printf("GetPhysicallyInstalledSystemMemory, err: %s
", err)

}

图片转代码服务由CSDN问答提供 功能建议

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

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

任何人都可以解释如何执行此操作吗?

  包main 
 
import(
“ fmt” 
“ syscall” 
)
 
var内存uintptr 
 
func main(){
 kernel32:= syscall.NewLazyDLL(“ kernel32.dll”)\  n getPhysicallyInstalledSystemMemoryProc:= kernel32.NewProc(“ GetPhysicalInstalledSystemMemory”)
 
 ret,_,err == getPhysicallyInstalledSystemMemoryProc.Call(uintptr(memory))
 fmt.Printf(“ GetPhysicallyInstalledSystemMemory,返回:%+ v 
”,  ret)
 fmt.Printf(“ GetPhysicallyInstalledSystemMemory,内存:%d 
”,内存)
 fmt.Printf(“ GetPhysicallyInstalledSystemMemory,错误:%s 
”,err)
 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douyasihefu6214 2017-09-11 18:56
    已采纳
    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 (
        "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
    ", ret)
        fmt.Printf("GetPhysicallyInstalledSystemMemory, memory: %d
    ", memory)
        fmt.Printf("GetPhysicallyInstalledSystemMemory, err: %s
    ", err)
    
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题