Detecting USB Insertion / Removal Events in Windows using C++

后端 未结 4 2051
悲&欢浪女
悲&欢浪女 2020-11-30 02:38

I am writing an extension for an existing application that needs to handle USB insertion/removal events. I know the VID/PID of the device of interest. However, I don\'t ha

4条回答
  •  时光取名叫无心
    2020-11-30 03:22

    I followed your "new approach" and also found that OnDeviceChange wasn't being called. The problem was that there was no message loop because it was a Console app. Calling the following function at regular intervals fixed it.

    void check_for_device_change()
    {
        MSG msg; 
    
        const int val = PeekMessage( &msg, 0, 0, 0, PM_REMOVE );
    
        if( val > 0 )
        { 
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        } 
    }
    

提交回复
热议问题