如何得到运行程序返回的句柄值
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
H : THandle;
PI : TProcessInformation;
implementation
{$R *.dfm}
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;
S : String;
begin
ZeroMemory(@SI, SizeOf(SI));
ZeroMemory(@PI, SizeOf(PI));
SI.cb := SizeOf(SI);
if CreateProcess(nil,'D:\Program Files\StormII\Storm.exe', nil, nil, False, 0 ,nil,nil, SI, PI) then
begin
//注意!
WaitForInputIdle(PI.hProcess, INFINITE);
//INFINITE
H := FindMainWindow(PI.dwProcessID);
if H>0 then
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//PostThreadMessage(pi.dwThreadId, WM_QUIT, 0,0);
//SendMessage(H, WM_LBUTTONUP, MK_LBUTTON, makelparam(50,50));
SendMessage(H,WM_LBUTTONdown, MK_LBUTTON, makelparam(50,50));
//PostThreadMessage(pi.dwThreadId, WM_QUIT, 0,0);
end;
end.
现在是如何得到H返回的句柄值,如果在H := FindMainWindow(PI.dwProcessID);
设断点可以得到H返回的句柄值,但是不设置断点就得不到,希望能给我解决这个问题,谢谢,分不够,开贴再送