使用打开的端口获取应用程序

Get applications using opened ports

本文关键字:获取 应用程序      更新时间:2023-10-16

想要获取在我的电脑中使用打开端口的应用程序。我使用GetTcpPort检索打开端口的列表

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#define addr_size (3 + 3*4 + 1)   // xxx.xxx.xxx.xxx
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:%dn",            
            ntohs(tcp_stats->table[i].dwLocalPort));
    }
    free(tcp_stats);
    system("pause");
    return 0;
}

但是,我想得到使用每个端口的应用程序。

您可以使用WMI类Win32_Processhttp://msdn.microsoft.com/en-us/library/windows/desktop/aa394372(v=vs.85(.aspx

在Vista及以上版本中,从GetTcpTable2返回的连接表中的每一行MIB_TCPROW2都有一个dwOwningPid成员,该成员包含创建进程的进程标识符。