去系统调用EnumProcessModules

时间:2015-12-19 19:35:29

标签: go system-calls

我试图调用; EnumProcessModules;使用给定句柄的Go

var (
    psapi = syscall.NewLazyDLL("Psapi.dll")
    procEnumProcessModules = psapi.NewProc("EnumProcessModules")
)

func EnumProcessModules(handle uintptr) {
    log.Println(handle)
    modules := make([]interface{}, 2049)
    var needed int
    procEnumProcessModules.Call(
        handle,
        uintptr(unsafe.Pointer(&modules)),
        uintptr(2048),
        uintptr(unsafe.Pointer(&needed)),
    )
    log.Println(needed)
    for i := 0; i < needed; i++ {
        log.Println(modules[i])
    }
}

一切似乎都有效...需要设置但是当我尝试访问模块变量时,我得到以下错误:

unexpected fault address 0x12d0000
fatal error: fault
[signal 0xc0000005 code=0x0 addr=0x12d0000 pc=0x455b56]

模块阵列应该如何?

1 个答案:

答案 0 :(得分:0)

是的,您必须引用第一个元素。

var n uint32
var needed uint32

// How many I need?
ret, _, _ := procEnumProcessModules.Call(
  uintptr(handle),
  0,
  uintptr(n),
  uintptr(unsafe.Pointer(&needed)))

if int(ret) == 1 && needed > 0 {

  procHandles := make([]syscall.Handle, needed) 
  procHandlesPtr := unsafe.Pointer(&procHandles[0])

  n = needed
  ret2, _, _ := procEnumProcessModules.Call(
    uintptr(n),
    uintptr(procHandlesPtr),
    uintptr(n), 
    uintptr(unsafe.Pointer(&needed))))

  if int(ret2) == 1 {
    for i:= 0; i < needed / 4; i++ {
       fmt.Println(procHandles[i])
  }
}