Board index » delphi » Get messages for an external window (like Notepad) in a thread

Get messages for an external window (like Notepad) in a thread

Hi!

In my program, I have a Thread (TThread) which does something like that :

procedure TBuffThread.Execute;
...
while(Terminated = false) do
  begin
    //Get Messages in fHandle queue
    if PeekMessage(lpMsg, fHandle, 0, 600, PM_NOREMOVE) then
    begin
      if lpMsg.message = RFB_SCREEN_UPDATE  then
...

where fHandle is the handle (HWND) to a specified window (already opened on
the computer) like Notepad and RFB_SCREEN_UPDATE  a message previously
registered with RegisterWindowMessage() and sent by an hook installed on the
window fHandle.

The problem is that the thread get absolutely no messages.  I know that I
can't get messages for a window which was not created by my thread (which is
my case!) and in the situation of my program I can't put another hook
either.  So, any ideas how to proceed?

Thanks,

hooger

 

Re:Get messages for an external window (like Notepad) in a thread


I don't know if this is the problem but, each thread must have it's
own message pump.

You are using peekMessage which does not actually serve as an official

message pump.

You could be getting the messages, but never dispatching them.

On Tue, 09 Jul 2002 19:09:42 GMT, "hooger"

Quote
<mberge...@NOSPAM.eduglobe.ca> wrote:
>In my program, I have a Thread (TThread) which does something like that :

>procedure TBuffThread.Execute;
<>
>    //Get Messages in fHandle queue
>    if PeekMessage(lpMsg, fHandle, 0, 600, PM_NOREMOVE) then
>    begin
>      if lpMsg.message = RFB_SCREEN_UPDATE  then
<>
>The problem is that the thread get absolutely no messages.  I know that I
>can't get messages for a window which was not created by my thread (which is
>my case!) and in the situation of my program I can't put another hook
>either.  So, any ideas how to proceed?

Re:Get messages for an external window (like Notepad) in a thread


Quote
hooger <mberge...@NOSPAM.eduglobe.ca> wrote in message

news:WxGW8.2928$_y5.110415@charlie.risq.qc.ca...

Quote
> Hi!

> In my program, I have a Thread (TThread) which does something like that :

> procedure TBuffThread.Execute;
> ...
> while(Terminated = false) do
>   begin
>     file://Get Messages in fHandle queue
>     if PeekMessage(lpMsg, fHandle, 0, 600, PM_NOREMOVE) then
>     begin
>       if lpMsg.message = RFB_SCREEN_UPDATE  then
> ...

> where fHandle is the handle (HWND) to a specified window (already opened
on
> the computer) like Notepad and RFB_SCREEN_UPDATE  a message previously
> registered with RegisterWindowMessage() and sent by an hook installed on
the
> window fHandle.

> The problem is that the thread get absolutely no messages.  I know that I
> can't get messages for a window which was not created by my thread (which
is
> my case!) and in the situation of my program I can't put another hook
> either.  So, any ideas how to proceed?

> Thanks,

> hooger

I think you have this one back the front.     PeekMessage() and the like
fetch messages from the message queue associated with the thread in which
they are executed. PostMessage() and the like place messages in the queue of
the thread that created the window to which they are sent.

I've never tried hooking an application and getting the hook to send an
interapplication message, but assuming you have this bit working OK, then
what you need to do is get a message queue working in your thread. Windows
does this for you when the thread creates its first window. Currently you
have none, so you can do something quick like creating a TTimer in the
Execute code, or use the Delphi function AllocateHWnd to create one. This is
the window handle that you use in PeekMessage. If this is all the thread is
doing, then I would be inclined to use GetMessage() with NULL as the window
handle. This simplifies any problems of not getting the right handle and so
forth.

This leaves the problem of getting the message to the queue of the thread.
Once again, you can avoid the issue of having to identify the right window
to post the message to by using the  HWND_BROADCAST for the hWnd parameter
of Postmessage()........... I think this will find the thread window OK, but
am not sure.

Dave

Re:Get messages for an external window (like Notepad) in a thread


It works!  Thank you very much!!!

hooger

Quote
"David Reeve" <drscienti...@powerup.com.au> wrote in message

news:L%JW8.1059$Cq.45099@ozemail.com.au...
Quote

> hooger <mberge...@NOSPAM.eduglobe.ca> wrote in message
> news:WxGW8.2928$_y5.110415@charlie.risq.qc.ca...
> > Hi!

> > In my program, I have a Thread (TThread) which does something like that
:

> > procedure TBuffThread.Execute;
> > ...
> > while(Terminated = false) do
> >   begin
> >     file://Get Messages in fHandle queue
> >     if PeekMessage(lpMsg, fHandle, 0, 600, PM_NOREMOVE) then
> >     begin
> >       if lpMsg.message = RFB_SCREEN_UPDATE  then
> > ...

> > where fHandle is the handle (HWND) to a specified window (already opened
> on
> > the computer) like Notepad and RFB_SCREEN_UPDATE  a message previously
> > registered with RegisterWindowMessage() and sent by an hook installed on
> the
> > window fHandle.

> > The problem is that the thread get absolutely no messages.  I know that
I
> > can't get messages for a window which was not created by my thread
(which
> is
> > my case!) and in the situation of my program I can't put another hook
> > either.  So, any ideas how to proceed?

> > Thanks,

> > hooger

> I think you have this one back the front.     PeekMessage() and the like
> fetch messages from the message queue associated with the thread in which
> they are executed. PostMessage() and the like place messages in the queue
of
> the thread that created the window to which they are sent.

> I've never tried hooking an application and getting the hook to send an
> interapplication message, but assuming you have this bit working OK, then
> what you need to do is get a message queue working in your thread. Windows
> does this for you when the thread creates its first window. Currently you
> have none, so you can do something quick like creating a TTimer in the
> Execute code, or use the Delphi function AllocateHWnd to create one. This
is
> the window handle that you use in PeekMessage. If this is all the thread
is
> doing, then I would be inclined to use GetMessage() with NULL as the
window
> handle. This simplifies any problems of not getting the right handle and
so
> forth.

> This leaves the problem of getting the message to the queue of the thread.
> Once again, you can avoid the issue of having to identify the right window
> to post the message to by using the  HWND_BROADCAST for the hWnd parameter
> of Postmessage()........... I think this will find the thread window OK,
but
> am not sure.

> Dave

Re:Get messages for an external window (like Notepad) in a thread


I have worked a lot with messages between applications,
but they were all written by myself.
I'm afraid, I have no idea about if you can let an alien application
(i.e. not home brew :-) know it should send (or post) messages
to this or that particular app.

You can send standard windows messages (Wm_Close for instance) to any
application yourself, as long as you know the correct handle of its main form.

Quote
hooger wrote:
> Hi!

> In my program, I have a Thread (TThread) which does something like that :

> procedure TBuffThread.Execute;
> ...
> while(Terminated = false) do
>   begin
>     //Get Messages in fHandle queue
>     if PeekMessage(lpMsg, fHandle, 0, 600, PM_NOREMOVE) then
>     begin
>       if lpMsg.message = RFB_SCREEN_UPDATE  then
> ...

> where fHandle is the handle (HWND) to a specified window (already opened on
> the computer) like Notepad and RFB_SCREEN_UPDATE  a message previously
> registered with RegisterWindowMessage() and sent by an hook installed on the
> window fHandle.

> The problem is that the thread get absolutely no messages.  I know that I
> can't get messages for a window which was not created by my thread (which is
> my case!) and in the situation of my program I can't put another hook
> either.  So, any ideas how to proceed?

> Thanks,

> hooger

Other Threads