Вот код, с которым я столкнулся, для тех, кто решает эту проблему после меня
#include "stdafx.h"
#include <windows.h>
#include <iphlpapi.h>
// These are just for the ntohl function in the printf below
#include <winsock.h>
#pragma comment(lib, "Ws2_32.lib")
DWORD (WINAPI *pGetExtendedTcpTable)(
PVOID pTcpTable,
PDWORD pdwSize,
BOOL bOrder,
ULONG ulAf,
TCP_TABLE_CLASS TableClass,
ULONG Reserved
);
int _tmain(int argc, _TCHAR* argv[])
{
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");
if (!pGetExtendedTcpTable)
{
printf("Could not load iphlpapi.dll. This application is for Windows XP SP2 and up.\n");
return 1;
}
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);
if (dwResult != NO_ERROR)
{
printf("Couldn't get our IP table");
return 2;
}
printf("Iterating though table:\n");
for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++)
{
owner = &pTCPInfo->table[dwLoop];
printf(" PID: %5u - Port: %5u\n", owner->dwOwningPid, ntohs(owner->dwLocalPort));
}
// Pause a moment
printf("Done Processing\n");
return 0;
}