CDialog has NULL HWND after CDialog::Create()

David Partridge 21 Reputation points
2022-09-20T15:42:02.787+00:00
processingDlg.Create(IDD_PROCESSING);  


HWND hwnd{ processingDlg.GetSafeHwnd() };  

Sadly the returned HWND is null :(

What can I do to get the HWND assigned without actually displaying the dialog?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,440 questions
{count} votes

Accepted answer
  1. RLWA32 40,861 Reputation points
    2022-09-20T19:44:41.437+00:00

    You're correct Create returned 0 .
    What might cause CDialog::Create to return 0?

    Create a debug build of your application and use the debugger to step through the call to CDialog::Create. You should be able to do this because the MFC source code is installed as part of a Visual Studio installation that includes installing MFC.

    And here's some bonus reading for you to do - Why can't I create my dialog with DialogBox, DialogBoxParam, CreateDialog, CreateDialogParam, or the indirect versions of same?

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. David Partridge 21 Reputation points
    2022-09-20T19:35:07.75+00:00

    You're correct Create returned 0 .

    This is not quite your normal MFC application.

    I have my own main() which does:

       if (!AfxWinInit(::GetModuleHandle(nullptr), nullptr, ::GetCommandLine(), 0))  
       {  
       ZTRACE_RUNTIME("Fatal Error: MFC initialization failed");  
       return 1;  
       }  
       // initialize all the windows stuff we need for now  
       theApp.InitInstance();  
    

    theApp.InitInstance in turn calls CWinApp::InitInstance() and that DOES return 1.

    What might cause CDialog::Create to return 0?

    0 comments No comments

  2. David Partridge 21 Reputation points
    2022-09-20T21:33:03.5+00:00

    Problem solved - the message pump wasn't running at that point - I moved the call to processingDlg.Create() to a mf that was g'teed to be called after the message pump was started.

    It was in a ctor that was invoked before the message pump was started.

    Thanks
    D.


  3. David Partridge 21 Reputation points
    2022-09-20T23:28:08.54+00:00

    Hmmm In that case I'm baffled (but happy it now works).

    0 comments No comments