2016-04-26 3 views
0

Wenn ich ShellExecute aus meiner Delphi-Anwendung,ShellExecute, Wie kann ich meinem Projekt mitteilen, dass die Anwendung geschlossen ist?

wie kann ich feststellen, ob das Programm, das ich aufgerufen habe, getan wird, so dass ich zu meiner App zurückkehren und andere Sachen nach dem anderen Programm getan werden kann.

Zum Beispiel, öffnen Sie den Editor, nachdem fertig und schließen Sie es, zeigen Sie Nachricht in meiner Anwendung "Fertig!"

+0

Sie verwenden die falsche Funktion. Verwenden Sie CreateProcess und warten Sie auf die Prozesskennung. –

Antwort

0

Sie müssen ShellExecuteEx mit SEE_MASK_NOCLOSEPROCESS verwenden. Überprüfen Sie dies und sehen Sie meine Kommentare inline:

var 
    sei: TShellExecuteInfo; 
    exitCode: Cardinal; 
begin 
    ZeroMemory(@sei, SizeOf(sei)); 
    with sei do 
    begin 
     cbSize := SizeOf(sei); 
     fMask := SEE_MASK_NOCLOSEPROCESS; // Tell ShellExecuteEx to keep the process handle open 
     Wnd := WindowHandleIfNeeded; // Can be omitted 
     lpVerb := 'open'; 
     lpFile := PChar(PathOfExeToRun); 
     lpParameters := PChar(ParametersToUse); 
     lpDirectory := PChar(WorkingDirectoryToUse); // Can be omitted 
     nShow := SW_NORMAL; // Can be omitted 
    end; 

    if ShellExecuteEx(@sei) then 
    begin 
     // I have encapsulated the different ways in begin/end and commented. 

     // *** EITHER: Wait for the child process to close, without processing messages (if you do it in a background thread) 
     begin 
      WaitForSingleObject(sei.hProcess, INFINITE); 
     end; 

     // *** OR: Wait for the child process to close, while processing messages (if you do it in the UI thread) 
     begin 
      while MsgWaitForMultipleObjects(1, sei.hProcess, FALSE, INFINITE, QS_ALLINPUT) = (WAIT_OBJECT_0 + 1) do begin 
       Application.ProcessMessages 
      end; 
     end; 

     // *** OR: Do something else, and in the middle you can check whether the child is still running using this: 
     begin 
      GetExitCodeProcess(sei.hProcess, exitCode); 
      if exitCode == STILL_ACTIVE then begin 
       // It's still running! 
      end else begin 
       // It has finished! 
      end; 
     end; 

     // At the end, close the handle 
     CloseHandle(sei.hProcess); 
    end; 
end; 
Verwandte Themen