需要启动任意EXE,获取这个EXE运行后窗口的标题,并结束这个程序。
{{{{{{{{{{
查到VB的一个贴子问题类似:
问:
我的意思是用Shell打开一个exe程序,
myExe = Shell(File1.Path & "\ " & File1.FileName, vbNormalFocus)
能不能根据返回的程序ID(myExe)获得那个窗口的句柄?或者当前打开窗口的句柄。
==========================
答:myexe得到的是进程pid,即使再用openprocess,得到的也只是进程句柄,根窗口句柄是两会事
其实当你用myExe = Shell(File1.Path & "\ " & File1.FileName, vbNormalFocus)这种方式运行一个exe的时候,一般来说,等一会儿那个exe就会得到焦点了,那么你可以先用doevents等待一下,然后用GetForegroundWindow来得到它的句柄下面是我简单写的一个检测ForegroundWindow改变的小程序,你参考一下:
}}}}}}}}}}}}}}
我觉得这个方法太不好用了,等启动的EXE获取焦点,这个比较不保险啊。还是请DELPHI版的高手做答,谢谢!
{{{{{{{{{{
查到VB的一个贴子问题类似:
问:
我的意思是用Shell打开一个exe程序,
myExe = Shell(File1.Path & "\ " & File1.FileName, vbNormalFocus)
能不能根据返回的程序ID(myExe)获得那个窗口的句柄?或者当前打开窗口的句柄。
==========================
答:myexe得到的是进程pid,即使再用openprocess,得到的也只是进程句柄,根窗口句柄是两会事
其实当你用myExe = Shell(File1.Path & "\ " & File1.FileName, vbNormalFocus)这种方式运行一个exe的时候,一般来说,等一会儿那个exe就会得到焦点了,那么你可以先用doevents等待一下,然后用GetForegroundWindow来得到它的句柄下面是我简单写的一个检测ForegroundWindow改变的小程序,你参考一下:
}}}}}}}}}}}}}}
我觉得这个方法太不好用了,等启动的EXE获取焦点,这个比较不保险啊。还是请DELPHI版的高手做答,谢谢!
PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.cb=sizeof(si);
si.wShowWindow=SW_SHOW;
si.dwFlags=STARTF_USESHOWWINDOCreateProcess(TEXT("c:\\****.exe"),NULL,NULL,FALSE,NULL,NULL,NULL,NULL,&si,&pi);
::TerminateProcess(pi.hProcess, INFINITE);进程信息都在Pi, 然后用EM 遍历窗口,判断进程ID 保存标题
{ returns -1 if the Exec failed, otherwise returns the process' }
{ exit code when the process terminates. }
var
zAppName: array[0..512] of char;
zCurDir: array[0..255] of char;
WorkDir: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
StrPCopy(zAppName, FileName);
GetDir(0, WorkDir);
StrPCopy(zCurDir, WorkDir);
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then { pointer to PROCESS_INF }
Result := -1
else
begin
case WaitforSingleObject(ProcessInfo.hProcess, TimeOut) of
WAIT_TIMEOUT: TerminateProcess(ProcessInfo.hProcess, Cardinal(Result));
else
GetExitCodeProcess(ProcessInfo.hProcess, Cardinal(Result));
end;
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;