在Delphi中如何获取netstat等命令的输出,并将其保存到一个文本文件中?(100分)

  • 主题发起人 主题发起人 vc3000
  • 开始时间 开始时间
V

vc3000

Unregistered / Unconfirmed
GUEST, unregistred user!
很多Dos命令的输出都没问题,可是netstat,nbstat就不行,这是为什么?怎么做?
 
netstat >d:/temp.txt
用dos的 >转向命令试一试
 
我试了一下完全可以
 
在命令行下当然可以,但是在程序中调用不行!!!
 
因为netstat比较慢所以你必须考虑等待netstat的完成做法有两种
一种用创建Process的方法!还有一种土办法:
设置一个单元变量
var
bStop:boolean;

/////////////////////////////
处理过程
if Fileexists('D:/temp.txt') then
DeleteFile('D:/temp.txt')
WinExec('netstat >D:/temp.txt',0);
while not bStop do
begin
if FileExists('D:/temp.txt') then
break;
Application.ProcessMessages;
end;
..............

如果要停止只需要将bStop:=true就可以了,不过保险期间还是用CreateProcess因为这样
就让程序同步了!
 
还是不行!!!
 
function WinExecAndWait32(FileName:String; Visibility :integer):integer;
//调用其它程序并等待
var
zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);

StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process securityattributes }
nil, { pointer to thread securityattributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then Result := -1 { pointer to PROCESS_INF }

else begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
// GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
用这个函数调用试试
 
不行,请各位大侠试过以后再发帖子好吗?
 
function GetDosOutput(const CommandLine:string): string;
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[0..255] of Char;
BytesRead: Cardinal;
WorkDir, Line: String;
begin
Application.ProcessMessages;
with SA do
begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
// create pipe for standard output redirection
CreatePipe(StdOutPipeRead, // read handle
StdOutPipeWrite, // write handle
@SA, // security attributes
0 // number of bytes reserved for pipe - 0 default
);
try
// Make child process use StdOutPipeWrite as standard out,
// and make sure it does not show on screen.
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdinput
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;

// launch the command line compiler
WorkDir := ExtractFilePath(CommandLine);
WasOK := CreateProcess(nil, PChar(CommandLine), nil, nil, True, 0, nil,
PChar(WorkDir), SI, PI);

// Now that the handle has been inherited, close write to be safe.
// We don't want to read or write to it accidentally.
CloseHandle(StdOutPipeWrite);
// if process could be created then handle its output
if not WasOK then
raise Exception.Create('Could not execute command line!')
else
try
// get all output until dos app finishes
Line := '';
repeat
// read block of characters (might contain carriage returns and line feeds)
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);

// has anything been read?
if BytesRead > 0 then
begin
// finish buffer to PChar
Buffer[BytesRead] := #0;
// combine the buffer with the rest of the last run
Line := Line + Buffer;
end;
until not WasOK or (BytesRead = 0);
// wait for console app to finish (should be already at this point)
WaitForSingleObject(PI.hProcess, INFINITE);
finally
// Close all remaining handles
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
finally
result:=Line;
CloseHandle(StdOutPipeRead);
end;
end;
 
i try again,but it do't work.
 
生成一个批处理文件,里面是你要的命令,然后调用它,完成后如果有必要再把它DEL掉
另外,我还看到有人用WINEXEC,里面的参数是command ipconfig /all >output.txt之
类的东西,主要是前面加了个COMMAND,我现在这里没有环境,你可以试一下
 
还是不行。
 
不可能吧,我这里通过了呀,运行结束后,你看看跟你运行文件目录中的net.log
var
ts:tstringlist;
begin
ts:=tstringlist.Create;
ts.Add('netstat -a >net.log');
ts.SaveToFile('net.bat');
ts.Free;
winexec('net.bat',sw_show);
end;
 
接受答案了.
 
thx to xiao_min!
 
后退
顶部