Sending Custom Parameters to a Message Handler

Started by
4 comments, last by veiled eagle 18 years, 5 months ago
just want to know how to set wParam, and lParam when sending a custom message to a message handler in win32 eg i want to send a pointer to one of my objects to the message handler as part of a custom message any response appreciated
Advertisement
From MSDN:-

LRESULT SendMessage(          HWND hWnd,    UINT Msg,    WPARAM wParam,    LPARAM lParam);



So to send a pointer, use the lParam -> SendMessage( hWnd, WM_YOUR_MESSAGE, 0, (LPARAM)your_pointer );

Then in your message handler, remember to cast your lParam back to your pointer type.
Hrm. An additional question to this answer... is it a better idea to use the win32 message handler and send my own messages through it (as mentioned above) or would it be more prudent to create a custom message handler class. I understand that if you need the messages you send to be more complex or in a different format or whatever it would be beneficial to make ones own, but for a simple messaging system, does using the win32 message handler have any drawbacks?
What kind of drawbacks are you interested in? Performance? Code becomes harder to write?

There are some gotchas related to using windows messaging one should be aware of.

For instance, sending pointers might not make sense if it is a different thread/process you are sending messages to. (Each thread can have a separate message loop).

When you do a SendMessage, your thread blocks until the processing for that message is done. So you must be really careful if you are multithreaded and might send messages between threads.

PostMessage just posts a message to the queue and returns. It is completely asynchronous.

If you want exact synchronization between sending and receiving messages, it would be preferable not to use windows messaging. Even with SendMessage, you are not guaranteed that it will be handled immediately. (Think of it as a separate SendMessage queue...)



MSDN topic on messages
Quote:Original post by rjackets
Hrm. An additional question to this answer... is it a better idea to use the win32 message handler and send my own messages through it (as mentioned above) or would it be more prudent to create a custom message handler class. I understand that if you need the messages you send to be more complex or in a different format or whatever it would be beneficial to make ones own, but for a simple messaging system, does using the win32 message handler have any drawbacks?


This depends on what kind of messages you are sending around, and who is listening to the messages. If you need to send UI-related or callback type messages to things that have their own message pumps (entire threads or individual windows) then you're fine, although IMHO the Windows message pump system leaves a lot to be desired in terms of writing clean message dispatching code; in my experience it doesn't bend gracefully for use with arbitrary message data.

If you're working in C++ or another OO language, I'd suggest using something like the Command pattern, where individual "messages" are encapsulated by instances of a class. You can even use subclassing to make the message storage and handling much more powerful. As a bonus, such a pattern can easily be used to store a list of requests in a data file and act on them later, or send requests across an IPC pipe to another program, or even send requests across a socket to another computer.

All of that is severe overkill, however, if all you need is to trip your WM_MOUSEUP handler [wink]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Well thats fixed, was what i tried to do in the first place but it helps if you cast to object pointer and not just to object, duh! In response to rjacket like the others said i couldn't really see a need for writing a message handling system unless you were embarking on a substantially sized project, for a small app the dev time spent on that could be much better spent elsewhere, and the win32 message handling system is fairly robust. thanks for the help evolutional

This topic is closed to new replies.

Advertisement