質問

want to get the applications that use opened ports in my PC. I used GetTcpPort to retrieve the list of opened ports

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

#define addr_size (3 + 3*4 + 1)   // xxx.xxx.xxx.xxx\0

char const *dotted(DWORD input) {
    char output[addr_size];

    sprintf(output, "%d.%d.%d.%d", 
        input>>24, 
        (input>>16) & 0xff, 
        (input>>8)&0xff, 
        input & 0xff);
    return strdup(output);
}

int main() { 
    MIB_TCPTABLE *tcp_stats = NULL;
    MIB_UDPTABLE *udp_stats = NULL;
    MIB_TCPROW2 *a = NULL;
    DWORD size = 0;
    unsigned i;
    char const *s1, *s2;

    GetTcpTable(tcp_stats, &size, TRUE);
    tcp_stats = (MIB_TCPTABLE *)malloc(size);
    GetTcpTable(tcp_stats, &size, TRUE);
    printf("les ports :");
    for (i=0; i<tcp_stats->dwNumEntries; ++i) {
        printf("TCP:\t:%d\n",            
            ntohs(tcp_stats->table[i].dwLocalPort));
    }
    free(tcp_stats);
    system("pause");
    return 0;
}

But, I want to get the application that use each port.

役に立ちましたか?

他のヒント

On Vista and above each MIB_TCPROW2 row from the connection table returned from GetTcpTable2 has a dwOwningPid member that contains the process identifier of the creating process.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top