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

设备注册:

#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;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Meta_Cpp/article/details/46818357
文章标签: usb
个人分类: C/C++
上一篇&lt;C/C++&gt; FILE指针的使用
下一篇DuiLib窗口增加阴影效果
想对作者说点什么? 我来说一句

c++监控USB插拔状态源码

2018年01月19日 666B 下载

没有更多推荐了,返回首页

关闭
关闭