Code: Select all
procedure TfrmMain.Button1Click(Sender: TObject);
var
nProc : Integer;
nMod : Integer;
nHandle: Integer;
OpenHan: THandle;
DupHand: THandle;
OptVal: Array[1..10] Of Char;
OptLen: Integer;
begin
OptLen := SizeOf(OptVal);
pbProc.Max := Processes.ItemCount;
for nProc := 0 to Processes.ItemCount - 1 do
begin
pbProc.Position := nProc;
Application.ProcessMessages;
pbMod.Max := Processes.Items[ nProc ].Modules.ItemCount;
for nMod := 0 to Processes.Items[ nProc ].Modules.ItemCount - 1 do
begin
pbMod.Position := nMod;
Application.ProcessMessages;
if LowerCase( ExtractFileName( Processes.Items[ nProc ].Modules.Items[ nMod ].FileName ) ) = 'ws2_32.dll' then
begin
OpenHan := OpenProcess( PROCESS_ALL_ACCESS, False, Processes.Items[ nProc ].ID );
if OpenHan <> 0 then
begin
pbHandle.Max := Processes.Items[ nProc ].Handles.ItemCount;
for nHandle := 0 to Processes.Items[ nProc ].Handles.ItemCount - 1 do
begin
pbHandle.Position := nHandle;
Application.ProcessMessages;
if DuplicateHandle( OpenHan, Processes.Items[ nProc ].Handles.Items[ nHandle ].Handle, GetCurrentProcess, @Duphand, 0, False, DUPLICATE_SAME_ACCESS ) then
begin
Application.ProcessMessages;
if getsockopt( DupHand, IPPROTO_TCP, SO_ACCEPTCONN, PChar( @OptVal ), OptLen ) = 0 then
ShowMessage( Processes.Items[ nProc ].ExeFile );
CloseHandle( DupHand );
end;
end;
end;
CloseHandle( OpenHan );
Break;
end;
end;
end;
end;
Passes over all running process and check if they have the module "ws2_32.dll" attatched to it. If true then I Open this process using OpenProcess and I pass over all the handles, duplicating them and using "getsockopt" to check if its a valid socket handle. But, its not always, just in some process and in some handles. There are handles that accepts normaly but there are handles that my application freezes when I try to "getsockopt" in this handle. Why does this happens? Can you explain me why?