drxt70655 2017-11-21 00:22
浏览 118
已采纳

调用GetExtendedTcpTable时为空响应()

I am new to Go, and I need to catch net information in Windows. I tried to call GetExtendedTcpTable() with pointers to byte arrays as params but get nothing after the call.

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,
)

I expected some data in 'buffer' and 'table', but there are only 0. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongxie2613 2017-11-21 15:13
    关注

    Your code has two errors. First, you pass in legnth=0, which causes GetExtendedTcpTable() to return ERROR_INSUFFICIENT_BUFFER 122 (0x7A). Then, the fifth parameter is not a pointer to the table itself, but an input parameter that states the class (type) of the table to return (write into parameter 1. Here is a corrected version to get over these hurdles:

    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)
    }
    

    I figured this out by inspecting the return code of the GetExtendedTcpTable(). Microsoft system error codes are listed on: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog