我是新手,我需要在Windows中捕捉networking信息。 我试图调用GetExtendedTcpTable()
指针作为参数的字节数组,但在调用后什么也没有。
var ( iphelp = syscall.NewLazyDLL("iphlpapi.dll") tcptable = iphelp.NewProc("GetExtendedTcpTable") ) var ( buffer [20000]byte table [20000]byte length int ) res1, res2, err := tcptable.Call( uintptr(unsafe.Pointer(&buffer)), uintptr(unsafe.Pointer(&length)), 1, syscall.AF_INET, uintptr(unsafe.Pointer(&table)), 0, )
我期望在'缓冲区'和'表'中的一些数据,但只有0.我做错了什么?
你的代码有两个错误。 首先,您传入legnth = 0,这会导致GetExtendedTcpTable()返回ERROR_INSUFFICIENT_BUFFER 122(0x7A)。 然后,第五个参数不是一个指向表本身的指针,而是一个输入参数,用于指明要返回的表的类(类型)(写入参数1)。这是一个纠正版本来克服这些障碍:
import ( "fmt" "syscall" "unsafe" ) const ( TCP_TABLE_BASIC_LISTENER = iota TCP_TABLE_BASIC_CONNECTIONS TCP_TABLE_BASIC_ALL TCP_TABLE_OWNER_PID_LISTENER TCP_TABLE_OWNER_PID_CONNECTIONS TCP_TABLE_OWNER_PID_ALL TCP_TABLE_OWNER_MODULE_LISTENER TCP_TABLE_OWNER_MODULE_CONNECTIONS TCP_TABLE_OWNER_MODULE_ALL ) func main() { var table [2000]byte var length int = len(table) iphelp := syscall.NewLazyDLL("iphlpapi.dll") tcptable := iphelp.NewProc("GetExtendedTcpTable") length = len(table) res1, res2, err := tcptable.Call( uintptr(unsafe.Pointer(&table)), uintptr(unsafe.Pointer(&length)), 1, syscall.AF_INET, TCP_TABLE_BASIC_LISTENER, 0, ) fmt.Println(res1, res2, length, err) fmt.Println(table) }
我通过检查GetExtendedTcpTable()的返回码来解决这个问题。 Microsoft系统错误代码列在: https : //msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx