程序中需和USB设备进行通信,但中途可能会拔掉USB设备,而且每次的插拔都要做处理,在软件中如何检测到该动作?   

解决方案 »

  1.   

    处理 WM_DEVICECHANGE消息, MSDN中有示例
      

  2.   


    响应WM_DEVICECHANGE消息,在BOOL CFCCompareDlg::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
    {
    CDialog::OnDeviceChange(nEventType, dwData);
        
    switch(nEventType)
    {
    case DBT_DEVICEARRIVAL:
                break;
    case DBT_DEVICEREMOVECOMPLETE:
        break;
    }
    return true;
    }The nEvent parameter can be one of these values:
    DBT_DEVICEARRIVAL   A device has been inserted and is now available. DBT_DEVICEQUERYREMOVE   Permission to remove a device is requested. Any application can deny this request and cancel the removal.DBT_DEVICEQUERYREMOVEFAILED   Request to remove a device has been canceled.
    DBT_DEVICEREMOVEPENDING   Device is about to be removed. Cannot be denied.DBT_DEVICEREMOVECOMPLETE   Device has been removed.DBT_DEVICETYPESPECIFIC   Device-specific event.DBT_CONFIGCHANGED   Current configuration 
      

  3.   

    一般在ondevicechange里面做,是插还是拔俺靠自己枚举。