절름발이 프로그래머/Delphi

델파이에서 다른 프로그램 실행시키고, 종료 확인하기

훅크선장 2009. 7. 17. 14:14
http://delphi.about.com/od/windowsshellapi/a/executeprogram.htm
를 참고하였습니다.

델파이에서 별도의 프로그램, 즉 exe를 실행시키고 종료를 확인할 수 있습니다.
ShellExecuteEx 를 사용하는데, 
먼저 uses ShellApi; 를 포함합니다.

그리고,
procedure TForm1.Button2Click(Sender: TObject);
var
   SEInfo: TShellExecuteInfo;
   ExitCode: DWORD;
   ExecuteFile, ParamString, StartInString: string;
begin
   ExecuteFile:='C:\Windows\Notepad.exe'; // 실행하려는 프로그램의 경로 및 파일명 지정
   ParamString := 'C:\autoexec.bat'; // 프로그램 실행시 인자값을 문자열로 지정

   FillChar(SEInfo, SizeOf(SEInfo), 0) ;
   SEInfo.cbSize := SizeOf(TShellExecuteInfo) ;
   with SEInfo do begin
     fMask := SEE_MASK_NOCLOSEPROCESS;
     Wnd := Application.Handle;
     lpFile := PChar(ExecuteFile) ;
     lpParameters := PChar(ParamString) ;
 // lpDirectory := PChar(StartInString) ; // StartInString 문자열에 실행되고자 하는 디렉토리를 지정할 수 있음. 지정하지 않으면 현재 프로그램 실행 디렉토리가 디폴트로 사용됨
     nShow := SW_SHOWNORMAL; // 프로그램이 실행되는 윈도우 형태를 지정할 수 있습니다. ACTIVE, 최대화, 최소화 등등...
   end;
   if ShellExecuteEx(@SEInfo) then begin
     repeat
       Application.ProcessMessages;
       GetExitCodeProcess(SEInfo.hProcess, ExitCode) ;
     until (ExitCode <> STILL_ACTIVE) or
Application.Terminated;
     ShowMessage('프로그램이 종료되었습니다.') ;
   end
   else ShowMessage('프로그램을 실행하지 못했습니다.') ;
end;
같이 코드를 작성하면 됩니다.