Discussion:
Why DispatchMessage() ?
(too old to reply)
smawsk
2008-02-19 19:40:33 UTC
Permalink
Hi,

This is my first post to this group. I have started reading "Windows
Internals" recently. My first question is that is this the right group
to ask questions on Internal working of the Windows OS?

If not then please redirect me to the appropriate group.

Now here's my question:

Why do we need to call DispatchMessage() to dispatch a message to the
window procedure? What if I directly call GetWindowLong() passing it
the window handle and get the address of the window procedure
associated with that window?

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
...............
WNDPROC fWndProc = (WNDPROC)GetWindowLong(Msg.hwnd, GWL_WNDPROC);
fWndProc(Msg.hwnd, Msg.message, Msg.wParam, Msg.lParam);
...................
}

Thanks in advance.

Warm Regards.
alexis
2008-02-19 20:52:53 UTC
Permalink
Post by smawsk
Hi,
What if I directly call GetWindowLong() passing it
the window handle and get the address of the window procedure
associated with that window?
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
...............
WNDPROC fWndProc = (WNDPROC)GetWindowLong(Msg.hwnd, GWL_WNDPROC);
fWndProc(Msg.hwnd, Msg.message, Msg.wParam, Msg.lParam);
...................
}
Try and you see it will crash...
Scott Seligman
2008-02-19 20:49:28 UTC
Permalink
Post by smawsk
Why do we need to call DispatchMessage() to dispatch a message to the
window procedure? What if I directly call GetWindowLong() passing
it the window handle and get the address of the window procedure
associated with that window?
If I recall correctly, DispatchMessage is the component responsible for
thunking ANSI messages for Unicode windows (or vice versa). It's also
the piece that calls TimerProc if a timer is registered with one.

No doubt there are other things it does that you'll have to recreate if
you don't call it and send the messages directly.
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
What do you despise? By this are you truly known.
-- Dune by Frank Herbert
Christian ASTOR
2008-02-19 22:09:30 UTC
Permalink
Post by smawsk
Why do we need to call DispatchMessage() to dispatch a message to the
window procedure?
For special messages like WM_TIMER, WM_PAINT,
ANSI <=> UNICODE if needed, ...
smawsk
2008-02-19 23:10:33 UTC
Permalink
Post by Christian ASTOR
Post by smawsk
Why do we need to call DispatchMessage() to dispatch a message to the
window procedure?
For special messages like WM_TIMER, WM_PAINT,
ANSI <=> UNICODE if needed, ...
Can some one please let me know what special processing that
DispatchMessage() does in case of WM_PAINT and WM_TIMER message?

As I understand (abstractly), once GetMessage() has found one message
in the message queue it fills in the details of into the MSG structure
and calls DispatchMessage() to call the window procedure.

So incase of WM_PAINT also, this should hold true. That is calling
window procedure directly would do the job for me. So what advantage
do I have if window procedure gets called thru DispatchMessage()?

Actually what you are saying might be true but it would be very
helpful for me if some one can explain this in detail.

Thanks again.

Warm regards.
Leslie Milburn
2008-02-20 05:48:47 UTC
Permalink
Post by smawsk
Post by Christian ASTOR
Post by smawsk
Why do we need to call DispatchMessage() to dispatch a message to the
window procedure?
For special messages like WM_TIMER, WM_PAINT,
ANSI <=> UNICODE if needed, ...
Can some one please let me know what special processing that
DispatchMessage() does in case of WM_PAINT and WM_TIMER message?
As I understand (abstractly), once GetMessage() has found one message
in the message queue it fills in the details of into the MSG structure
and calls DispatchMessage() to call the window procedure.
So incase of WM_PAINT also, this should hold true. That is calling
window procedure directly would do the job for me. So what advantage
do I have if window procedure gets called thru DispatchMessage()?
Actually what you are saying might be true but it would be very
helpful for me if some one can explain this in detail.
Thanks again.
Warm regards.
You must call DispatchMessage() because to put it bluntly you have no idea
what preprocessing it is doing before finally reaching your WindowProc. This
preprocessing may also be Windows version specific, you really do not want
to go there. Why not just do things how they are supposed to be done and
move on !!
Leslie.
Christian ASTOR
2008-02-20 07:55:58 UTC
Permalink
Post by smawsk
Post by Christian ASTOR
Post by smawsk
Why do we need to call DispatchMessage() to dispatch a message to the
window procedure?
For special messages like WM_TIMER, WM_PAINT,
ANSI <=> UNICODE if needed, ...
Can some one please let me know what special processing that
DispatchMessage() does in case of WM_PAINT and WM_TIMER message?
It does various security checks which are OS dependant.
Scott Seligman
2008-02-19 23:44:08 UTC
Permalink
Post by smawsk
WNDPROC fWndProc = (WNDPROC)GetWindowLong(Msg.hwnd, GWL_WNDPROC);
fWndProc(Msg.hwnd, Msg.message, Msg.wParam, Msg.lParam);
Also, I forgot to mention: GetWindowLong(.., GWL_WNDPROC) may not
always return a valid function pointer, the docs say:

If this value is obtained by calling the GetWindowLong function
with the nIndex parameter set to GWL_WNDPROC or DWL_DLGPROC, it is
actually either the address of a window or dialog box procedure, or
a special internal value meaningful only to CallWindowProc.
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
The devil's cleverest trick is to convince us that he does not exist.
-- Charles Baudelaire
JussiJ
2008-02-27 05:17:06 UTC
Permalink
Post by smawsk
Why do we need to call DispatchMessage() to dispatch
a message to the window procedure?
Windows is a event message system. When things happen
it sends event messages to those that need to know.

By calling the DispatchMessage your application is
giving Windows it's chance to generate these all
important event messages.

One call to DispatchMessage from you application might
result in hundreds of other messages, some of which are
destined for other applications.

And that's why you have no choice but to call DispatchMessage.

Jussi Jumppanen
Author: Zeus for Windows IDE
http://www.zeusedit.com

Loading...