程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi版進程連接端口查看程序

Delphi版進程連接端口查看程序

編輯:Delphi

Delphi版進程連接端口查看程序,這是主文件的代碼,可編譯成可運行的EXE程序,前兩年收集的Delphi實用程序,現在把主代碼發出來哦。裡面涉及一些UDP/TCP和枚舉進程的相關知識,包括獲取一些端口和路徑信息等:

vIEw source print? 001 unit Unit1; 002 //進程連接端口查看器 003 //作者:yeye55 004 interface 005 uses 006   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 007   Dialogs, Grids, StdCtrls, ExtCtrls, StrUtils, PsAPI, tlhelp32, WinSock; 008 type 009   TForm1 = class(TForm) 010     Panel1: TPanel; 011     Button1: TButton; 012     Label1: TLabel; 013     StringGrid1: TStringGrid; 014     Button2: TButton; 015     Button3: TButton; 016     SaveDialog1: TSaveDialog; 017     procedure FormCreate(Sender: TObject); 018     procedure Button1Click(Sender: TObject); 019     procedure Button2Click(Sender: TObject); 020     procedure Button3Click(Sender: TObject); 021   private 022     { Private declarations } 023   public 024     { Public declarations } 025   end; 026 var 027   Form1: TForm1; 028 //以下常量定義根據VC++6.0自帶IPRTRMIB.H文件中的相關定義改寫 029 const 030     MIB_TCP_STATE_CLOSED     =  1; 031     MIB_TCP_STATE_LISTEN     =  2; 032     MIB_TCP_STATE_SYN_SENT   =  3; 033     MIB_TCP_STATE_SYN_RCVD   =  4; 034     MIB_TCP_STATE_ESTAB      =  5; 035     MIB_TCP_STATE_FIN_WAIT1  =  6; 036     MIB_TCP_STATE_FIN_WAIT2  =  7; 037     MIB_TCP_STATE_CLOSE_WAIT =  8; 038     MIB_TCP_STATE_CLOSING    =  9; 039     MIB_TCP_STATE_LAST_ACK   = 10; 040     MIB_TCP_STATE_TIME_WAIT  = 11; 041     MIB_TCP_STATE_DELETE_TCB = 12; 042 //相關數據結構的定義 043 const 044     ANY_SIZE=256; 045 type 046     MIB_TCPEXROW = record 047         dwState : DWord;      //連接狀態 048         dwLocalAddr : DWord;  //本地地址 049         dwLocalPort : DWord;  //本地端口 050         dwRemoteAddr : DWord; //遠程地址 051         dwRemotePort : DWord; //遠程端口 052         dwProcessId : DWord;  //進程ID號 053     end; 054     MIB_TCPEXTABLE = record 055         dwNumEntrIEs : DWord; //端口數量 056         table : array [0..ANY_SIZE-1of MIB_TCPEXROW; 057     end; 058     PMIB_TCPEXTABLE = ^MIB_TCPEXTABLE; 059     MIB_UDPEXROW = record 060         dwLocalAddr : DWord; //本地地址 061         dwLocalPort : DWord; //本地端口 062         dwProcessId : DWord; //進程ID號 063     end; 064     MIB_UDPEXTABLE = record 065         dwNumEntrIEs : DWord; //端口數量 066         table : array [0..ANY_SIZE-1of MIB_UDPEXROW; 067     end; 068     PMIB_UDPEXTABLE = ^MIB_UDPEXTABLE; 069 //獲取TCP連接表的IP幫助函數聲明 070 function AllocateAndGetTcpExTableFromStack( 071     var pTcpTable; 072     bOrder : Bool; 073     heap : THandle; 074     zero : DWord; 075     flags : DWord 076     ): DWord; stdcall; external 'iphlpapi.dll' 077     name 'AllocateAndGetTcpExTableFromStack'; 078 //獲取UDP連接表的IP幫助函數聲明 079 function AllocateAndGetUdpExTableFromStack( 080     var pUdpTable; 081     bOrder : Bool; 082     heap : THandle; 083     zero : DWord; 084     flags : DWord 085     ): DWord; stdcall; external 'iphlpapi.dll' 086     name 'AllocateAndGetUdpExTableFromStack'; 087 implementation 088 {$R *.dfm} 089 procedure TForm1.FormCreate(Sender: TObject); 090 begin 091     self.StringGrid1.Cells[0,0]:=' 協議'; 092     self.StringGrid1.Cells[1,0]:=' 本地IP地址'; 093     self.StringGrid1.Cells[2,0]:=' 遠程IP地址'; 094     self.StringGrid1.Cells[3,0]:=' 連接狀態'; 095     self.StringGrid1.Cells[4,0]:=' 進程ID'; 096     self.StringGrid1.Cells[5,0]:=' 可執行文件名'; 097     self.StringGrid1.Cells[6,0]:=' 文件路徑'; 098     self.StringGrid1.Cells[7,0]:=' 文件說明 / 公司名稱'; 099     // 100     self.Button1Click(self); 101 end; 102 //將連接狀態轉換為說明字符串 103 function StateToString(dwState : DWord): String; 104 begin 105     result:=''; 106     case dwState of 107         MIB_TCP_STATE_CLOSED     : result:='關閉'; 108         MIB_TCP_STATE_LISTEN     : result:='監聽'; 109         MIB_TCP_STATE_SYN_SENT   : result:='同步發送'; 110         MIB_TCP_STATE_SYN_RCVD   : result:='同步接收'; 111         MIB_TCP_STATE_ESTAB      : result:='已建立'; 112         MIB_TCP_STATE_FIN_WAIT1  : result:='等待完成1'; 113         MIB_TCP_STATE_FIN_WAIT2  : result:='等待完成2'; 114         MIB_TCP_STATE_CLOSE_WAIT : result:='等待關閉'; 115         MIB_TCP_STATE_CLOSING    : result:='正在關閉'; 116         MIB_TCP_STATE_LAST_ACK   : result:='最後確認'; 117         MIB_TCP_STATE_TIME_WAIT  : result:='等待時間'; 118         MIB_TCP_STATE_DELETE_TCB : result:='刪除'; 119         else result:=Format('未知狀態%d',[dwState]); 120     end; 121 end; 122 //根據進程ID查找可執行文件名 123 function IdToExeFile(hSnapshot : THandle; dwProcessId : DWord): String; 124 var 125     lppe : TProcessEntry32; 126     b : BOOL; 127 begin 128     lppe.dwSize:=SizeOf(lppe); 129     //取第1個進程數據 130     b:=Process32First(hSnapshot,lppe); 131     result:=''; 132     while do 133     begin 134         if lppe.th32ProcessID=dwProcessId then 135         begin 136             result:=lppe.szExeFile; 137             break; 138         end; 139         //取下一個進程 140         b:=Process32Next(hSnapshot,lppe); 141     end; 142 end; 143 //根據進程ID查找可執行文件路徑 144 function IdToExeDir(dwProcessId : DWord): String; 145 var 146     cbNeeded : DWord; 147     hProcess : THandle; 148     hModules : HMODULE; 149     lpFilename : array [0..1024-1of Char; 150 begin 151     result:=''; 152     hProcess:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,false,dwProcessId); 153     if hProcess=0 then exit; 154     EnumProcessModules(hProcess,@hModules,sizeof(hModule),cbNeeded); 155     GetModuleFileNameEx(hProcess,hModules,lpFilename,1024); 156     result:=lpFilename; 157     CloseHandle(hProcess); 158 end; 159 //根據文件名查找文件信息 160 function ExeDirToFileInfo(Filename : String): String; 161 var 162     NameLen,BufLen,ValLen : DWord; 163     pBuf,pVal : Pointer; 164     LangCode : String; 165 begin 166     result:=''; 167     NameLen:=Length(Filename); 168     if NameLen=0 then exit; 169     //獲取版本信息 170     BufLen:=GetFileVersionInfoSize(PChar(Filename),NameLen); 171     pBuf:=AllocMem(BufLen); 172     if pBuf=nil then exit; 173     if GetFileVersionInfo(PChar(Filename),0,BufLen,pBuf)=false then 174     begin 175         FreeMem(pBuf); 176         exit; 177     end; 178     //獲取代碼頁信息 179     if VerQueryValue(pBuf,'\VarFileInfo\Translation',pVal,ValLen)=false then 180     begin 181         FreeMem(pBuf); 182         exit; 183     end; 184     LangCode:=Format('%.2x%.2x%.2x%.2x',[ 185         BYTE(PChar(pVal)[1]), 186         BYTE(PChar(pVal)[0]), 187         BYTE(PChar(pVal)[3]), 188         BYTE(PChar(pVal)[2])]); 189     //獲取文件說明 190     if VerQueryValue(pBuf,PChar('\StringFileInfo\'+LangCode+'\FileDescription'),pVal,ValLen)=false then 191     begin 192         FreeMem(pBuf); 193         exit; 194     end; 195     result:=PChar(pVal); 196     //獲取公司名稱 197     if VerQueryValue(pBuf,PChar('\StringFileInfo\'+LangCode+'\CompanyName'),pVal,ValLen)=false then 198     begin 199         FreeMem(pBuf); 200         exit; 201     end; 202     result:=result+' / '+PChar(pVal); 203     FreeMem(pBuf); 204 end; 205 procedure TForm1.Button1Click(Sender: TObject); 206 var 207     hSnapshot : THandle; //進程快照 208     pTcpTab : PMIB_TCPEXTABLE; //TCP連接表 209     pUdpTab : PMIB_UDPEXTABLE; //UDP連接表 210     i : Integer; 211 begin 212     self.StringGrid1.RowCount:=2; 213     //創建進程快照 214     hSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 215     //獲取TCP連接表 216     if AllocateAndGetTcpExTableFromStack(pTcpTab,true,GetProcessHeap(),2,2)<>0 then 217     begin 218         MessageBox(self.Handle,'無法獲取TCP連接表','調用失敗',MB_ICONEXCLAMATION or MB_OK); 219     end 220     else 221     begin 222         //輸出TCP連接表 223         for i:=0 to pTcpTab.dwNumEntrIEs-1 do 224         begin 225             self.StringGrid1.Cells[0,self.StringGrid1.RowCount-1]:= 226                 'TCP'; 227             self.StringGrid1.Cells[1,self.StringGrid1.RowCount-1]:= 228                 inet_ntoa(TInAddr(pTcpTab.table[i].dwLocalAddr))+ 229                 ':'+IntToStr(ntohs(pTcpTab.table[i].dwLocalPort)); 230             self.StringGrid1.Cells[2,self.StringGrid1.RowCount-1]:= 231                 inet_ntoa(TInAddr(pTcpTab.table[i].dwRemoteAddr))+ 232                 ':'+IntToStr(ntohs(pTcpTab.table[i].dwRemotePort)); 233             self.StringGrid1.Cells[3,self.StringGrid1.RowCount-1]:= 234                 StateToString(pTcpTab.table[i].dwState); 235             self.StringGrid1.Cells[4,self.StringGrid1.RowCount-1]:= 236                 IntToStr(pTcpTab.table[i].dwProcessId); 237             self.StringGrid1.Cells[5,self.StringGrid1.RowCount-1]:= 238                 IdToExeFile(hSnapshot,pTcpTab.table[i].dwProcessId); 239             self.StringGrid1.Cells[6,self.StringGrid1.RowCount-1]:= 240                 IdToExeDir(pTcpTab.table[i].dwProcessId); 241             self.StringGrid1.Cells[7,self.StringGrid1.RowCount-1]:= 242                 ExeDirToFileInfo(self.StringGrid1.Cells[6,self.StringGrid1.RowCount-1]); 243             self.StringGrid1.RowCount:=self.StringGrid1.RowCount+1; 244         end; 245     end; 246     //獲取UDP連接表 247     if AllocateAndGetUdpExTableFromStack(pUdpTab,true,GetProcessHeap(),2,2)<>0 then 248     begin 249         MessageBox(self.Handle,'無法獲取UDP連接表','調用失敗',MB_ICONEXCLAMATION or MB_OK); 250     end 251     else 252     begin 253         //輸出UDP連接表 254         for i:=0 to pUdpTab.dwNumEntrIEs-1 do 255         begin 256             self.StringGrid1.Cells[0,self.StringGrid1.RowCount-1]:= 257                 'UDP'; 258             self.StringGrid1.Cells[1,self.StringGrid1.RowCount-1]:= 259                 inet_ntoa(TInAddr(pUdpTab.table[i].dwLocalAddr))+ 260                 ':'+IntToStr(ntohs(pUdpTab.table[i].dwLocalPort)); 261             self.StringGrid1.Cells[2,self.StringGrid1.RowCount-1]:= 262                 '*.*.*.*:*'; 263             self.StringGrid1.Cells[3,self.StringGrid1.RowCount-1]:= 264                 ''; 265             self.StringGrid1.Cells[4,self.StringGrid1.RowCount-1]:= 266                 IntToStr(pUdpTab.table[i].dwProcessId); 267             self.StringGrid1.Cells[5,self.StringGrid1.RowCount-1]:= 268                 IdToExeFile(hSnapshot,PUdpTab.table[i].dwProcessId); 269             self.StringGrid1.Cells[6,self.StringGrid1.RowCount-1]:= 270                 IdToExeDir(pUdpTab.table[i].dwProcessId); 271             self.StringGrid1.Cells[7,self.StringGrid1.RowCount-1]:= 272                 ExeDirToFileInfo(self.StringGrid1.Cells[6,self.StringGrid1.RowCount-1]); 273             self.StringGrid1.RowCount:=self.StringGrid1.RowCount+1; 274         end; 275     end; 276     //關閉句柄 277     CloseHandle(hSnapshot); 278     if self.StringGrid1.RowCount=2 then 279     begin 280         self.Label1.Caption:='發現 TCP/UDP 連接端口: 0 個'; 281         self.StringGrid1.Cells[0,1]:='空'; 282         self.StringGrid1.Cells[1,1]:=''; 283         self.StringGrid1.Cells[2,1]:=''; 284         self.StringGrid1.Cells[3,1]:=''; 285         self.StringGrid1.Cells[4,1]:=''; 286         self.StringGrid1.Cells[5,1]:=''; 287         self.StringGrid1.Cells[6,1]:=''; 288         self.StringGrid1.Cells[7,1]:=''; 289     end 290     else 291     begin 292         self.StringGrid1.RowCount:=self.StringGrid1.RowCount-1; 293         self.Label1.Caption:=Format('發現 TCP/UDP 連接端口: %d 個', 294             [self.StringGrid1.RowCount-1]); 295     end; 296 end; 297 procedure TForm1.Button2Click(Sender: TObject); 298 var 299     ColLen : array of Integer; 300     c,r,n : Integer; 301     f : TFileStream; 302     s : String; 303 begin 304     if (self.StringGrid1.ColCount=0or 305        (self.StringGrid1.RowCount=0then exit; 306     if not self.SaveDialog1.Execute then exit; 307     //計算每列字符長度 308     SetLength(ColLen,self.StringGrid1.ColCount); 309     for c:=0 to self.StringGrid1.ColCount-1 do 310     begin 311         ColLen[c]:=0; 312         for r:=0 to self.StringGrid1.RowCount-1 do 313         begin 314             n:=Length(self.StringGrid1.Cells[c,r]); 315             if n>ColLen[c] then ColLen[c]:=n; 316         end; 317         ColLen[c]:=ColLen[c]+1; 318     end; 319     //每列對齊後寫入文件 320     f:=TFileStream.Create(self.SaveDialog1.FileName,fmCreate); 321     for r:=0 to self.StringGrid1.RowCount-1 do 322     begin 323         for c:=0 to self.StringGrid1.ColCount-1 do 324         begin 325             s:=self.StringGrid1.Cells[c,r]; 326             n:=Length(s); 327             s:=s+DupeString(' ',ColLen[c]-n); 328             f.Write(Pointer(s)^,ColLen[c]); 329         end; 330         s:=#13#10#32; 331         f.Write(Pointer(s)^,3); 332     end; 333     MessageBox(self.Handle,PChar( 334         '連接表信息已保存到文件 '+ 335         self.SaveDialog1.FileName+ 336         ' 中。'),'保存完畢',MB_ICONINFORMATION or MB_OK); 337     SetLength(ColLen,0); 338     f.Free; 339 end; 340 procedure TForm1.Button3Click(Sender: TObject); 341 begin 342     MessageBox(self.Handle, 343         '進程連接端口查看器'+#13+#13+ 344         '作者:yeye55'+#13+#13+ 345         '本程序最早在http://www.codefans.Net/上發布', 346         '關於',MB_ICONASTERISK or MB_OK); 347 end; 348 end.
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved