为什么我用CreateProcess创建一个进程,执行一个dos命令(就一个dir/s,正常大约1分钟)
用 GetExitCodeProcess来获得进程结束代号,要等四五分钟才结束,
而用WaitForSingleObject(hProc, INFINITE)干脆就无限期等待了
procedure TForm1.Button2Click(Sender: TObject);
var
pi: Process_Information;
ExitCode: DWord;
si: TStartUPINFO;
Ti: integer;
begin
FillChar(si, Sizeof(si), 0);
si.cb := sizeof(si);
si.wShowWindow := SW_HIDE;
si.dwFlags := STARTF_USESHOWWINDOW;
if CreateProcess(nil, PChar('C:\BB.bat'), nil, nil, True, NORMAL_PRIORITY_CLASS,
nil, PChar('C:\'), si, pi) then
begin
WaitForSingleObject(pi.hProcess, 1000);
{repeat
GetExitCodeProcess(pi.hProcess, INFINITE);
until ExitCode <> STILL_ACTIVE;}
closeHandle(pi.hProcess);
closeHandle(pi.hThread);
end;
但是如果用下面的代码,却很快就返回
function ExcuteCmd(const CmdLine, CmdDir: string; var RetStr: string):Boolean;
var
hPipe,hRead1, hWrite1, hRead2, hWrite2: THandle;
si: TStartupInfo;
sa: SECURITY_ATTRIBUTES;
pi: Process_Information;
ExitCode: DWord;
ByteCount: Integer;
dwBytesRead,dwTotal, dwLeft: Dword;
Buf: PChar;
bSucc: Boolean;
const
BufSize = 5120;begin
Result := False;
FillChar(si, Sizeof(si), 0);
sa.nLength := sizeOf(sa);
sa.lpSecurityDescriptor := nil;
sa.bInheritHandle := True;
CreatePipe(hRead1, hWrite1, @sa, 0);
CreatePipe(hRead2, hWrite2, @sa, 0);
si.cb := sizeof(si);
si.wShowWindow := SW_HIDE;
si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
si.hStdInput := hRead1;
si.hStdOutput := hWrite2;
Buf := StrAlloc(BufSize);
if CreateProcess(nil, PChar(CmdLine), nil, nil, True, NORMAL_PRIORITY_CLASS,
nil, PChar(CmdDir), si, pi) then
begin
Result := True;
repeat
GetExitCodeProcess(pi.hProcess, ExitCode);
while True do
begin
PeekNamedPipe(hRead2, Buf, BufSize, @dwBytesRead, @dwTotal, @dwLeft);
if dwBytesRead = 0 then Break;
ZeroMemory(Buf, BufSize);
ReadFile(hRead2, Buf^, BufSize, dwBytesRead, nil);
ByteCount := ByteCount + dwBytesRead;
RetStr := RetStr + StrPas(Buf);
end;
until ExitCode <> STILL_ACTIVE;
end;
CloseHandle(hRead1);
CloseHandle(hRead2);
CloseHandle(hWrite1);
CloseHandle(hWrite2);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
StrDispose(Buf)
end;请赐教...
用 GetExitCodeProcess来获得进程结束代号,要等四五分钟才结束,
而用WaitForSingleObject(hProc, INFINITE)干脆就无限期等待了
procedure TForm1.Button2Click(Sender: TObject);
var
pi: Process_Information;
ExitCode: DWord;
si: TStartUPINFO;
Ti: integer;
begin
FillChar(si, Sizeof(si), 0);
si.cb := sizeof(si);
si.wShowWindow := SW_HIDE;
si.dwFlags := STARTF_USESHOWWINDOW;
if CreateProcess(nil, PChar('C:\BB.bat'), nil, nil, True, NORMAL_PRIORITY_CLASS,
nil, PChar('C:\'), si, pi) then
begin
WaitForSingleObject(pi.hProcess, 1000);
{repeat
GetExitCodeProcess(pi.hProcess, INFINITE);
until ExitCode <> STILL_ACTIVE;}
closeHandle(pi.hProcess);
closeHandle(pi.hThread);
end;
但是如果用下面的代码,却很快就返回
function ExcuteCmd(const CmdLine, CmdDir: string; var RetStr: string):Boolean;
var
hPipe,hRead1, hWrite1, hRead2, hWrite2: THandle;
si: TStartupInfo;
sa: SECURITY_ATTRIBUTES;
pi: Process_Information;
ExitCode: DWord;
ByteCount: Integer;
dwBytesRead,dwTotal, dwLeft: Dword;
Buf: PChar;
bSucc: Boolean;
const
BufSize = 5120;begin
Result := False;
FillChar(si, Sizeof(si), 0);
sa.nLength := sizeOf(sa);
sa.lpSecurityDescriptor := nil;
sa.bInheritHandle := True;
CreatePipe(hRead1, hWrite1, @sa, 0);
CreatePipe(hRead2, hWrite2, @sa, 0);
si.cb := sizeof(si);
si.wShowWindow := SW_HIDE;
si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
si.hStdInput := hRead1;
si.hStdOutput := hWrite2;
Buf := StrAlloc(BufSize);
if CreateProcess(nil, PChar(CmdLine), nil, nil, True, NORMAL_PRIORITY_CLASS,
nil, PChar(CmdDir), si, pi) then
begin
Result := True;
repeat
GetExitCodeProcess(pi.hProcess, ExitCode);
while True do
begin
PeekNamedPipe(hRead2, Buf, BufSize, @dwBytesRead, @dwTotal, @dwLeft);
if dwBytesRead = 0 then Break;
ZeroMemory(Buf, BufSize);
ReadFile(hRead2, Buf^, BufSize, dwBytesRead, nil);
ByteCount := ByteCount + dwBytesRead;
RetStr := RetStr + StrPas(Buf);
end;
until ExitCode <> STILL_ACTIVE;
end;
CloseHandle(hRead1);
CloseHandle(hRead2);
CloseHandle(hWrite1);
CloseHandle(hWrite2);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
StrDispose(Buf)
end;请赐教...
解决方案 »
- 下载了5,6个RXlib控件包装的时候都提示说错误,找不到什么文件~
- Cannot assign a TFont to a TFont
- 图片按比例压缩
- 问个三层的问题,在应用层ADOQuery中写SQL语句,与在客户端ClientDataSet中写SQL语有什么区别没有?
- 第一次用installshield制作安装文件,请指教
- 进度条的使用
- 不能用搜索引擎,只好麻烦大家,如何判断用Query 打开的一个数据集是空的呢??
- 关于Delphi 小小的问题。
- 如何检测网卡上网线是否连接上了,就像win2000一样,插上网线就能提示连接速度?
- 怎么关闭(释放)内存中使用的文件??
- 请教各位!.bin文件要用什么工具才能提取出来呀!~
- 急用!求 对实数进行“四舍五入”的函数!
//执行处部可执行文件
Function LaunchApp(pExePath,WorkDir:String;ShowWindow,WaitFlag:Boolean):Boolean;
var ExePath : array[0..512] of char;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
StrPCopy(exePath, pExePath);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
if ShowWindow then
StartupInfo.wShowWindow := 1
else
StartupInfo.wShowWindow := 0;
if not CreateProcess(nil,
ExePath,
nil,
nil,
false,
CREATE_NEW_CONSOLE or
NORMAL_PRIORITY_CLASS,
nil,
nil,
StartupInfo,
ProcessInfo) then begin
Result:=False;
Exit;
end else begin
if WaitFlag then
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
Result:=True;
end;
end;