正在写一个应用程序,其中需要启动其他程序,然后检查该程序是否被关闭,如果关闭了,则启动另一个程序。
其实也就是依次的启动一系列的程序,每个程序关闭后,再启动下一个。我使用ShellExecute()来启动其他程序,查了很多的资料,检查程序是否被关闭,一般使用Handle来查的,而获得Handle一般又都用:FindWindow(类名,窗体名)。我的问题出来了,因为我所运行的程序,窗体名和程序名,都不一样,所以我根本没办法用这个去获得Handle,从而来检查是否已经被关闭。如何去获得窗体名?或者说如何去获得类名???请问大家:有什么好的方法?
其实也就是依次的启动一系列的程序,每个程序关闭后,再启动下一个。我使用ShellExecute()来启动其他程序,查了很多的资料,检查程序是否被关闭,一般使用Handle来查的,而获得Handle一般又都用:FindWindow(类名,窗体名)。我的问题出来了,因为我所运行的程序,窗体名和程序名,都不一样,所以我根本没办法用这个去获得Handle,从而来检查是否已经被关闭。如何去获得窗体名?或者说如何去获得类名???请问大家:有什么好的方法?
解决方案 »
- GLScene使用
- Activex如何封装TListview控件
- 关于树与数据库的问题,在线急等。
- 如何获取另一个exe程序的窗口上的label的caption值
- 关于应用程序嵌入NETMEETING控件的问题
- 模拟directinput的问题
- NMFTP控件使用问题
- 请大家分析做WEB服务器,用D6跟.Net C#,那个更快更方便
- 重大事件(和愚人节有关)!!!!!!
- 请问我这条语句哪里错了啊?ComboBox1.AddItem(ADOQueryLogin1.FieldValues['姓名']);
- 关于数据拷贝问题?分不够开帖再加!!
- 奇怪!! 关联文件后,双击打开关联的文件,在exe中居然参数出错,路径中的空格居然把文件路径分成两个参数?? 在线等
但是在进程里面,显示成为:SAFALSHPLAY.E我没办法去匹配阿!
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
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
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;
判断A是否关闭A 启动时CreateMutext 创建一个 公用的。。
A 终止时 ReleaseMutex。。其他进程 比如B启动时 就OpenMutext...这个 如果失败表示A已关闭 否则还在运行方法比较多种哦。