这是我在一个论坛的讨论稿,我想这些情况在实践程序设计过程中会常碰到,所以就将这些讨论资料写了下来:
作者:e梦缘
?
请问用什么方法可以运行一个外部的EXE并返回它的Handle? WinExec 返回不是该EXE的Handle ShellExecute, ShellExecuteEx 也不是 CreateProcess 返回的是 Process Handle 到底要如何才可以返回和 GetWindow 所返回的一样的 Handle?
Re:
可以用FindWindow()函数,它能够解决这个问题,它有两个参数:lpClassName:程序的类名;lpWindowName:程序窗体的标题。
例如:
procedure TForm1.Button1Click(Sender: TObject); begin ShellExecute(handle,'open','notepad.exe',nil,nil,SW_ShowNormal); end;
procedure TForm1.Button2Click(Sender: TObject); var ExeHandle:Thandle; Begin //获得句柄 ExeHandle:=FindWindow('notepad',nil);//'');//返回句柄 //关闭程序 if ExeHandle<>0 then SendMessage(ExeHandle,WM_Close,0,0) Else Application.MessageBox('没有打开"记事本"程序!','提示', MB_IconInformation+MB_OK); end;
? FindWindow() 方法不是我想要的,因为这里的lpWindowName 具有不确定性,没有办法准确地找到。 我希望能在执行这支外部程序里就可以得到它的 Handle. 比如有没有办法,通过Process Handle得到它有 Application Handle 以及 MainForm Handle
Re:
您可以通过lpClassName:程序的类名来准确地找到。
ExeHandle:=FindWindow('notepad',nil);//返回句柄
function ExecuteFile(const FileName, Params, DefaultDir: String;
ShowCmd: Integer): THandle;
var
zFileName, zParams, zDir: array[0..79] of Char;
begin
Result := ShellExecute(Application.MainForm.Handle, nil,
StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),
StrPCopy(zDir, DefaultDir), ShowCmd);
end;
procedure TForm1.Button3Click(Sender: TObject); var hwd:thandle; begin
hwd:=ExecuteFile('notepad.exe','','',SW_ShowNormal); if hwd<>0 then showmessage('哈哈~~'+#13+'我取到handle了,是>>>'+inttostr(hwd));
end;
?
谢谢您的热心帮助, 但ShellExecute得到的并不是Handle 用FindWindow得到的才是真正的Handle 你可以试一下. 最简单的方法就是向你得到的Handle发一个WM_QUIT消息,看它是否会被关闭. 如: SendMessage(AHandle, WM_QUIT, 0, 0);
Re:
我看了很多delphi的资料,好像只有FindWindow()函数是 可以实现的,至于是否还有其它的方法,我 不太清楚!
最后结果:
我已从大富翁论坛上得到了满意的结论了,在这和大家分享一下: (大富翁论坛: http://www.delphibbs.com/)
type PEnumInfo = ^TEnumInfo; TEnumInfo = record ProcessID : DWORD; HWND : THandle; end;
function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall; var PID : DWORD; begin GetWindowThreadProcessID(Wnd, @PID); Result := (PID <> EI.ProcessID) or (not IsWindowVisible(WND)) or (not IsWindowEnabled(WND));
if not result then EI.HWND := WND; end;
function FindMainWindow(PID: DWORD): DWORD; var EI : TEnumInfo; begin EI.ProcessID := PID; EI.HWND := 0; EnumWindows(@EnumWindowsProc, Integer(@EI)); Result := EI.HWND; end;
procedure TForm1.Button1Click(Sender: TObject); var SI : TStartupInfo; PI : TProcessInformation; H : THandle; S : String; begin ZeroMemory(@SI, SizeOf(SI)); ZeroMemory(@PI, SizeOf(PI)); SI.cb := SizeOf(SI); if CreateProcess(nil,'CALC.EXE', nil, nil, FALSE, 0 ,nil,nil, SI, PI) then begin //注意! WaitForInputIdle(PI.hProcess, INFINITE);
H := FindMainWindow(PI.dwProcessID); if H > 0 then begin SetLength(S, 255); GetWindowText(H, PChar(S), 255); SetLength(S, StrLen(PChar(S))); ShowMessage(S); end;
CloseHandle(PI.hProcess); CloseHandle(PI.hThread); end; end; end.

|