怖客-致力于对网络技术的研究!

delphi获取进程信息的网络信息实例代码

自行添加内容

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ActnList, ComCtrls, StdCtrls, winsock, tlhelp32, ExtCtrls;

type
  MIB_TCPEXRow = record
    dwState: DWORD;
    dwLocalAddr: DWORD;
    dwLocalPort: DWORD;
    dwRemoteAddr: DWORD;
    dwRemotePort: DWORD;
    dwProcessId: DWORD;
  end;
  PMIB_TCPExRow = ^MIB_TCPExRow;

  MIB_TCPExTable = record
    dwNumEntries: DWORD;
    table: array[0..50] of MIB_TCPExRow; // 50 ? .. ANY_SIZE !
  end;
  PMIB_TCPExTable = ^MIB_TCPExTable;

  MIB_UDPExRow = record
    dwLocalAddr: DWORD;
    dwLocalPort: DWORD;
    dwProcessID: DWORD;
  end;
  PMIB_UDPExRow = ^MIB_UDPExRow;

  MIB_UDPExTable = record
    dwNumEntries: DWORD;
    table: array[0..50] of MIB_UDPExRow;
  end;
  PMIB_UDPExTable = ^MIB_UDPExTable;

type
  TForm1 = class(TForm)
    Button1: TButton; //onclick -> acRefreshExecute , Action->acRefresh
    ListView1: TListView;
    procedure acRefreshExecute(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    function GetIP(ipAddr: cardinal): string;
    function GetPort(port: cardinal): string;
    function PIDToName(ProcessSnap: THandle; PID: dword): string;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  TcpState: array[0..12] of string = ('???',
    'CLOSED',
    'LISTENING',
    'SYN_SENT',
    'SYN_RCVD',
    'ESTABLISHED',
    'FIN_WAIT1',
    'FIN_WAIT2',
    'CLOSE_WAIT',
    'CLOSING',
    'LAST_ACK',
    'TIME_WAIT',
    'Delete_TCB');

implementation

{$R *.dfm}



function AllocateAndGetTcpExTableFromStack(TCPExTable: PMIB_TCPExTable;  order: boolean;  AllocHeap: THandle;  AlloFlags: integer;  ProtocolVer: integer): integer;
  stdcall; external 'iphlpapi.dll' name 'AllocateAndGetTcpExTableFromStack';

function AllocateAndGetUdpExTableFromStack(UDPExTable: PMIB_UDPExTable;  order: boolean;  AllocHeap: THandle;  AlloFlags: integer;  ProtocolVer: integer): integer;
  stdcall; external 'iphlpapi.dll' name 'AllocateAndGetUdpExTableFromStack';

function TForm1.GetIP(ipAddr: cardinal): string;
var nIpAddr: cardinal;
begin
  nIpAddr := htonl(ipAddr);
  Result := IntToStr(HIByte(HIWord(nIpAddr))) + '.' +
    IntToStr(LOByte(HIWord(nIpAddr))) + '.' +
    IntToStr(HIByte(LOWord(nIpAddr))) + '.' +
    IntToStr(LOByte(LOWord(nIpAddr)));
end;

function TForm1.GetPort(port: cardinal): string;
begin
  Result := inttostr(htons(word(port)));
end;

function TForm1.PIDToName(ProcessSnap: THandle; PID: dword): string;
var ProcessEntry: TProcessEntry32;
begin
  if (not Process32First(ProcessSnap, ProcessEntry)) then
    Result := '????'
  else begin
    while PID <> ProcessEntry.th32ProcessID do
      Process32Next(ProcessSnap, ProcessEntry);
    Result := ProcessEntry.szExeFile
  end
end;

procedure TForm1.acRefreshExecute(Sender: TObject);
var
  i: integer;
  tcp: PMIB_TCPExTable;
  udp: PMIB_UDPExTable;
  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;
  ListView1.Items.Clear;
  for i := 0 to tcp.dwNumEntries - 1 do begin
    item := nil;
    item := ListView1.Items.Add();
    item.Caption := 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(PIDToName(psnap, tcp.table[i].dwProcessId)); // Process
    item.SubItems.Add(IntToStr(tcp.table[i].dwProcessId)); // PId
    item.SubItems.Add(TCPState[tcp.table[i].dwState]); //Status
    item.SubItems.Add('TCP'); //Protocol
    //todo : add path
  end;

  for i := 0 to udp.dwNumEntries - 1 do begin
    item := nil;
    item := ListView1.Items.Add();
    item.Caption := GetIP(udp.table[i].dwLocalAddr);
    item.SubItems.Add(GetPort(udp.table[i].dwLocalPort));
    item.SubItems.Add('');
    item.SubItems.Add('');
    item.SubItems.Add(PIDToName(psnap, udp.table[i].dwProcessId));
    item.SubItems.Add(IntToStr(udp.table[i].dwProcessId));
    item.SubItems.Add('');
    item.SubItems.Add('UDP')
  end;
  CloseHandle(psnap);

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  acRefreshExecute(nil);
end;

end.



 

自行添加内容
标签:delphi实例
分类:编程教程| 发布:lxsky| 查看: | 发表时间:2012-01-03
原创文章如转载,请注明:转载自怖客,delphi教程,socket编程,vc 教程,电脑技术培训,网络安全 http://www.bkhack.com/
本文链接:http://www.bkhack.com/biancheng/delphiHuoQuJinChengXinXiDeWangLuoXinXiShiLiDaiMa.html

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

自行添加内容