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

GAVIN_JAVA616 2007-04-16 10:20:35
求详细代码,在windows界面下,能直接访问dos界面,并返回结果.达到重定向的效果~~谢谢!!
...全文
251 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
HsWong 2007-04-18
  • 打赏
  • 举报
回复
去盒子 www.2ccc.com 搜索一下Dos应该有的
GAVIN_JAVA616 2007-04-18
  • 打赏
  • 举报
回复
up
withcsharp 2007-04-18
  • 打赏
  • 举报
回复
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;
withcsharp 2007-04-18
  • 打赏
  • 举报
回复
用命名管道
俺写的
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;
ahjoe 2007-04-18
  • 打赏
  • 举报
回复
真的需要直接访问DOS界面?如果是图形界面怎么办?

如果需要接收DOS程序的标准输出,可以用管道。
有个控件,很好用的
http://www.google.cn/search?complete=1&hl=zh-CN&q=StdIORedirect&meta=
GAVIN_JAVA616 2007-04-16
  • 打赏
  • 举报
回复
大侠们,急啊!等了一天了,都没的反映的!知道的来一段代码啊~~谢谢了~~~~~

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧