Joby Taffey
Joby Taffey

Reputation: 83

Can the HWND from CreateWindow/CreateDialog be GetMessage'd from another thread?

Using the Win32 APIs, is it possible to create a Window or Dialog in one thread then collect events for it from another thread?

Are HWNDs tied to threads?

Trying the contrived example below I never see GetMessage() fire.

HWND g_hWnd;

DWORD WINAPI myThreadProc(LPVOID lpParam)
{
    while(GetMessage(&msg, hWnd, 0, 0) > 0)
    {
       ...
    }

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 
{
    hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MYDIALOG), 0, myDlgProc);
    CreateThread(NULL, 0 myThreadProc, NULL, 0, NULL);
    ...
}

But here, I do.

HWND g_hWnd;
HINSTANCE g_hInstance;

DWORD WINAPI myThreadProc(LPVOID lpParam)
{
    hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MYDIALOG), 0, myDlgProc);

    while(GetMessage(&msg, hWnd, 0, 0) > 0)
    {
       ...
    }

}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 
{
    g_hInstance = hInstance;
    CreateThread(NULL, 0 myThreadProc, NULL, 0, NULL);
    ...
}

Can somebody explain what I'm seeing?

Upvotes: 3

Views: 7866

Answers (6)

Valentin Galea
Valentin Galea

Reputation: 1094

Use AttachThreadInput.

Upvotes: 2

Martlark
Martlark

Reputation: 14591

You can of course change the window procedure that handles messages for any window. Check the SetWindowLong function - http://msdn.microsoft.com/en-us/library/ms633591(VS.85).aspx - there are some rules as to what address space the new proc is. I suggest using a dll. Another way is to sub class the window message queue.

Upvotes: -1

bayda
bayda

Reputation: 13581

In your example programm finish after create window.

But anyway in win32 all threads have own message queue.

And all message queues get messages for windows created in this thread.

see:

http://msdn.microsoft.com/en-us/library/ms644928(VS.85).aspx (Using Messages and Message Queues)

http://msdn.microsoft.com/en-us/library/ms644936(VS.85).aspx (GetMessage Function)

Upvotes: 0

dirkgently
dirkgently

Reputation: 111306

In your first example the Dialog and GetMessage are in separate threads. And the documentation says:

The GetMessage function retrieves a message from the calling thread's message queue.

The second example works since the calling thread (for GetMessage) also owns the Dialog.

Upvotes: 2

Michael
Michael

Reputation: 55445

No.

GetMessage returns messages on the current thread's input queue. The HWND parameter is a filter, so that GetMessage only returns messages in the current thread's input queue intended for that window.

Windows have thread affinity - messages intended for a window get handled on the thread that created and therefore owns the window.

Upvotes: 5

newgre
newgre

Reputation: 5291

From the MSDN:

The GetMessage function retrieves a message from the calling thread's message queue

So no, what you describe is not directly possible.

Upvotes: 2

Related Questions