2013-03-21 48 views
0

想要獲取使用我的PC中打開的端口的應用程序。我以前GetTcpPort檢索開放的端口使用打開的端口獲取應用程序

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

的列表,但是,我想使用的每個端口的應用程序。

回答

1

在Vista和更高版本上,從GetTcpTable2返回的連接表中的每個MIB_TCPROW2行都有一個包含創建進程的進程標識符的dwOwningPid成員。

+0

GetTcpTable2可與XP? – Bacem 2013-03-21 14:40:33

+0

沒有。也許在XP SP2上的'GetExtendedTcpTable' http://stackoverflow.com/questions/6499845/finding-tcp-ports-used-by-application – 2013-03-21 14:56:33

相關問題