Frage

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

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

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top