正在写一个应用程序,其中需要启动其他程序,然后检查该程序是否被关闭,如果关闭了,则启动另一个程序。
其实也就是依次的启动一系列的程序,每个程序关闭后,再启动下一个。我使用ShellExecute()来启动其他程序,查了很多的资料,检查程序是否被关闭,一般使用Handle来查的,而获得Handle一般又都用:FindWindow(类名,窗体名)。我的问题出来了,因为我所运行的程序,窗体名和程序名,都不一样,所以我根本没办法用这个去获得Handle,从而来检查是否已经被关闭。如何去获得窗体名?或者说如何去获得类名???请问大家:有什么好的方法?

解决方案 »

  1.   

    进程已经知道如何查了?但是请问有什么作用呢?一个exe文件:SAFALSHPLAY.exe
    但是在进程里面,显示成为:SAFALSHPLAY.E我没办法去匹配阿!
      

  2.   

    win32api . list current process
      

  3.   

    Dim WshShell, oExec
    below is vbscript edition ,you can modify it to delphi 
    Set WshShell = CreateObject("WScript.Shell")Set oExec = WshShell.Exec("calc")Do While oExec.Status = 0
         WScript.Sleep 100
    LoopWScript.Echo oExec.Status
      

  4.   

    blow is get window handle from hinstance 's code you can change it to delphi
    Function ProcIDFromWnd(ByVal hwnd As Long) As Long
       Dim idProc As Long
       
       ' Get PID for this HWnd
       GetWindowThreadProcessId hwnd, idProc
       
       ' Return PID
       ProcIDFromWnd = idProc
    End Function
          
    Function GetWinHandle(hInstance As Long) As Long
       Dim tempHwnd As Long
       
       ' Grab the first window handle that Windows finds:
       tempHwnd = FindWindow(vbNullString, vbNullString)
       
       ' Loop until you find a match or there are no more window handles:
       Do Until tempHwnd = 0
          ' Check if no parent for this window
          If GetParent(tempHwnd) = 0 Then
             ' Check for PID match
             If hInstance = ProcIDFromWnd(tempHwnd) Then
                ' Return found handle
                GetWinHandle = tempHwnd
                ' Exit search loop
                Exit Do
             End If
          End If
       
          ' Get the next window handle
          tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT)
       Loop
    End Function 
      

  5.   

    以前在论坛上看到的:
    use  ShellApi
    /////////////////////
    function WaitExeRun(FileName,Param:string):Boolean;//文件名和参数
    var
      SHExecInfo: SHELLEXECUTEINFO;
      HasClose:Boolean;
    begin
      HasClose:=False;
      try
        SHExecInfo.cbSize := sizeof(SHELLEXECUTEINFO);
        SHExecInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
        SHExecInfo.Wnd := 0;
        SHExecInfo.lpVerb := nil;
        SHExecInfo.lpFile := PChar(FileName);
        if Param='' then SHExecInfo.lpParameters :=nil
        else SHExecInfo.lpParameters :=PChar(Param);
        SHExecInfo.lpDirectory := nil;
        SHExecInfo.nShow := SW_SHOW;
        SHExecInfo.hInstApp := 0;
        ShellExecuteEx(@SHExecInfo);
        WaitForSingleObject(SHExecInfo.hProcess, INFINITE);
        CloseHandle(SHExecInfo.hProcess);
        HasClose:=True;
      finally
        Result:=HasClose;
      end;
    end;
    ///////////////////////////
    使用方法:
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      if  WaitExeRun('notepad.exe','') then
        ShowMessage('Closed');
    end;
      

  6.   

    可以用 
    判断A是否关闭A 启动时CreateMutext 创建一个 公用的。。
    A 终止时 ReleaseMutex。。其他进程 比如B启动时 就OpenMutext...这个 如果失败表示A已关闭 否则还在运行方法比较多种哦。
      

  7.   

    同意 jian23cn(黑翼天使),用WaitForSingleObject
      

  8.   

    我刚才试用了waitexerun函数方式,发觉她一旦运行其他程序后,主程序就进入了等待状态,这个状态表现像死机一样。不知道这种状态可否变化?比如不想出现像死机一样?