Question

My application creates a window for the purpose of handling the WM_DEVICECHANGE Windows message. WndProc does get called several times, until my application calls a function to poll for keyboard events, but for whatever reason it does not get called when I remove or insert my USB device.

This is the GUID for my USB device. I'm sure it's correct:

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

This is how my window is created:

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

This is how I register for device notification:

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;

My WndProc function looks like this:

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

Does anyone know what I'm doing wrong? Thanks.

Was it helpful?

Solution

You're missing a message pump to retrieve the notifications from the queue and dispatch them to your WndProc. The message pump is effectively a loop that checks for messages and calls the appropriate WndProc synchronously. MSDN has some good information on them. I don't know what the context of your code is, so I'm not sure if you just need to insert a pump after RegisterForNotification or if a larger architectural change is necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top