Link to home
Start Free TrialLog in
Avatar of titz
titz

asked on

handle and createprocess/2

hi,

under D3/WIN98 i start an app, which starts another app (by using createprocess).
i want to send messages from app1 to app2 (not in the other direction)
by the POSTMESSAGE-call.
but how can i get the handle if the window of app2 ?
because createprocess only gives a handle for the process not for the window!
i am looking for something like the reversal of GETWINDOWTHREADPROCESSID. But i did not find it.
FINDWINDOW can not be used, for the title of the app2 can be different or changed.

any idea ?

cheers
titz


 
Avatar of inthe
inthe

hi,
why not use findwindow using the classname instead of caption ?
i have example of this with sending message.(well starting notepad and sending text to it.)
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is what I do to get a window handle after using CreateProcess:

In the Process_Information structure returned by CreateProcess you have a field which is the ProcessID use this
as follows with EnumWindows an the following callback:

EnumWindows(@FindProcessWindow, ProcessID);

function FindProcessWindow(Window: hWnd; ProcessID: lParam): boolean; stdcall;
var
  PID: dword;
  Rect: TRect;
begin
  Result := true; { Continue enumeration }
  GetWindowRect(Window, Rect);
  if (not IsWindowVisible(Window)) or     { Ignore invisible windows }
     (not IsWindow(Window)) or            { Ignore non windows !!! }
     (GetParent(Window) <> 0) or          { Ignore windows with parents }
     (GetWindowTextLength(Window) = 0) or { Ignore windows with no text }
     (Rect.Left = Rect.Right) or          { Ignore windows with no width }
     (Rect.Top = Rect.Bottom)             { Ignore windows with no depth }
    then Exit;
  GetWindowThreadProcessID(Window, @PID); { Get the process ID }
  if PID = dword(ProcessID)
    then
      begin
        WindowHandle := Window;
        Result := false; { Found a window so stop }
      end;
end;

I used to use FindWindows but during testing with Notepad.exe I found that if there was already an instance running and I started another thenI could get the handle of the wrong window.

I also found that if the app that has just been started takes a while to present a window then you need to put the EnumWindows in a loop till a satifactory result is obtained.

Cheers - Walter McKie
Walter's suggestion looks alright to me, except one thing (WHY DO ALL PEOPLE MAKE THIS ERROR???):

function FindProcessWindow(Window: hWnd; ProcessID: lParam): BOOL; stdcall;

The result type must be a BOOL (= 4 bytes long), not a boolean (= 1 byte long).

Regards, Madshi.
Avatar of titz

ASKER

hi inthe,

i will do your way: the class for app2 will have a unique name, so that only this app can ahve this class. then the use of findwindow will be easy.

thanks to all,
titz
ok cheers but do remember to make that clasname unique
(hehe look at microsofts examples)
What if app2 is already running before you run app1? Then you have 2 instances of app2 running at the same time. The chance is then 50:50, that you'll get the wrong window handle.
Avatar of titz

ASKER

hi madshi,

this is not possible.
because app2 is only (and this is sure!) started from app1. And if app1 closes (for what reason ever) always app2 is closed automatically.

thanks
titz

So I guess, app1 can't run twice at the same time, either? Then it's alright.
Avatar of titz

ASKER

Hi madshi,
correct : app1 can only run once. If it is always running, and a second instance will bestarted this one(=the second) will close immidiatly.

thanks for the hint !
titz