WM_DEVICECHANGE消息未发送到WndProc - C ++

时间:2010-12-01 17:09:10

标签: c++ visual-studio-2008 usb device windows-messages

我的应用程序创建了一个窗口,用于处理WM_DEVICECHANGE Windows消息。 WndProc会被多次调用,直到我的应用程序调用一个函数来轮询键盘事件,但无论出于什么原因,当我删除或插入我的USB设备时都不会调用它。

这是我的USB设备的GUID。我确定这是对的:

static const GUID _guidForCP210xDevices = {
    0xA2A39220, 0x39F4, 0x4B88, 0xAE, 0xCB, 0x3D, 0x86, 0xA3, 0x5D, 0xC7, 0x48
};

这就是我的窗口创建方式:

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_wcx.cbSize = sizeof(WNDCLASSEX);  // size of structure
m_wcx.style = CS_HREDRAW | CS_VREDRAW; // initially minimized
m_wcx.lpfnWndProc = &WndProc;       // points to window procedure
m_wcx.cbClsExtra = 0;               // no extra class memory
m_wcx.cbWndExtra = 0;               // no extra window memory
m_wcx.hInstance = m_hInstance;      // handle to instance
m_wcx.hIcon = ::LoadIcon( NULL, IDI_APPLICATION ); // default app icon
m_wcx.hCursor = ::LoadCursor( NULL, IDC_ARROW ); // standard arrow cursor
m_wcx.hbrBackground = NULL;         // no background to paint
m_wcx.lpszMenuName = NULL;          // no menu resource
m_wcx.lpszClassName = _pwcWindowClass; // name of window class
m_wcx.hIconSm = NULL;               // search system resources for sm icon

m_atom = ::RegisterClassEx( &m_wcx );

if ( m_atom == 0 )
{
    TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to register window class.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
    THROW(::GetLastError());
}

m_hWnd = ::CreateWindow(
    _pwcWindowClass,
    _pwcWindowName,
    WS_ICONIC,
    0,
    0,
    CW_USEDEFAULT,
    0,
    NULL,
    NULL,
    m_hInstance,
    NULL
    );

if ( m_hWnd == NULL )
{
    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 );    // function does not fail

if ( RegisterForNotification() != ERROR_SUCCESS )
{
    TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to register for device notification.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
    THROW(::GetLastError());
}

这是我注册设备通知的方式:

static DEV_BROADCAST_DEVICEINTERFACE dbt = {0};

ASSERT(m_hWnd != NULL);

// Populate DEV_BROADCAST_DEVICEINTERFACE structure.
dbt.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
dbt.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
dbt.dbcc_classguid = _guidForCP210xDevices;

// Register for HID devic notifications
m_hNotify = RegisterDeviceNotification( m_hWnd, &dbt, DEVICE_NOTIFY_WINDOW_HANDLE );

if ( m_hNotify == NULL )
{
    TRACE(_T("CNotifyWindow::RegisterForNotification : Failed to register for device notification.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
    return ::GetLastError();
}

return ERROR_SUCCESS;

我的WndProc函数如下所示:

static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    DEV_BROADCAST_HDR * pHeader = reinterpret_cast<DEV_BROADCAST_HDR *>(lParam);

    switch ( uMsg )
    {
    case WM_DEVICECHANGE:
        if ( pHeader != NULL )
        {
            if ( pHeader->dbch_devicetype == DBT_DEVTYP_PORT )
            {
                OnDeviceChange( wParam );
            }
        }
        break;

    default:
        // Do nothing.
        break;
    }

    return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
}

有谁知道我做错了什么?感谢。

1 个答案:

答案 0 :(得分:4)

您错过了一个消息泵来从队列中检索通知并将它们发送到您的WndProc。消息泵实际上是一个循环,它检查消息并同步调用适当的WndProc。 MSDN有一些很好的信息。我不知道你的代码的上下文是什么,所以我不确定你是否只需要在RegisterForNotification之后插入一个泵,或者是否需要进行更大的体系结构更改。