2010-01-20 100 views
2

我正在使用ShellExecute运行外部应用程序 如何判断外部应用程序何时结束?如何判断外部应用程序何时以delphi结尾

这里我的代码

theProgram  := 'MySql.exe'; 
itsParameters := ' -u user1 -ppassword -e "create database abc"’; 
rslt := ShellExecute(0, 'open', 
         pChar (theProgram), 
         pChar (itsParameters), 
         nil, 
         SW_SHOW); 

回答

10

请尝试以下功能。 WaitForSingleObject可以满足你的需求。

function ExecAppAndWait(const sApp, sParams: String; wShow: Word; sCurrentDirectory: String = ''): DWord; 
{ Parameter wShow: SW_HIDE, SW_SHOWNORMAL, SW_NORMAL, SW_MAXIMIZE ...} 
var 
    aSI  : TStartupInfo;  // Win32 : STARTUPINFO 
    aPI  : TProcessInformation; // Win32 : PROCESS_INFORMATION 
    aProc : THandle;    // Win32 
    aCurrentDirectory: PChar; 
    s: String; 
begin 
    s := sApp + ' ' + sParams; 
    FillChar(aSI, SizeOf(aSI), 0); 
    aSI.cb := SizeOf(aSI); 
    aSI.wShowWindow := wShow; 
    aSi.dwFlags := STARTF_USESHOWWINDOW; 


    if sCurrentDirectory = '' then 
    aCurrentDirectory := nil 
    else 
    aCurrentDirectory := PChar(sCurrentDirectory); 

    Win32Check(CreateProcess(nil, PChar(s), nil, nil, 
      False, Normal_Priority_Class, nil, aCurrentDirectory, aSI, aPI)); 
    // in TProcessInformation.hProcess -> Process-Handle 
    aProc := aPI.hProcess; 

    CloseHandle(aPI.hThread); 


    if WaitForSingleObject(aProc, Infinite) <> Wait_Failed then 
    GetExitCodeProcess(aProc, Result); 
    // free Ressource 
    CloseHandle(aProc); 
end; 
2

的ShellExecute是直接WinAPI的功能。要获得有关执行过程的任何信息,您需要改为使用ShellExecuteEx

+0

然后在上面的LPSHELLEXECUTEINFO.hProcess上使用WaitForSingleObject – 2010-01-20 20:18:37

相关问题