hab das ganze jetzt mal über eine pipe gelöst (nicht so wie im tip)
function TForm1.CallNetstat: string;
var
ProcessInfo: TProcessInformation;
SecAttribs: TSecurityAttributes;
StartupInfo: TStartupInfo;
hReadOut, hWriteOut: THandle;
BytesRead: dword;
Buffer: Array [1..1024] of char;
begin
Result := '';
FillChar(SecAttribs, SizeOf(TSecurityAttributes), #0);
with SecAttribs do
begin
nLength := SizeOf(TSecurityAttributes);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(hReadOut, hWriteOut, @SecAttribs, 0);
FillChar(StartupInfo, SizeOf(TStartupInfo), #0);
with StartupInfo do
begin
cb := SizeOf(TStartupInfo);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdOutput := hWriteOut;
end;
if CreateProcess(nil, pchar('cmd /c netstat -n | find ":"'),
@SecAttribs, @SecAttribs, True, NORMAL_PRIORITY_CLASS, nil, nil,
StartupInfo, ProcessInfo) then
begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
FillChar(Buffer, 1024, #0);
ReadFile(hReadOut, Buffer, 1024, BytesRead, nil);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
CloseHandle(hReadOut);
CloseHandle(hWriteOut);
Result := Buffer;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
k: integer;
tmp: string;
begin
Memo1.Lines.BeginUpdate;
Memo1.Text := CallNetstat;
for k := Memo1.Lines.Count - 1 downto 0 do
begin
if Pos(':80', Memo1.Lines[k]) = 0 then // hier den gewünschten port angeben
begin
Memo1.Lines.Delete(k);
end else begin
tmp := Trim(Memo1.Lines[k]);
Delete(tmp, 1, Pos(' ', tmp));
tmp := Trim(tmp);
Delete(tmp, 1, Pos(' ', tmp));
tmp := Trim(tmp);
Delete(tmp, Pos(':', tmp), Length(tmp));
Memo1.Lines[k] := Trim(tmp);
end;
end;
Memo1.Lines.EndUpdate;
end;