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

标签 delphi

我的任务是查找 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/

相关文章:

delphi - 如何使我的 32 位 Delphi 应用程序能够在 64 位 Windows 上使用 4GB 内存(通过 Wow64.exe)?

delphi - Delphi中的圆角矩形

multithreading - delphi - 类静态方法,多线程应用程序中的静态变量

delphi - 父字体似乎随机变化

delphi - 查找用户可见的所有控件

delphi - 为什么我的组件会自动将其他单元添加到使用界面?

delphi - 我应该使用什么来以编程方式在 Delphi 中生成打印输出/报告

delphi - JclDebug 不包括行号

delphi - 初始化时获取当前包名称

delphi - 如何使组件在表单设计器中消失?