关闭

<C/C++>USB设备注册与插拔监听

标签: usb
1771人阅读 评论(0) 收藏 举报
分类:

设备注册:

#include <DBT.h>
#include <comdefs.h>

void RegisterDevice()
{
	const GUID GUID_DEVINTERFACE_LIST[] = { 
		{ 0xA5DCBF10, 0x6530, 0x11D2, { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } },
		{ 0x53f56307, 0xb6bf, 0x11d0, { 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },
		{ 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },	/* HID */
		{ 0xad498944, 0x762f, 0x11d0, { 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } }};

	HDEVNOTIFY   hDevNotify;

	DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
	ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
	NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
	NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
	for(int i=0; i<sizeof(GUID_DEVINTERFACE_LIST)/sizeof(GUID); i++)
	{
		NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_LIST[i];
		hDevNotify = RegisterDeviceNotification(m_hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
		if( !hDevNotify ) {
			MessageBox(m_hWnd, TEXT("注册USB设备通知失败"), TEXT("错误"), MB_ICONERROR);
			break;
		}
	}

}


插拔监听:

LRESULT CShuajiBaoDlg::HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
	switch (uMsg)
	{
	case WM_DEVICECHANGE:
		if (lpdb)
		{
			bHandled = TRUE;
			if (wParam == DBT_DEVICEARRIVAL)
			{
				// 设备插入
			}
			else if (wParam == DBT_DEVICEREMOVECOMPLETE)
			{
				// 设备拔出
			}
		}
		break;

	}
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:66508次
    • 积分:1308
    • 等级:
    • 排名:千里之外
    • 原创:64篇
    • 转载:9篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论