这么简单 var i: integer; tcp: PMIB_TCPTABLE;//PMIB_TCPExTable; udp: PMIB_UDPTABLE; item: TListItem; psnap: THandle; begin i := AllocateAndGetTCPExTableFromStack(@tcp, true, GetProcessHeap(), 2, 2); if i <> 0 then exit; i := AllocateAndGetUDPExTableFromStack(@udp, true, GetProcessHeap(), 2, 2); if i <> 0 then exit; psnap := CreateToolHelp32SnapShot(2,0); if psnap = INVALID_HANDLE_VALUE then exit; PortForm.lv_Port.Items.Clear; for i := 0 to tcp.dwNumEntries -1 do begin item := lv_Port.Items.Add(); item.Caption :=IntToStr(tcp.table[i].dwProcessId); // Process item.SubItems.Add(GetIP(tcp.table[i].dwLocalAddr)); //local IP item.SubItems.Add(GetPort(tcp.table[i].dwLocalPort)); //Local Port item.SubItems.Add(GetIP(tcp.table[i].dwRemoteAddr)); // Remote IP item.SubItems.Add(GetPort(tcp.table[i].dwRemotePort)); // Remote Port item.SubItems.Add(GetProcessPath(tcp.table[i].dwProcessId)); // Process //item.SubItems.Add(IntToStr(tcp.table[i].dwProcessId)); // PId item.SubItems.Add(TCPConnState[tcp.table[i].dwState]); //Status //item.SubItems.Add() item.SubItems.Add('TCP'); //Protocol //todo : add path end; for i := 0 to udp.dwNumEntries -1 do begin item := lv_Port.Items.Add(); item.Caption :=IntToStr(udp.table[i].dwProcessId); item.SubItems.add(GetIP(udp.table[i].dwLocalAddr)); item.SubItems.Add(GetPort(udp.table[i].dwLocalPort)); item.SubItems.Add(''); item.SubItems.Add(''); item.SubItems.Add(GetProcessPath(udp.table[i].dwProcessId)); //item.SubItems.Add(IntToStr(udp.table[i].dwProcessId)); item.SubItems.Add(''); item.SubItems.Add('UDP') end; CloseHandle(psnap);lv_Port是一ListView今天心情好,就给你贴出来,给分三
只能判断整个系统是否联网,而不能判断某个进程吧。
GetExtendedTcpTable
GetExtendedUdpTable
分别是针对TCP和UDP的。
用它们可以得到系统当前的网络连接列表,并可列出IP和端口等,以及每个连接所对应的进程ID。
然后你再在该列表中去查询你指定的PID即可。
至于它们的具体用法,请查MSDN,或网上找具体的例子吧,比较多。
var
i: integer;
tcp: PMIB_TCPTABLE;//PMIB_TCPExTable;
udp: PMIB_UDPTABLE;
item: TListItem;
psnap: THandle;
begin
i := AllocateAndGetTCPExTableFromStack(@tcp, true, GetProcessHeap(), 2, 2);
if i <> 0 then exit;
i := AllocateAndGetUDPExTableFromStack(@udp, true, GetProcessHeap(), 2, 2);
if i <> 0 then exit;
psnap := CreateToolHelp32SnapShot(2,0);
if psnap = INVALID_HANDLE_VALUE then exit;
PortForm.lv_Port.Items.Clear;
for i := 0 to tcp.dwNumEntries -1 do
begin
item := lv_Port.Items.Add();
item.Caption :=IntToStr(tcp.table[i].dwProcessId); // Process
item.SubItems.Add(GetIP(tcp.table[i].dwLocalAddr)); //local IP
item.SubItems.Add(GetPort(tcp.table[i].dwLocalPort)); //Local Port
item.SubItems.Add(GetIP(tcp.table[i].dwRemoteAddr)); // Remote IP
item.SubItems.Add(GetPort(tcp.table[i].dwRemotePort)); // Remote Port
item.SubItems.Add(GetProcessPath(tcp.table[i].dwProcessId)); // Process
//item.SubItems.Add(IntToStr(tcp.table[i].dwProcessId)); // PId
item.SubItems.Add(TCPConnState[tcp.table[i].dwState]); //Status
//item.SubItems.Add()
item.SubItems.Add('TCP'); //Protocol
//todo : add path
end;
for i := 0 to udp.dwNumEntries -1 do
begin
item := lv_Port.Items.Add();
item.Caption :=IntToStr(udp.table[i].dwProcessId);
item.SubItems.add(GetIP(udp.table[i].dwLocalAddr));
item.SubItems.Add(GetPort(udp.table[i].dwLocalPort));
item.SubItems.Add('');
item.SubItems.Add('');
item.SubItems.Add(GetProcessPath(udp.table[i].dwProcessId));
//item.SubItems.Add(IntToStr(udp.table[i].dwProcessId));
item.SubItems.Add('');
item.SubItems.Add('UDP')
end;
CloseHandle(psnap);lv_Port是一ListView今天心情好,就给你贴出来,给分三
PMIB_TCPTABLE = ^MIB_TCPTABLE;
{$EXTERNALSYM PMIB_TCPTABLE}
_MIB_TCPTABLE = record
dwNumEntries: DWORD;
table: array [0..ANY_SIZE - 1] of MIB_TCPROW;
end;
{$EXTERNALSYM _MIB_TCPTABLE}
MIB_TCPTABLE = _MIB_TCPTABLE;
{$EXTERNALSYM MIB_TCPTABLE}
TMibTcpTable = MIB_TCPTABLE;
PMibTcpTable = PMIB_TCPTABLE;
AllocateAndGetUdpExTableFromStack
上面这两个API从Vista及以后版本的系统就不再支持了楼主若考虑向后兼容性的话,还是用我之前说的下面这两个API吧
GetExtendedTcpTable
GetExtendedUdpTable用法类似.
type
PMIB_TCPTABLE = ^MIB_TCPTABLE;
{$EXTERNALSYM PMIB_TCPTABLE}
_MIB_TCPTABLE = record
dwNumEntries: DWORD;
table: array [0..ANY_SIZE - 1] of MIB_TCPROW; //这里错误,错误信息如下:
end;
{$EXTERNALSYM _MIB_TCPTABLE}
MIB_TCPTABLE = _MIB_TCPTABLE;
{$EXTERNALSYM MIB_TCPTABLE}
TMibTcpTable = MIB_TCPTABLE;
PMibTcpTable = PMIB_TCPTABLE;
[Error] uMain.pas(269): Undeclared identifier: 'ANY_SIZE'改怎么办?朋友能否发个到我邮箱? 邮箱[email protected]