MFC检测USB设备热插拔

在Dlg.cpp中添加头文件

#include <Dbt.h> //刷新设备添加或移除

添加响应消息

ON_WM_DEVICECHANGE() //设备添加或移除通知

添加响应消息函数

BOOL CXXXDlg::OnDeviceChange(UINT nEventType,DWORD dwData)  
{  
	switch (nEventType)  
	{  
	case DBT_DEVICEREMOVECOMPLETE://移除设备 
		
		break;
	case DBT_DEVICEARRIVAL://添加设备 

		break;  
  
	default:  
		break;  
	}  
	return TRUE;  
} 

添加初始化,这一块比较重要,需要注册设备,注册所需的GUID信息在驱动文件的inf文件里,若不注册,有些设备是无法检测到的,返回7。

void CXXXDlg::RegisterCANDevice()
{
	/*	[Version]
		Signature = "$Windows NT$"
		Class = CustomUSBDevices
		ClassGuid= {a503e2d3-a031-49dc-b684-c99085dbfe92}
		Provider = %MFGNAME%
		DriverVer=12/03/2012,1.0.0.7
	*/
    GUID GUID_CLASS_USB_DEVICE = {0xa503e2d3, 0xa031, 0x49dc, 0xb6, 0x84, 0xc9, 0x90, 0x85, 0xdb,0xfe, 0x92};
    HDEVNOTIFY m_hDeviceNotify;
    DEV_BROADCAST_DEVICEINTERFACE Filter;  
    ZeroMemory(&Filter,sizeof(Filter));  
    Filter.dbcc_size = sizeof(Filter);   // size gets set to 29 with 1-byte packing or 32 with 4- or 8-byte packing  
    Filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;  
    Filter.dbcc_classguid = GUID_CLASS_USB_DEVICE;//WceusbshGUID;  
    //DEVICE_NOTIFY_ALL_INTERFACE_CLASSES //关注所有设备事件  
    m_hDeviceNotify =RegisterDeviceNotification(this->m_hWnd,&Filter,DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);  
    if(NULL == m_hDeviceNotify)  
		TRACE("RegisterDeviceNotification failed!!");  
}







猜你喜欢

转载自blog.csdn.net/dongganxiao_maidou/article/details/80709612
今日推荐