普通网友 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)

}
  • 写回答

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)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度