user1526124
user1526124

Reputation: 247

CreateProcess and get the handle

I'm using CreateProcess API to integrate RealVNC with my exe... I just need to process handle for the created vnc client, but I'm unsuccess so far. The code is pretty simple:

procedure TForm1.VncAuth;
var
  StartInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CmdLine: string;
  title: string;
  ProcHandle: THandle;
begin
  FillChar(StartInfo,SizeOf(TStartupInfo),#0);
  FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
  StartInfo.cb := SizeOf(TStartupInfo);
  CmdLine:= 'vnc.exe';
  UniqueString(CmdLine);
  CreateProcess(NIL ,PChar(CmdLine), NIL, NIL, False, CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS
                                  , NIL, NIL, StartInfo, ProcInfo);
  ProcHandle:= ProcInfo.hProcess;
  GetWindowText(ProcHandle, PChar(title), 255);
  ShowMessage(title);
end;

Nothing is returned in title var... the GetWindowText function is just a test to see if I have the right handle, if Yes I should see the vnc client title's right? Thank you!

Upvotes: 1

Views: 5186

Answers (1)

David Heffernan
David Heffernan

Reputation: 613461

Window handles and process handles are not the same thing. For GetWindowText you need a window handle.

  1. After creating the process call WaitForInputIdle to allow the process to start up and create its main window.
  2. Call EnumWindows to enumerate the top level windows.
  3. For each top level window, call GetWindowThreadProcessId to find out the process ID of the process that created that window. The process ID of the process you created is ProcInfo.dwProcessId.
  4. When you have find a window with process ID that matches that of the process you just created, that window is your guy!

Upvotes: 5

Related Questions