VC上检测设备热插拔事件

时间:2022-05-20 19:57:23

1.包含头文件“dbt.h”;

2.在头文件“stdafx.h”中添加“#define       WINVER  0x0501”;
3.添加消息映射和处理函数:
afx_msg BOOL OnDeviceChange(UINT nEventType,DWORD dwData);

ON_WM_DEVICECHANGE()


BOOL CTestDlg::OnDeviceChange(UINT nEventType,DWORD dwData)
{
 DEV_BROADCAST_DEVICEINTERFACE* dbd = (DEV_BROADCAST_DEVICEINTERFACE*) dwData;
 
 //这里进行信息匹配,比如guid等
 
 //针对各个事件进行处理.
 switch (nEventType)
 {

 case DBT_DEVICEREMOVECOMPLETE:

//拔掉设备处理

 break;
  
  case DBT_DEVICEARRIVAL:
//插上设备处理
    break;

  default:
  break;
 }
 return TRUE; 
}
4.注册设备
void CTestDlg::RegistDevice()
{
 GUID   hid_guid={0x4D1E55B2,0xF16F,0x11CF,0x88,0xCB,0x00,0x11,0x11,0x00,0x00,0x30};
 DEV_BROADCAST_DEVICEINTERFACE   dbi;
 ZeroMemory(&dbi,sizeof(dbi));
 dbi.dbcc_size=sizeof(dbi);
 dbi.dbcc_devicetype   =   DBT_DEVTYP_DEVICEINTERFACE;
 dbi.dbcc_reserved   =   0;
 dbi.dbcc_classguid   =   hid_guid;
 HDEVNOTIFY   hDevNotify;
  hDevNotify   =   RegisterDeviceNotification(m_hWnd,  
                &dbi,
                DEVICE_NOTIFY_WINDOW_HANDLE
        );
        if(!hDevNotify)  
        {
                int   Err   =   GetLastError();
               MessageBox("RegisterDeviceNotification   failed");
        }

}


PS:

在vc6中出现此错误:error C2065: 'DEV_BROADCAST_DEVICEINTERFACE' : undeclared identifier

解决:

1、#include <dbt.h>;
2、在StdAfx.h添加        #define WINVER 0x0500


只有注册了该设备,OnDeviceChange才能获得详细的信息,否则收到的nEventType参数都是0007,dwData无数据。

常用设备接口类GUID
Identifier Class GUID Header
GUID_DEVINTERFACE_USB_DEVICE {A5DCBF10-6530-11D2-901F-00C04FB951ED} Usbiodef.h
GUID_DEVINTERFACE_USB_HOST_CONTROLLER {3ABF6F2D-71C4-462A-8A92-1E6861E6AF27} Usbiodef.h
GUID_DEVINTERFACE_USB_HUB {F18A0E88-C30C-11D0-8815-00A0C906BED8} Usbiodef.h
GUID_DEVINTERFACE_NET {CAC88484-7515-4C03-82E6-71A87ABAC361} Ndisguid.h
GUID_DEVINTERFACE_MODEM {2C7089AA-2E0E-11D1-B114-00C04FC2AAE4} Ntddmodm.h
GUID_DEVINTERFACE_DISK {53F56307-B6BF-11D0-94F2-00A0C91EFB8B} Ntddstor.h
GUID_DEVINTERFACE_VOLUME {53F5630D-B6BF-11D0-94F2-00A0C91EFB8B} Ntddstor.h
GUID_DEVINTERFACE_MEDIUMCHANGER {53F56310-B6BF-11D0-94F2-00A0C91EFB8B} Ntddstor.h
GUID_DEVINTERFACE_CDROM {53F56308-B6BF-11D0-94F2-00A0C91EFB8B} Ntddstor.h
GUID_DEVINTERFACE_PARTITION {53F5630A-B6BF-11D0-94F2-00A0C91EFB8B} Ntddstor.h
GUID_DEVINTERFACE_HID {4D1E55B2-F16F-11CF-88CB-001111000030} Hidclass.h
GUID_DEVINTERFACE_KEYBOARD {884B96C3-56EF-11D1-BC8C-00A0C91405DD} Ntddkbd.h
GUID_DEVINTERFACE_MOUSE {378DE44C-56EF-11D1-BC8C-00A0C91405DD} Ntddmou.h
GUID_DEVINTERFACE_DISPLAY_ADAPTER {5B45201D-F2F2-4F3B-85BB-30FF1F953599} Ntddvdeo.h
GUID_DEVINTERFACE_IMAGE {6BDD1FC6-810F-11D0-BEC7-08002BE2092F} Wiaintfc.h
GUID_DEVINTERFACE_MONITOR {E6F07B5F-EE97-4a90-B076-33F57BF4EAA7} Ntddvdeo.h
GUID_DEVINTERFACE_BRIGHTNESS {FDE5BBA4-B3F9-46FB-BDAA-0728CE3100B4} Dispmprt.h
GUID_DEVINTERFACE_I2C {2564AA4F-DDDB-4495-B497-6AD4A84163D7} Dispmprt.h
GUID_BTHPORT_DEVICE_INTERFACE {0850302A-B344-4fda-9BE9-90576B8D46F0} Bthdef.h
GUID_DEVINTERFACE_COMPORT {86E0D1E0-8089-11D0-9CE4-08003E301F73} Ntddser.h
GUID_DEVINTERFACE_PARCLASS {811FC6A5-F728-11D0-A537-0000F8753ED1} Ntddpar.h