gpt4 book ai didi

c++ - GetExtendedTcpTable 不会返回与 netstat -ano 相同的结果

转载 作者:可可西里 更新时间:2023-11-01 09:25:17 75 4
gpt4 key购买 nike

我使用此代码获取我的 PC 中已打开端口的列表以及使用每个端口的应用程序。

string Port::GetListOfTcpPorts()

{
string ApplicationName = "";
string result = "";
string aux = "";
string RemotePort = "";
DWORD (WINAPI *pGetExtendedTcpTable)(
PVOID pTcpTable,
PDWORD pdwSize,
BOOL bOrder,
ULONG ulAf,
TCP_TABLE_CLASS TableClass,
ULONG Reserved
);
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");

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);
for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++)
{
owner = &pTCPInfo->table[dwLoop];
ApplicationName = GetNameByPID(owner->dwOwningPid);
OpenedPort = convertInt(ntohs(owner->dwLocalPort));
RemotePort = convertInt(ntohs(owner->dwRemotePort));
aux = "TCP ; " + OpenedPort + ";"+ RemotePort+";"+ ApplicationName + "\n";
result = result + aux;

}
return result;
}

但是,如果我将结果与 netstat -ano 的结果进行比较,此函数不会返回所有 TCP 端口。

最佳答案

使用 TCP_TABLE_OWNER_PID_ALL 代替 TCP_TABLE_OWNER_PID_LISTENER,您将获得所有 TCP 端口。

关于c++ - GetExtendedTcpTable 不会返回与 netstat -ano 相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15573504/

75 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com