델파이 7을 사용하는데요.
어플리케이션을 실행하고 종료될때까지 기다리는 함수입니다.
박지훈.임프님께서 올려주신걸 그대로 복사해서 함수로 사용할 수 있게 인수만 고친건데요.
WaitClose('aaa.exe') 이렇게 실행해보면 aaa.exe는 실행되는데
aaa.exe가 종료될때까지 기다리지 않고 바로 끝나버립니다.
제가 뭘 잘못한걸까요?
아래는 함수입니다.
Function WaitClose(vApp:String) : String;
var
start: TStartupInfo;
sec: TSecurityAttributes;
pinfo: TProcessInformation;
waitresult: Dword;
begin
FillChar(start, sizeof(STARTUPINFO), 0);
start.cb := sizeof(start);
start.wShowWindow := SW_SHOWDEFAULT;
sec.nLength := sizeof(sec);
sec.lpSecurityDescriptor := nil;
sec.bInheritHandle := true;
if CreateProcess(PAnsiChar(vApp), nil, @sec, @sec, true, 0, nil, nil, start, pinfo) <> true then
begin
Result := AnsiString('CreateProcess() failed: ') + IntToStr(GetLastError);
exit;
end;
repeat
Application.ProcessMessages;
if NowClose then exit;
waitresult := WaitForSingleObject(pinfo.hProcess, 100);
until waitresult <> WAIT_TIMEOUT;
if waitresult = WAIT_FAILED then
begin
Result := 'WaitForSingleObject() failed';
exit;
end;
CloseHandle(pinfo.hProcess);
Result := '';
end;
|