1

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!

user1526124
  • 229
  • 4
  • 20
  • 2
    `ProcInfo.hProcess` is a **process** handle. `GetWindowText` is expecting a **window** handle. They're not the same thing at all, any more than a door handle is a knife handle. – Ken White Nov 19 '12 at 13:40

1 Answers1

6

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!
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    You might need to include a class name or visibility or etc.. test to the 4th step as many processes creates more than one top level window. – Sertac Akyuz Nov 19 '12 at 18:14