Reputation: 75
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.
Upvotes: 0
Views: 1016
Reputation: 582
You can use WMI class Win32_Process
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372(v=vs.85).aspx
Upvotes: 2
Reputation: 175768
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.
Upvotes: 1