Go系统调用EnumProcessModule
我想打电话;枚举处理模块;使用给定句柄的GoGo系统调用EnumProcessModule,go,system-calls,Go,System Calls,我想打电话;枚举处理模块;使用给定句柄的Go var ( psapi = syscall.NewLazyDLL("Psapi.dll") procEnumProcessModules = psapi.NewProc("EnumProcessModules") ) func EnumProcessModules(handle uintptr) { log.Println(handle) modules := make([]interface{}, 2049)
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])
}
}
模块数组应该是什么样子?是的,必须引用第一个元素
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])
}
}
var n uint32
var需要uint32
//我需要多少?
ret,,:=procEnumProcessModules.Call(
uintptr(手柄),
0,
uintptr(n),
uintptr(不安全的.指针(&needed)))
如果int(ret)==1&&needed>0{
procHandles:=make([]syscall.Handle,需要)
procHandlesPtr:=不安全。指针(&procHandles[0])
n=需要
ret2,,:=procEnumProcessModules.Call(
uintptr(n),
uintptr(procHandlesPtr),
uintptr(n),
uintptr(不安全的.指针(&needed)))
如果int(ret2)==1{
对于i:=0;i
根据EnumProcessModules文档,模块是一个句柄数组。因此它应该是modules:=make(syscall.Handle,2049)。EnumProcessModules的最后一个参数是*uint32,因此您的“需要”变量应该是uint32,而不是int。int没有固定大小(386上是int32,amd64上是int64)。您正在将一个切片指针传递给对切片一无所知的windows函数。您需要使用数组或使用第一个元素的地址。
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])
}
}