其他分享
首页 > 其他分享> > CreateWindow因无法找到窗口类而失败 – C

CreateWindow因无法找到窗口类而失败 – C

作者:互联网

在我的应用程序中,CreateWindow函数由于某种原因失败. GetLastError指示错误1407,根据MSDN文档,“无法找到窗口类”.以下代码显示了如何调用CreateWindow以及调用时的相应变量名称:

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 );

我究竟做错了什么?

解决方法:

您必须先调用RegisterClassEx才能在CreateWindow上使用窗口类.

示例代码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.

标签:c,visual-studio-2008,createwindow
来源: https://codeday.me/bug/20190730/1580667.html