【问题标题】:GetExtendedTcpTable donesn't return the same result as netstat -anoGetExtendedTcpTable 不返回与 netstat -ano 相同的结果
【发布时间】:2013-03-22 14:59:43
【问题描述】:

我使用此代码来获取我的 PC 中打开的端口列表以及使用每个端口的应用程序。

string Port::GetListOfTcpPorts()

{
    string ApplicationName = "";
    string result = "";
    string aux = "";
    string RemotePort = "";
    DWORD (WINAPI *pGetExtendedTcpTable)(
  PVOID pTcpTable,
  PDWORD pdwSize,
  BOOL bOrder,
  ULONG ulAf,
  TCP_TABLE_CLASS TableClass,
  ULONG Reserved
);
    MIB_TCPTABLE_OWNER_PID *pTCPInfo;
    MIB_TCPROW_OWNER_PID *owner;
    DWORD size;
    DWORD dwResult;

    HMODULE hLib = LoadLibrary("iphlpapi.dll");

    pGetExtendedTcpTable = (DWORD (WINAPI *)(PVOID,PDWORD,BOOL,ULONG,TCP_TABLE_CLASS,ULONG))
        GetProcAddress(hLib, "GetExtendedTcpTable");

    dwResult = pGetExtendedTcpTable(NULL,       &size, false, AF_INET, TCP_TABLE_OWNER_PID_LISTENER, 0);
    pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)malloc(size);
    dwResult = pGetExtendedTcpTable(pTCPInfo,   &size, false, AF_INET, TCP_TABLE_OWNER_PID_LISTENER, 0);       
   for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++)
    {
        owner = &pTCPInfo->table[dwLoop];      
        ApplicationName = GetNameByPID(owner->dwOwningPid);
        OpenedPort = convertInt(ntohs(owner->dwLocalPort));         
        RemotePort = convertInt(ntohs(owner->dwRemotePort));
        aux = "TCP ; " + OpenedPort + ";"+ RemotePort+";"+ ApplicationName + "\n";
        result = result + aux;

    }
    return result;
}

但是,如果我将结果与netstat -ano 的结果进行比较,此函数不会返回所有 TCP 端口。

【问题讨论】:

    标签: c++ windows mfc ports netstat


    【解决方案1】:

    使用TCP_TABLE_OWNER_PID_ALL 代替TCP_TABLE_OWNER_PID_LISTENER,您将获得所有TCP 端口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2018-12-02
      • 2018-03-27
      • 2020-11-30
      • 2021-11-21
      • 2022-06-11
      • 2014-11-15
      相关资源
      最近更新 更多