views:

2148

answers:

4

Hi, my question is : how can I use SendMessage() to implement thread communication between two threads, one with a window (GUI) and the other with no window?

The problem is that SendMessage() needs a handle (HWND)?

Another detail about my project : Two threads, one running managed code (the one with the user interface), and the other running native code (the one without window)

Thank you very much!

+4  A: 

Perhaps you should try to use PostMessage or PostThreadMessage

schnaader
I like PostThreadMessage a lot.Is there a blockant version of this function? Because PostThreadMessage returns without waiting for the callee to process the message.
No. It wouldn't be too hard to implement a blocking version - use PostThreadMessage, and then have the calling thread block on an event that the callee will set when it has processed the message
1800 INFORMATION
+1  A: 

If the thread has no window, no message queue, and no message dispatcher, then it's going to be hard to a message to it. It is common for threads to create hidden windows just for communication purposes (take a look with Windows Spy and you'll see plenty of examples).

One alternative is to use shared memory and a synchronization primitive such an event or semaphore. Another alternative is to use pipes.

jdigital
+1  A: 

what @jdigital said. Note that if you create a hidden window, and your thread does not already implement a message loop (either in regular win32-speak, or one in the context of a COM STA -- and if you have no idea what I'm talking about then one probably does not exist in your thread), you'll also want to create a message loop as well. ATL makes it fairly easy with _AtlModule.RunMessageLoop(); Unfortunately this also means the thread in question is probably going to need to be event-driven while it is in the message loop. You can do tricky things like MsgWaitForMultipleObjects, but it gets hairy.

Here's an example of hidden windows if you're familiar with ATL/COM. I went through this pain a while back and thought there was a useful discussion on microsoft.public.vc.atl, but the best I can find now is this post. which goes into some detail about variants of message loops (what to do differently if you have keystroke accelerators or modeless windows, sounds like you don't in your application).

Jason S
+1  A: 

I would suggest creating a hidden window. When using postthreadmessage, there is a chance that your message could get lost (ie: if a messagebox is running the message loop).

More info about that at:

http://blogs.msdn.com/oldnewthing/archive/2005/04/26/412116.aspx

Bob