Jim Fell
Jim Fell

Reputation: 14264

CreateWindow Fails as Unable to Find Window Class - C++

In my application the function CreateWindow is failing for some reason. GetLastError indicates error 1407, which, according to the MSDN documentation is "Cannot find window class." The following code shows how CreateWindow is being called and the respective variables names at time of call:

m_hInstance = ::GetModuleHandle( NULL );

if ( m_hInstance == NULL )
{
    TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to retrieve the module handle.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
    THROW(::GetLastError());
}

m_hWnd = ::CreateWindow(
    _pwcWindowClass,    // L"USBEventNotificationWindowClass"
    _pwcWindowName,     // L"USBEventNotificationWindow"
    WS_ICONIC,
    0,
    0,
    CW_USEDEFAULT,
    0,
    NULL,
    NULL,
    m_hInstance,        // 0x00400000
    NULL
    );

if ( m_hWnd == NULL )   // m_hWnd is returned as NULL and exception is thrown.
{
    TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to create window.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
    THROW(::GetLastError());
}

::ShowWindow( m_hWnd, SW_HIDE );

What am I doing wrong?

Upvotes: 3

Views: 5895

Answers (1)

Steve Townsend
Steve Townsend

Reputation: 54178

You have to call RegisterClassEx before you can use the window class on CreateWindow.

Example code here.

Each process must register its own window classes. To register an application local class, use the RegisterClassEx function. You must define the window procedure, fill the members of the WNDCLASSEX structure, and then pass a pointer to the structure to the RegisterClassEx function.

Upvotes: 8

Related Questions