现在的位置: 首页 > 综合 > 正文

检测U盘设备插入和拔除状态的代码

2013年12月10日 ⁄ 综合 ⁄ 共 977字 ⁄ 字号 评论关闭

void CtestdeviceDlg::OnBnClickedButton1()
{  
 DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
 ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
 NotificationFilter.dbcc_size = sizeof(NotificationFilter);
 NotificationFilter.dbcc_devicetype=DBT_DEVTYP_DEVICEINTERFACE;
 NotificationFilter.dbcc_classguid=CAMERA;
   
 RegisterDeviceNotification(this->m_hWnd,&NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
 //DWORD err= GetLastError();
}

BOOL CtestdeviceDlg::PreTranslateMessage(MSG* pMsg)
{  
    char str[200];
 if( pMsg->message == WM_DEVICECHANGE )
    {  
       //if ( pMsg->wParam==DBT_DEVNODES_CHANGED )
       //int kk=1;
    //sprintf(str,"%d %d ",pMsg->wParam,pMsg->lParam);
    //::MessageBoxA(NULL,str,"",MB_OK);
     
       if ( pMsg->wParam == DBT_DEVICEARRIVAL) //device arrival
       {
           AfxMessageBox(L"arrival",MB_OK,0);
           return 1;
       }
       else if (pMsg->wParam == DBT_DEVICEREMOVECOMPLETE) //device remove
       {
           AfxMessageBox(L"remove",MB_OK,0);
           return -1;
       }
    }

抱歉!评论已关闭.