gpt4 book ai didi

delphi - 如何通过使用delphi 7中的API获取Netstat信息

转载 作者:行者123 更新时间:2023-12-03 15:07:55 25 4
gpt4 key购买 nike

我的任务是查找 abt n/w 信息,或者 Windows 中 netstat 命令给出的信息。现在,我被告知使用一些 API 来提取该信息。任何可用于 delphi 7 执行此任务的 API 都会有所帮助。我遇到过这个 API,即 IP 帮助器 API,但我在我的电脑中找不到它。我只能在 C:\Windows\System32 中找到 DLL“iphlpapi.dll”。此外,关于如何使用这个特定 API 的信息似乎很少。请帮忙。

提前致谢

附注早些时候,我通过执行 Netstat 命令,将输出写入文本文件,然后解析相同的内容以进行显示,这对我来说是一个非常好的方法。然而我的先生对此不太满意。到底是什么原因,我百思不得其解。

最佳答案

检查这些Windows函数GetTcpTable , GetUdpTable , GetExtendedTcpTable , GetExtendedUdpTable .

更新

{$APPTYPE CONSOLE}

uses
WinSock,
Windows,
SysUtils;

const
ANY_SIZE = 1;
iphlpapi = 'iphlpapi.dll';
TCP_TABLE_OWNER_PID_ALL = 5;

MIB_TCP_STATE:
array[1..12] of string = ('CLOSED', 'LISTEN', 'SYN-SENT ','SYN-RECEIVED', 'ESTABLISHED', 'FIN-WAIT-1',
'FIN-WAIT-2', 'CLOSE-WAIT', 'CLOSING','LAST-ACK', 'TIME-WAIT', 'delete TCB');

type
TCP_TABLE_CLASS = Integer;

PMibTcpRowOwnerPid = ^TMibTcpRowOwnerPid;
TMibTcpRowOwnerPid = packed record
dwState : DWORD;
dwLocalAddr : DWORD;
dwLocalPort : DWORD;
dwRemoteAddr: DWORD;
dwRemotePort: DWORD;
dwOwningPid : DWORD;
end;

PMIB_TCPTABLE_OWNER_PID = ^MIB_TCPTABLE_OWNER_PID;
MIB_TCPTABLE_OWNER_PID = packed record
dwNumEntries: DWORD;
table: Array [0..ANY_SIZE - 1] of TMibTcpRowOwnerPid;
end;

var
GetExtendedTcpTable:function (pTcpTable: Pointer; dwSize: PDWORD; bOrder: BOOL; lAf: ULONG; TableClass: TCP_TABLE_CLASS; Reserved: ULONG): DWord; stdcall;

procedure ShowCurrentTCPConnections;
var
Error : DWORD;
TableSize : DWORD;
i : integer;
IpAddress : in_addr;
RemoteIp : string;
LocalIp : string;
pTcpTable : PMIB_TCPTABLE_OWNER_PID;
begin
TableSize := 0;
//Get the size o the tcp table
Error := GetExtendedTcpTable(nil, @TableSize, False, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
if Error <> ERROR_INSUFFICIENT_BUFFER then exit;

//alocate the buffer
GetMem(pTcpTable, TableSize);
try
Writeln(Format('%-16s %-6s %-16s %-6s %s',['Local IP','Port','Remote IP','Port','Status']));
//get the tcp table data
if GetExtendedTcpTable(pTcpTable, @TableSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0) = NO_ERROR then
for i := 0 to pTcpTable.dwNumEntries - 1 do
begin
IpAddress.s_addr := pTcpTable.Table[i].dwRemoteAddr;
RemoteIp := string(inet_ntoa(IpAddress));
IpAddress.s_addr := pTcpTable.Table[i].dwLocalAddr;
LocalIp := string(inet_ntoa(IpAddress));
Writeln(Format('%-16s %-6d %-16s %-6d %s',[LocalIp,pTcpTable.Table[i].dwLocalPort,RemoteIp,pTcpTable.Table[i].dwRemotePort,MIB_TCP_STATE[pTcpTable.Table[i].dwState]]));
end;
finally
FreeMem(pTcpTable);
end;
end;

var
hModule : THandle;
begin
try
hModule := LoadLibrary(iphlpapi);
GetExtendedTcpTable := GetProcAddress(hModule, 'GetExtendedTcpTable');
ShowCurrentTCPConnections;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.

关于delphi - 如何通过使用delphi 7中的API获取Netstat信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6715516/

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