我使用此代码获取打开的 UDP 端口和使用每个端口的应用程序。
DWORD (WINAPI *pGetExtendedUdpTable)(
PVOID pUdpTable,
PDWORD pdwSize,
BOOL bOrder,
ULONG ulAf,
UDP_TABLE_CLASS TableClass,
ULONG Reserved
);
MIB_UDPTABLE_OWNER_PID *pUDPInfo;
MIB_UDPROW_OWNER_PID *owner;
DWORD size;
DWORD dwResult;
HMODULE hLib = LoadLibrary((LPCWSTR)"iphlpapi.dll");
pGetExtendedUdpTable = (DWORD (WINAPI *)(PVOID,PDWORD,BOOL,ULONG,UDP_TABLE_CLASS,ULONG))
GetProcAddress(hLib, "GetExtendedTcpTable");
if ( !pGetExtendedUdpTable )
{
//printf("Could not load iphlpapi.dll. This application is for Windows XP SP2 and up.\n");
return "Could not load iphlpapi.dll. This application is for Windows XP SP2 and up.\n";
}
dwResult = pGetExtendedUdpTable(NULL, &size, false, AF_INET, (UDP_TABLE_CLASS)5, 0);
pUDPInfo = (MIB_UDPTABLE_OWNER_PID*)malloc(size);
dwResult = pGetExtendedUdpTable(pUDPInfo, &size, false, AF_INET, (UDP_TABLE_CLASS)5, 0);
//
if (dwResult != NO_ERROR)
{
printf("Couldn't get our IP table");
return "Couldn't get our IP table";
}
for (DWORD dwLoop = 0; dwLoop < pUDPInfo->dwNumEntries; dwLoop++)
{
owner = &pUDPInfo->table[dwLoop];
ApplicationName = GetNameByPID(owner->dwOwningPid);
OpenedPort = convertInt(ntohs(owner->dwLocalPort));
LocalAdress = convertInt(ntohs(owner->dwLocalAddr));
}
但是,我没有与 netstat -ano 相同的结果。在我的代码中,我有超过 10 次端口 0,但在 netstat 命令中,端口 0 不存在。