views:

522

answers:

4

Is there any way to share a message queue among several threads, or otherwise to read a message queue of a different thread, without using hooks?

A: 

Messages in a message queue can be differentiated on the basis of the window they're for, but I don't think messages can be differentiated on the basis of an inteded thread - the fields just aren't there in the MSG structure - so I don't think you can share a queue over multiple threads.

That leaves you with a non-hook monitoring solution.

I'm pretty sure you could peek another threads queue, but the problem is you're basically polling; so you'll miss messages.

Do you have any influence over the threads you wish to read? if so, you can get them to rebroadcast their messages to you.

Apart from that, I can't see a way to do this.

Blank Xavier
Theoretically yes, and I see where you're going with this, but it's not the solution i'm looking for.
A: 

GetMessage and PeekMessage only read messages for the current thread, you can't use them to read messages sent to the input queue owned by another thread.

Try joining the thread input queues using AttachThreadInput, that might work.

Mo Flanagan
A: 

Ok, that's not an answer rather than a comment. Stackoverflow suddenly forgot who I am (cookies expired?), and as I'm not registered, I can't login. As there's no way to even comment without rep, maybe unregistered users should be disallowed altogether around here.

Rebroadcsting messages won't work for me since some messages should handle and change input prior to return in order to acheive some functionallity (take WM_SIZING as an example).

AttachThreadInput doesn't do it. Honestly, after looking at MSDN I can't say i fully understand what it does do.

As for polling messages at other threads and "loosing" them at the original thread, that's fine with me, if it can be done. It's actually even better.

Meat
I'm still not sure what you're asking for... Queues, including the Windows message queues, are normally used as a synchronization mechanism: a thread pulls messages as it is able, usually blocking until a message is received. If you have two threads reading from the same queue, then only one thread will ever get any given message. Keep in mind: messages that require a response, like WM_SIZING, aren't posted to the message queue at all - they're SENT directly to the window procedure.
Shog9
A: 

AttachThreadInput()

Adrian McCarthy