本文介绍了GetExtendedTcpTable不会返回与netstat -ano相同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这段代码在我的电脑和使用每个端口的应用程序打开的端口列表。

I used this code to get list of Opened port in my PC and the application that use each port.

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端口。

But, if I compare the result with the result of netstat -ano this function doesn't return all TCP ports.

推荐答案

$ c> TCP_TABLE_OWNER_PID_ALL 代替 TCP_TABLE_OWNER_PID_LISTENER ,您将获得所有TCP端口。

Use TCP_TABLE_OWNER_PID_ALL in place of TCP_TABLE_OWNER_PID_LISTENER and you will get all TCP ports.

这篇关于GetExtendedTcpTable不会返回与netstat -ano相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 13:03