在 golang 调用 DLL 上

On the golang call DLL?

本文关键字:DLL 调用 golang      更新时间:2023-10-16
package main
import (
    "fmt"
    "syscall"
    "unsafe"
)
const (
    PROCESS_QUERY_INFORMATION = 1 << 10
    PROCESS_VM_READ           = 1 << 4
)
//defind a struct
type PROCESS_MEMORY_COUNTERS struct {
    cb                         uint32
    PageFaultCount             uint32
    PeakWorkingSetSize         uint64
    WorkingSetSize             uint64
    QuotaPeakPagedPoolUsage    uint64
    QuotaPagedPoolUsage        uint64
    QuotaPeakNonPagedPoolUsage uint64
    QuotaNonPagedPoolUsage     uint64
    PagefileUsage              uint64
    PeakPagefileUsage          uint64
}
func main() {
    //get Process Handle
    current, err := syscall.OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, false, 728)
    if err != nil {
        return
    }
    //call psapi.ll
    psapi := syscall.NewLazyDLL("psapi.dll")
    var process PROCESS_MEMORY_COUNTERS
    process.cb = uint32(unsafe.Sizeof((process)))
    GetProcessMemoryInfo := psapi.NewProc("GetProcessMemoryInfo")
    _, _, err = GetProcessMemoryInfo.Call(uintptr(current), uintptr(unsafe.Pointer(&process)), unsafe.Sizeof(&process))
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(process)
    }
}

报告此错误:

传递给系统调用的数据区域太小

unsafe.Sizeof(&process) 返回指针的大小 — 变量 process 占用的内存地址。

我想你想用unsafe.Sizeof(process)来做这件事。