1

I've created a MFC window and, from another project, I wanted to add a button to that window via its handle (using FindWindow). The handle is correct, but nothing happens. Is this unachievable or am I doing something wrong?

Here's my code:

HWND hWnd = FindWindow(NULL, "MFCtest");
if(hWnd)
{
    printf("Found it\n");
    HWND hwndButton = CreateWindow( 
        "BUTTON",
        "OK",      // Button text 
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,  // Styles 
        0,         // x position 
        0,         // y position 
        100,        // Button width
        100,        // Button height
        hWnd,     // Parent window
        NULL,       // No menu.
        (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), 
        NULL);      // Pointer not needed.

    if(!hwndButton)
        printf("GetLastError: %d\n", GetLastError());
}
conectionist
  • 2,694
  • 6
  • 28
  • 50
  • I suspect it can't be done – o_weisman Jan 29 '15 at 10:01
  • 1
    The window you create is in a different thread, indeed a different process, from the UI. This never ends well. The system doesn not expect you to do things like this. – David Heffernan Jan 29 '15 at 10:01
  • 1
    Have a read of [Raymond Chen's blog](http://blogs.msdn.com/b/oldnewthing/archive/2013/04/12/10410454.aspx) for some relevant insight. – Roger Rowland Jan 29 '15 at 11:39
  • 1
    @o_weisman: SO isn't about guessing. Since you guessed wrong, you may as well remove that comment. – IInspectable Jan 30 '15 at 13:31
  • @IInspectable Maybe my phrasing was wrong, suspect meant to say, to the best of my knowledge. I have yet to see an answer that shows how this can be done that actually works well, so I'm waiting to be proved wrong. Coincidentally, did someone die and leave you in charge of SO? – o_weisman Feb 01 '15 at 07:45
  • @o_weisman: There is no technical reason that inhibits cross-thread window hierarchies. Since you want prove: [Is it legal to have a cross-process parent/child or owner/owned window relationship?](http://blogs.msdn.com/b/oldnewthing/archive/2013/04/12/10410454.aspx) (already provided by Roger above). The reason you don't see an answer to this specific question lies in the circumstances: To make this work reliably, you have to control all participating threads. This question comes up when you don't control the target thread. – IInspectable Feb 01 '15 at 10:58

1 Answers1

1

Yes. BUT!

The problem is that a window always belongs to a thread that creates it. So the messages for such a control messages will arrive in the thread that creates such a window.

This might work, but because messages form the parent window will be sent to the child will take the long way through the message queue and may cause locks if the message can't be retrieved and handled directly.

So at the end I would suggest to: Don't do it!

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • 1
    When creating a `WS_CHILD` window from a different thread than its parent window, the system automatically calls [AttachThreadInput](https://msdn.microsoft.com/en-us/library/windows/desktop/ms681956.aspx). Consequently, input messages will share a single queue, irrespective of the thread that owns the recipient window. – IInspectable Jan 29 '15 at 12:16