首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

在windows界面下访问dos,并返回结果?()

2012-03-14 
在windows界面下访问dos,并返回结果?(在线等)求详细代码,在windows界面下,能直接访问dos界面,并返回结果.

在windows界面下访问dos,并返回结果?(在线等)
求详细代码,在windows界面下,能直接访问dos界面,并返回结果.达到重定向的效果~~谢谢!!

[解决办法]
用命名管道
俺写的
procedure RunDosCommand(Command: string; Output: Tstringlist; TimeOut: integer);
var
hReadPipe: THandle;
hWritePipe: THandle;
SI: TStartUpInfo;
PI: TProcessInformation;
SA: TSecurityAttributes;
// SD : TSecurityDescriptor;
BytesRead: DWORD;
Dest: array[0..1023] of char;
CmdLine: array[0..512] of char;
TmpList: TStringList;
Avail, ExitCode, wrResult: DWORD;
osVer: TOSVERSIONINFO;
tmpstr: AnsiString;
all, sEnd: string;
TC: integer;
begin

begin
// InitializeSecurityDescriptor(@SD, SECURITY_DESCRIPTOR_REVISION);
// SetSecurityDescriptorDacl(@SD, True, nil, False);
SA.nLength := SizeOf(SA);
SA.lpSecurityDescriptor := nil; //@SD;
SA.bInheritHandle := True;
CreatePipe(hReadPipe, hWritePipe, @SA, 0);
end;

try

FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(TStartUpInfo);
SI.wShowWindow := SW_HIDE;
SI.dwFlags := STARTF_USESHOWWINDOW;
SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;
SI.hStdOutput := hWritePipe;
SI.hStdError := hWritePipe;
StrPCopy(CmdLine, 'CMD.EXE /c ' + Command);

TC := 0;
if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
begin
ExitCode := 0;

while ExitCode = 0 do
begin

wrResult := WaitForSingleObject(PI.hProcess, 500);
inc(Tc, 500);

BytesRead := 1024;

while BytesRead > 0 do
if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail, nil, nil) then
begin
if Avail > 0 then
begin

try
FillChar(Dest, SizeOf(Dest), 0);
ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);
TmpStr := Copy(Dest, 0, BytesRead);
all := all + TmpStr;

finally

end;

end
else
Break;
end
else
Break;


if wrResult <> WAIT_TIMEOUT then
begin
ExitCode := 1;
// Output.Add( '&Ocirc;&Euml;&ETH;&ETH;&Iacute;ê&sup3;&Eacute;&pound;&iexcl; ')
end

else
begin
if TC > TimeOut then
if TerminateProcess(PI.hProcess, 0) then
begin
sEnd := '&Ocirc;&Euml;&ETH;&ETH;&sup3;&not;&Ecirc;±&pound;&not;&Ccedil;&iquest;&Ouml;&AElig;&frac12;á&Ecirc;&oslash;&pound;&iexcl; '
end
else
begin
sEnd := '&Ocirc;&Euml;&ETH;&ETH;&sup3;&not;&Ecirc;±&pound;&not;&Ccedil;&iquest;&Ouml;&AElig;&frac12;á&Ecirc;&oslash;&Ecirc;§°&Uuml;&pound;&iexcl; '
end;

end;
end;


GetExitCodeProcess(PI.hProcess, ExitCode);
CloseHandle(PI.hProcess);


CloseHandle(PI.hThread);
end
else
Output.Add( '&Ocirc;&Euml;&ETH;&ETH;&sup2;&raquo;&Auml;&Uuml;&frac12;&oslash;&ETH;&ETH;&pound;&iexcl; ');

Output.Text := all;
if sEnd <> ' ' then
Output.Add(sEnd);
all := ' ';
tmpstr := ' ';
sEnd := ' ';
finally
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
end;
end;
[解决办法]
procedure RunDosCommand(Command: string; Output: Tstringlist; TimeOut: integer);
var
hReadPipe: THandle;
hWritePipe: THandle;
SI: TStartUpInfo;
PI: TProcessInformation;
SA: TSecurityAttributes;
// SD : TSecurityDescriptor;
BytesRead: DWORD;
Dest: array[0..1023] of char;
CmdLine: array[0..512] of char;
TmpList: TStringList;
Avail, ExitCode, wrResult: DWORD;
osVer: TOSVERSIONINFO;
tmpstr: AnsiString;
all, sEnd: string;
TC: integer;
begin

begin
// InitializeSecurityDescriptor(@SD, SECURITY_DESCRIPTOR_REVISION);
// SetSecurityDescriptorDacl(@SD, True, nil, False);
SA.nLength := SizeOf(SA);
SA.lpSecurityDescriptor := nil; //@SD;
SA.bInheritHandle := True;
CreatePipe(hReadPipe, hWritePipe, @SA, 0);
end;

try

FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(TStartUpInfo);
SI.wShowWindow := SW_HIDE;
SI.dwFlags := STARTF_USESHOWWINDOW;
SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;
SI.hStdOutput := hWritePipe;
SI.hStdError := hWritePipe;
StrPCopy(CmdLine, 'CMD.EXE /c ' + Command);

TC := 0;
if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
begin
ExitCode := 0;

while ExitCode = 0 do
begin

wrResult := WaitForSingleObject(PI.hProcess, 500);
inc(Tc, 500);

BytesRead := 1024;

while BytesRead > 0 do
if PeekNamedPipe(hReadPipe, @Dest[0], 1024, @Avail, nil, nil) then
begin
if Avail > 0 then
begin

try
FillChar(Dest, SizeOf(Dest), 0);
ReadFile(hReadPipe, Dest[0], Avail, BytesRead, nil);
TmpStr := Copy(Dest, 0, BytesRead);
all := all + TmpStr;

finally

end;

end
else
Break;
end
else
Break;


if wrResult <> WAIT_TIMEOUT then
begin
ExitCode := 1;
// Output.Add( '运行完成! ')
end

else
begin
if TC > TimeOut then
if TerminateProcess(PI.hProcess, 0) then
begin
sEnd := '运行超时,强制结束! '
end
else
begin
sEnd := '运行超时,强制结束失败! '
end;

end;
end;


GetExitCodeProcess(PI.hProcess, ExitCode);
CloseHandle(PI.hProcess);


CloseHandle(PI.hThread);
end
else
Output.Add( '运行不能进行! ');

Output.Text := all;
if sEnd <> ' ' then
Output.Add(sEnd);
all := ' ';
tmpstr := ' ';
sEnd := ' ';
finally
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
end;
end;

热点排行