Как средствами Delphi узнать имя процесса занявшее 80 порт.
![]() |
Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
|
| ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
| [216.73.217.172] |
|
|
Соблюдайте общие правила форума
MSDN Library
FAQ раздела
Поиск по разделу
Как правильно задавать вопросы
Как средствами Delphi узнать имя процесса занявшее 80 порт.
|
|
|
|
|
Как средствами Delphi узнать имя процесса занявшее 80 порт? приложение пишу под Windows 7 и выше (разрядность любая). спасибо
|
|
Сообщ.
#3
,
|
|
|
|
![]() ![]() unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,winsock, IdContext, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer, Sockets, ScktComp; type TForm1 = class(TForm) Memo1: TMemo; ServerSocket1: TServerSocket; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; const MIB_TCP_STATE_CLOSED = 1; //The TCP connection is in the CLOSED state that represents no connection state at all. MIB_TCP_STATE_LISTEN = 2; //The TCP connection is in the LISTEN state waiting for a connection request from any remote TCP and port. MIB_TCP_STATE_SYN_SENT = 3; //The TCP connection is in the SYN-SENT state waiting for a matching connection request after having sent a connection request (SYN packet). MIB_TCP_STATE_SYN_RCVD = 4; //The TCP connection is in the SYN-RECEIVED state waiting for a confirming connection request acknowledgment after having both received and sent a connection request (SYN packet). MIB_TCP_STATE_ESTAB = 5; //The TCP connection is in the ESTABLISHED state that represents an open connection, data received can be delivered to the user. This is the normal state for the data transfer phase of the TCP connection. MIB_TCP_STATE_FIN_WAIT1 = 6; //The TCP connection is FIN-WAIT-1 state waiting for a connection termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent. MIB_TCP_STATE_FIN_WAIT2 = 7; //The TCP connection is FIN-WAIT-1 state waiting for a connection termination request from the remote TCP. MIB_TCP_STATE_CLOSE_WAIT = 8; //The TCP connection is in the CLOSE-WAIT state waiting for a connection termination request from the local user. MIB_TCP_STATE_CLOSING = 9; //The TCP connection is in the CLOSING state waiting for a connection termination request acknowledgment from the remote TCP. MIB_TCP_STATE_LAST_ACK = 10; //The TCP connection is in the LAST-ACK state waiting for an acknowledgment of the connection termination request previously sent to the remote TCP (which includes an acknowledgment of its connection termination request). MIB_TCP_STATE_TIME_WAIT = 11; //The TCP connection is in the TIME-WAIT state waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request. MIB_TCP_STATE_DELETE_TCB = 12; type PTCP_TABLE_CLASS = ^TCP_TABLE_CLASS; TCP_TABLE_CLASS = (TCP_TABLE_BASIC_LISTENER, TCP_TABLE_BASIC_CONNECTIONS, TCP_TABLE_BASIC_ALL, TCP_TABLE_OWNER_PID_LISTENER, TCP_TABLE_OWNER_PID_CONNECTIONS, TCP_TABLE_OWNER_PID_ALL, TCP_TABLE_OWNER_MODULE_LISTENER, TCP_TABLE_OWNER_MODULE_CONNECTIONS, TCP_TABLE_OWNER_MODULE_ALL); PMIB_TCPROW = ^_MIB_TCPROW; _MIB_TCPROW = record dwState: DWORD; dwLocalAddr: DWORD; dwLocalPort: DWORD; dwRemoteAddr: DWORD; dwRemotePort: DWORD; end; PMIB_TCPTABLE = ^_MIB_TCPTABLE; _MIB_TCPTABLE = record dwNumEntries: DWORD; Table: array[0..0] of _MIB_TCPROW; end; type PVOID = Pointer; function GetExtendedTcpTable(pTcpTable: PVOID; pdwSize: PDWORD; bOrder: BOOL; ulAf: ULONG; TableClass: TCP_TABLE_CLASS; Reserved: ULONG): DWORD; stdcall; external 'Iphlpapi.dll'; var Form1: TForm1; implementation {$R *.dfm} function StateToStateStr(State: DWORD): string; begin Result := ''; case State of MIB_TCP_STATE_CLOSED: Result := 'MIB_TCP_STATE_CLOSED'; MIB_TCP_STATE_LISTEN: Result := 'MIB_TCP_STATE_LISTEN'; MIB_TCP_STATE_SYN_SENT: Result := 'MIB_TCP_STATE_SYN_SENT'; MIB_TCP_STATE_SYN_RCVD: Result := 'MIB_TCP_STATE_SYN_RCVD'; MIB_TCP_STATE_ESTAB: Result := 'MIB_TCP_STATE_ESTAB'; MIB_TCP_STATE_FIN_WAIT1: Result := 'MIB_TCP_STATE_FIN_WAIT1'; MIB_TCP_STATE_FIN_WAIT2: Result := 'MIB_TCP_STATE_FIN_WAIT2'; MIB_TCP_STATE_CLOSE_WAIT: Result := 'MIB_TCP_STATE_CLOSE_WAIT'; MIB_TCP_STATE_CLOSING: Result := 'MIB_TCP_STATE_CLOSING'; MIB_TCP_STATE_LAST_ACK: Result := 'MIB_TCP_STATE_LAST_ACK'; MIB_TCP_STATE_TIME_WAIT: Result := 'MIB_TCP_STATE_TIME_WAIT'; MIB_TCP_STATE_DELETE_TCB: Result := 'MIB_TCP_STATE_DELETE_TCB'; end; end; procedure TForm1.FormCreate(Sender: TObject); var pTcpTable: PMIB_TCPTABLE; MIB_TCPROW: _MIB_TCPROW; dwSize: DWORD; Res: DWORD; i: Integer; AF_INET:Cardinal; var ProcessID, ThreadID: Cardinal; begin ///Program Files/Borland/Delphi7/Bin/dclsockets70.bpl ThreadID := GetWindowThreadProcessId(Application.Handle, ProcessID); form1.Memo1.Lines.Add (inttostR(ThreadID)); form1.Memo1.Lines.Add (''); pTcpTable := nil; dwSize := 0; AF_INET:=0; Res := GetExtendedTcpTable(pTcpTable, @dwSize, False, MIB_TCP_STATE_LISTEN, TCP_TABLE_OWNER_PID_CONNECTIONS, 0); if Res = ERROR_INVALID_PARAMETER then raise Exception.Create('ERROR_INVALID_PARAMETER'); if Res = ERROR_INSUFFICIENT_BUFFER then begin pTcpTable := GetMemory(dwSize); ZeroMemory(pTcpTable, dwSize); try if GetExtendedTcpTable(pTcpTable, @dwSize, False, MIB_TCP_STATE_LISTEN, TCP_TABLE_OWNER_PID_CONNECTIONS, 0) = NO_ERROR then begin for i := 0 to pTcpTable^.dwNumEntries - 1 do begin MIB_TCPROW := pTcpTable^.Table[i]; form1.Memo1.Lines.Add(StateToStateStr(MIB_TCPROW.dwState) + ' State = ' + IntToStr(MIB_TCPROW.dwState) + ' LocalAddr = ' + inet_ntoa(in_addr(MIB_TCPROW.dwLocalAddr)) + ' LocalPort = ' + IntToStr(ntohs(MIB_TCPROW.dwLocalPort)) + ' RemoteAddr = ' + inet_ntoa(in_addr(MIB_TCPROW.dwRemoteAddr)) + ' RemotePort = ' + IntToStr(ntohs(MIB_TCPROW.dwRemotePort)) ); end; end; finally FreeMemory(pTcpTable); end end; end; end. Добавлено я понимаю как то по PID мне надо найти по списку. но что то не так делаю. в самом приложении открываю TCP порт 77777 помогите добить получаю: ![]() ![]() Memo1 9300 MIB_TCP_STATE_ESTAB State = 5 LocalAddr = 10.41.64.131 LocalPort = 445 RemoteAddr = 10.41.64.21 RemotePort = 1331 MIB_TCP_STATE_SYN_RCVD State = 4 LocalAddr = 5.0.0.0 LocalPort = 32512 RemoteAddr = 4.86.0.0 RemotePort = 32512 State = 37828 LocalAddr = 212.5.0.0 LocalPort = 1280 RemoteAddr = 127.0.0.1 RemotePort = 1110 State = 16777343 LocalAddr = 196.154.0.0 LocalPort = 54277 RemoteAddr = 5.0.0.0 RemotePort = 32512 State = 22020 LocalAddr = 127.0.0.1 LocalPort = 50338 RemoteAddr = 212.5.0.0 RemotePort = 1280 State = 16777343 LocalAddr = 4.86.0.0 LocalPort = 32512 RemoteAddr = 196.164.0.0 RemotePort = 54277 MIB_TCP_STATE_TIME_WAIT State = 11 LocalAddr = 127.0.0.1 LocalPort = 1110 RemoteAddr = 127.0.0.1 RemotePort = 53743 State = 0 LocalAddr = 11.0.0.0 LocalPort = 32512 RemoteAddr = 4.86.0.0 RemotePort = 32512 State = 33490 LocalAddr = 0.0.0.0 LocalPort = 2816 RemoteAddr = 127.0.0.1 RemotePort = 1110 State = 16777343 LocalAddr = 210.153.0.0 LocalPort = 0 RemoteAddr = 5.0.0.0 RemotePort = 32512 State = 22020 LocalAddr = 127.0.0.1 LocalPort = 53915 RemoteAddr = 212.5.0.0 RemotePort = 2816 State = 16777343 LocalAddr = 4.86.0.0 LocalPort = 32512 RemoteAddr = 210.181.0.0 RemotePort = 0 MIB_TCP_STATE_TIME_WAIT State = 11 LocalAddr = 127.0.0.1 LocalPort = 1110 RemoteAddr = 127.0.0.1 RemotePort = 53945 State = 0 LocalAddr = 11.0.0.0 LocalPort = 32512 RemoteAddr = 4.86.0.0 RemotePort = 32512 State = 48082 LocalAddr = 0.0.0.0 LocalPort = 2816 RemoteAddr = 127.0.0.1 RemotePort = 1110 State = 16777343 LocalAddr = 210.189.0.0 LocalPort = 0 RemoteAddr = 5.0.0.0 RemotePort = 32512 State = 22020 LocalAddr = 127.0.0.1 LocalPort = 53963 RemoteAddr = 212.5.0.0 RemotePort = 1280 State = 16777343 LocalAddr = 4.86.0.0 LocalPort = 32512 RemoteAddr = 210.208.0.0 RemotePort = 54277 MIB_TCP_STATE_TIME_WAIT State = 11 LocalAddr = 127.0.0.1 LocalPort = 1110 RemoteAddr = 127.0.0.1 RemotePort = 53977 State = 0 LocalAddr = 5.0.0.0 LocalPort = 32512 RemoteAddr = 4.86.0.0 RemotePort = 32512 |
|
Сообщ.
#4
,
|
|
|
|
Хм, вообще удивительно, что ты какой то валидный ответ от функции получаешь. Ты ведь ей передаешь TCP_TABLE_OWNER_PID_CONNECTIONS, значит получаешь ты таблицу вот этих структур.
|
|
Сообщ.
#5
,
|
|
|
|
ура, спасибо за подсказку, получилось.
![]() ![]() unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,winsock, IdContext, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer, Sockets, ScktComp; type TForm1 = class(TForm) Memo1: TMemo; ServerSocket1: TServerSocket; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; const MIB_TCP_STATE_CLOSED = 1; //The TCP connection is in the CLOSED state that represents no connection state at all. MIB_TCP_STATE_LISTEN = 2; //The TCP connection is in the LISTEN state waiting for a connection request from any remote TCP and port. MIB_TCP_STATE_SYN_SENT = 3; //The TCP connection is in the SYN-SENT state waiting for a matching connection request after having sent a connection request (SYN packet). MIB_TCP_STATE_SYN_RCVD = 4; //The TCP connection is in the SYN-RECEIVED state waiting for a confirming connection request acknowledgment after having both received and sent a connection request (SYN packet). MIB_TCP_STATE_ESTAB = 5; //The TCP connection is in the ESTABLISHED state that represents an open connection, data received can be delivered to the user. This is the normal state for the data transfer phase of the TCP connection. MIB_TCP_STATE_FIN_WAIT1 = 6; //The TCP connection is FIN-WAIT-1 state waiting for a connection termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent. MIB_TCP_STATE_FIN_WAIT2 = 7; //The TCP connection is FIN-WAIT-1 state waiting for a connection termination request from the remote TCP. MIB_TCP_STATE_CLOSE_WAIT = 8; //The TCP connection is in the CLOSE-WAIT state waiting for a connection termination request from the local user. MIB_TCP_STATE_CLOSING = 9; //The TCP connection is in the CLOSING state waiting for a connection termination request acknowledgment from the remote TCP. MIB_TCP_STATE_LAST_ACK = 10; //The TCP connection is in the LAST-ACK state waiting for an acknowledgment of the connection termination request previously sent to the remote TCP (which includes an acknowledgment of its connection termination request). MIB_TCP_STATE_TIME_WAIT = 11; //The TCP connection is in the TIME-WAIT state waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request. MIB_TCP_STATE_DELETE_TCB = 12; type PTCP_TABLE_CLASS = ^TCP_TABLE_CLASS; TCP_TABLE_CLASS = (TCP_TABLE_BASIC_LISTENER, TCP_TABLE_BASIC_CONNECTIONS, TCP_TABLE_BASIC_ALL, TCP_TABLE_OWNER_PID_LISTENER, TCP_TABLE_OWNER_PID_CONNECTIONS, TCP_TABLE_OWNER_PID_ALL, TCP_TABLE_OWNER_MODULE_LISTENER, TCP_TABLE_OWNER_MODULE_CONNECTIONS, TCP_TABLE_OWNER_MODULE_ALL); PMIB_TCPROW = ^_MIB_TCPROW; _MIB_TCPROW = record dwState: DWORD; dwLocalAddr: DWORD; dwLocalPort: DWORD; dwRemoteAddr: DWORD; dwRemotePort: DWORD; dwOwningPid: DWORD; end; PMIB_TCPTABLE = ^_MIB_TCPTABLE; _MIB_TCPTABLE = record dwNumEntries: DWORD; Table: array[0..0] of _MIB_TCPROW; end; type PVOID = Pointer; function GetExtendedTcpTable(pTcpTable: PVOID; pdwSize: PDWORD; bOrder: BOOL; ulAf: ULONG; TableClass: TCP_TABLE_CLASS; Reserved: ULONG): DWORD; stdcall; external 'Iphlpapi.dll'; var Form1: TForm1; implementation {$R *.dfm} function StateToStateStr(State: DWORD): string; begin Result := ''; case State of MIB_TCP_STATE_CLOSED: Result := 'MIB_TCP_STATE_CLOSED'; MIB_TCP_STATE_LISTEN: Result := 'MIB_TCP_STATE_LISTEN'; MIB_TCP_STATE_SYN_SENT: Result := 'MIB_TCP_STATE_SYN_SENT'; MIB_TCP_STATE_SYN_RCVD: Result := 'MIB_TCP_STATE_SYN_RCVD'; MIB_TCP_STATE_ESTAB: Result := 'MIB_TCP_STATE_ESTAB'; MIB_TCP_STATE_FIN_WAIT1: Result := 'MIB_TCP_STATE_FIN_WAIT1'; MIB_TCP_STATE_FIN_WAIT2: Result := 'MIB_TCP_STATE_FIN_WAIT2'; MIB_TCP_STATE_CLOSE_WAIT: Result := 'MIB_TCP_STATE_CLOSE_WAIT'; MIB_TCP_STATE_CLOSING: Result := 'MIB_TCP_STATE_CLOSING'; MIB_TCP_STATE_LAST_ACK: Result := 'MIB_TCP_STATE_LAST_ACK'; MIB_TCP_STATE_TIME_WAIT: Result := 'MIB_TCP_STATE_TIME_WAIT'; MIB_TCP_STATE_DELETE_TCB: Result := 'MIB_TCP_STATE_DELETE_TCB'; end; end; procedure TForm1.FormCreate(Sender: TObject); var pTcpTable: PMIB_TCPTABLE; MIB_TCPROW: _MIB_TCPROW; dwSize: DWORD; Res: DWORD; i: Integer; AF_INET:Cardinal; var ProcessID, ThreadID: Cardinal; begin ///Program Files/Borland/Delphi7/Bin/dclsockets70.bpl ProcessID:=0; ThreadID := GetWindowThreadProcessId(Application.Handle, ProcessID); form1.Memo1.Lines.Add (inttostR(ProcessID)); form1.Memo1.Lines.Add (''); { MIB_TCP_STATE_CLOSED = 1; //The TCP connection is in the CLOSED state that represents no connection state at all. MIB_TCP_STATE_LISTEN = 2; //The TCP connection is in the LISTEN state waiting for a connection request from any remote TCP and port. MIB_TCP_STATE_SYN_SENT = 3; //The TCP connection is in the SYN-SENT state waiting for a matching connection request after having sent a connection request (SYN packet). MIB_TCP_STATE_SYN_RCVD = 4; //The TCP connection is in the SYN-RECEIVED state waiting for a confirming connection request acknowledgment after having both received and sent a connection request (SYN packet). MIB_TCP_STATE_ESTAB = 5; //The TCP connection is in the ESTABLISHED state that represents an open connection, data received can be delivered to the user. This is the normal state for the data transfer phase of the TCP connection. MIB_TCP_STATE_FIN_WAIT1 = 6; //The TCP connection is FIN-WAIT-1 state waiting for a connection termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent. MIB_TCP_STATE_FIN_WAIT2 = 7; //The TCP connection is FIN-WAIT-1 state waiting for a connection termination request from the remote TCP. MIB_TCP_STATE_CLOSE_WAIT = 8; //The TCP connection is in the CLOSE-WAIT state waiting for a connection termination request from the local user. MIB_TCP_STATE_CLOSING = 9; //The TCP connection is in the CLOSING state waiting for a connection termination request acknowledgment from the remote TCP. MIB_TCP_STATE_LAST_ACK = 10; //The TCP connection is in the LAST-ACK state waiting for an acknowledgment of the connection termination request previously sent to the remote TCP (which includes an acknowledgment of its connection termination request). MIB_TCP_STATE_TIME_WAIT = 11; //The TCP connection is in the TIME-WAIT state waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request. MIB_TCP_STATE_DELETE_TCB = 12; PTCP_TABLE_CLASS = ^TCP_TABLE_CLASS; TCP_TABLE_CLASS = (TCP_TABLE_BASIC_LISTENER, TCP_TABLE_BASIC_CONNECTIONS, TCP_TABLE_BASIC_ALL, TCP_TABLE_OWNER_PID_LISTENER, TCP_TABLE_OWNER_PID_CONNECTIONS, TCP_TABLE_OWNER_PID_ALL, TCP_TABLE_OWNER_MODULE_LISTENER, TCP_TABLE_OWNER_MODULE_CONNECTIONS, TCP_TABLE_OWNER_MODULE_ALL); } // pTcpTable := nil; dwSize := 0; AF_INET:=0; Res := GetExtendedTcpTable(pTcpTable, @dwSize, False, MIB_TCP_STATE_LISTEN, TCP_TABLE_OWNER_PID_LISTENER, 0); if Res = ERROR_INVALID_PARAMETER then raise Exception.Create('ERROR_INVALID_PARAMETER'); if Res = ERROR_INSUFFICIENT_BUFFER then begin pTcpTable := GetMemory(dwSize); ZeroMemory(pTcpTable, dwSize); try if GetExtendedTcpTable(pTcpTable, @dwSize, False, MIB_TCP_STATE_LISTEN, TCP_TABLE_OWNER_PID_LISTENER, 0) = NO_ERROR then begin for i := 0 to pTcpTable^.dwNumEntries - 1 do begin MIB_TCPROW := pTcpTable^.Table[i]; form1.Memo1.Lines.Add(StateToStateStr(MIB_TCPROW.dwState) + ' State = ' + IntToStr(MIB_TCPROW.dwState) + ' LocalAddr = ' + inet_ntoa(in_addr(MIB_TCPROW.dwLocalAddr)) + ' LocalPort = ' + IntToStr(ntohs(MIB_TCPROW.dwLocalPort)) + ' RemoteAddr = ' + inet_ntoa(in_addr(MIB_TCPROW.dwRemoteAddr)) + ' RemotePort = ' + IntToStr(ntohs(MIB_TCPROW.dwRemotePort)) + ' OwningPid = ' + IntToStr(MIB_TCPROW.dwOwningPid) ); end; end; finally FreeMemory(pTcpTable); end end; end; end. |