Rivari
Rivari

Reputation:

Thread communication using SendMessage

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!

Upvotes: 5

Views: 10768

Answers (4)

Bob
Bob

Reputation: 255

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

Upvotes: 4

Jason S
Jason S

Reputation: 189686

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).

Upvotes: 2

jdigital
jdigital

Reputation: 12276

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.

Upvotes: 2

schnaader
schnaader

Reputation: 49719

Perhaps you should try to use PostMessage or PostThreadMessage

Upvotes: 3

Related Questions