关于HID设备与WM_DEVICECHANGE

kgduwu BAOLONG 服务器端工程师  2008-12-08 11:12:56
各位,问一个问题,当HID设备如USB鼠标或键盘插上或拔除时,系统向各窗口发送WM_DEVICECHANGE消息所带的wParam,lParam等相关信息,我在程序中试了一下,插入USB鼠标,系统收到4条WM_DEVICECHANGE消息,wParam都显示为0X07,VC中有#define DBT_DEVNODES_CHANGED 0x0007 而拔除USB鼠标系统收到2条WM_DEVICECHANGE消息,wParam都显示为0X07 而插入或拔除U盘时系统除了收到几条WM_DEVICECHANGE消息,wParam都显示为0X07外, 还有分别有wParam显示为0X8000或0x8004 ,VC中#define BT_DEVICEARRIVAL 0x8000,#define DBT_DEVICEREMOVECOMPLETE 0x8004,那请问HID设备如USB鼠标或键盘插上或拔除时WM_DEVICECHANGE消息的wParam有什么规律,有没有熟悉的,指导指导


另我在程式的消息循环中,处理WM_DEVICECHANGE消息,即窗口每收到一个WM_DEVICECHANGE消息,即使用一个消息框把该消息的wParam值显示出来,我的程序运行后,当HID设备如USB鼠标或键盘插上或拔除时,或插入或拔除U盘时收到的消息如上所述收到那些消息,但是我让我的程序一直开着,不插拔任何USB鼠标或键盘或U盘(不插拔任何设备,间隔大概7至8分钟左右,我发现程序中每次还是弹出3个消息框,显示wParam值为0x0007,我看到设备管理器窗口也闪一下,但我试着找是否有相关时而断开,时而连上,而导致窗口收到WM_DEVICECHANGE,基本没发现什么硬件有问题啊,有没有谁知道是咋会事?
...全文
717 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zhongjj2008 2012-09-10
需要注册设备 RegisterDeviceNotification
回复
erlangshens2012 2012-04-23
我想通过获取USB鼠标返回的数据来算出鼠标移动的速度,可以实现么?有源代码么~~~谢谢!
回复
iwillbeback008 2011-08-29
look one look
回复
投投 2011-03-16
ssssss
回复
cqxc413 2010-08-19
look one look
回复
s669pengwei 2010-04-14
如需要阅读该回复,请登录或注册CSDN!我没登陆?
回复
ilovedrv 2008-12-09
/*
* Message = WM_DEVICECHANGE
* wParam = DBT_DEVNODES_CHANGED
* lParam = 0
*
* send when configmg finished a process tree batch. Some devnodes
* may have been added or removed. This is used by ring3 people which
* need to be refreshed whenever any devnode changed occur (like
* device manager). People specific to certain devices should use
* DBT_DEVICE* instead.
*/

#define DBT_DEVNODES_CHANGED 0x0007

是用来刷新节点的,一般不处理
回复
cnzdgs 2008-12-08
看起来是硬件连接不好,你先把各种USB设备都拔除,看看还会不会收到消息。
回复
ilovedrv 2008-12-08
关键的就是两个函数处理


LRESULT CHWDetectDlg::OnMyDeviceChange(WPARAM wParam, LPARAM lParam)
{
if ( DBT_DEVICEARRIVAL == wParam || DBT_DEVICEREMOVECOMPLETE == wParam ) {
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
PDEV_BROADCAST_DEVICEINTERFACE pDevInf;
PDEV_BROADCAST_HANDLE pDevHnd;
PDEV_BROADCAST_OEM pDevOem;
PDEV_BROADCAST_PORT pDevPort;
PDEV_BROADCAST_VOLUME pDevVolume;
switch( pHdr->dbch_devicetype ) {
case DBT_DEVTYP_DEVICEINTERFACE:
pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
UpdateDevice(pDevInf, wParam);
break;

case DBT_DEVTYP_HANDLE:
pDevHnd = (PDEV_BROADCAST_HANDLE)pHdr;
break;

case DBT_DEVTYP_OEM:
pDevOem = (PDEV_BROADCAST_OEM)pHdr;
break;

case DBT_DEVTYP_PORT:
pDevPort = (PDEV_BROADCAST_PORT)pHdr;
break;

case DBT_DEVTYP_VOLUME:
pDevVolume = (PDEV_BROADCAST_VOLUME)pHdr;
break;
}
}
return 0;
}



void CHWDetectDlg::UpdateDevice(PDEV_BROADCAST_DEVICEINTERFACE pDevInf, WPARAM wParam)
{
// pDevInf->dbcc_name:
// \\?\USB#Vid_04e8&Pid_503b#0002F9A9828E0F06#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
// szDevId: USB\Vid_04e8&Pid_503b\0002F9A9828E0F06
// szClass: USB
ASSERT(lstrlen(pDevInf->dbcc_name) > 4);
CString szDevId = pDevInf->dbcc_name+4;
int idx = szDevId.ReverseFind(_T('#'));
ASSERT( -1 != idx );
szDevId.Truncate(idx);
szDevId.Replace(_T('#'), _T('\\'));
szDevId.MakeUpper();

CString szClass;
idx = szDevId.Find(_T('\\'));
ASSERT(-1 != idx );
szClass = szDevId.Left(idx);
CString szLog;
m_ctrlEdit.GetWindowText(szLog);
CString szTmp;
if ( DBT_DEVICEARRIVAL == wParam ) {
szTmp.Format(_T("Adding %s\r\n"), szDevId.GetBuffer());
// TRACE("Adding %s\n", szDevId.GetBuffer());
} else {
szTmp.Format(_T("Removing %s\r\n"), szDevId.GetBuffer());
// TRACE("Removing %s\n", szDevId.GetBuffer());
}
szLog.Append(szTmp);
m_ctrlEdit.SetWindowText(szLog);

// seems we should ignore "ROOT" type....
if ( _T("ROOT") == szClass ) {
return;
}

DWORD dwFlag = DBT_DEVICEARRIVAL != wParam ?
DIGCF_ALLCLASSES : (DIGCF_ALLCLASSES | DIGCF_PRESENT);
HDEVINFO hDevInfo = SetupDiGetClassDevs(NULL,szClass,NULL,dwFlag);
if( INVALID_HANDLE_VALUE == hDevInfo ) {
AfxMessageBox(CString("SetupDiGetClassDevs(): ")
+ _com_error(GetLastError()).ErrorMessage(), MB_ICONEXCLAMATION);
return;
}

SP_DEVINFO_DATA spDevInfoData;
if ( FindDevice(hDevInfo, szDevId, spDevInfoData) ) {
// OK, device found
DWORD DataT ;
TCHAR buf[MAX_PATH];
DWORD nSize = 0;

// get Friendly Name or Device Description
if ( SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData,
SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize) ) {
} else if ( SetupDiGetDeviceRegistryProperty(hDevInfo, &spDevInfoData,
SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize) ) {
} else {
lstrcpy(buf, _T("Unknown"));
}

HTREEITEM hClass = GetClassItem(&(spDevInfoData.ClassGuid), wParam);
// hClass can only be NULL for remove device and class node is not found
if ( NULL == hClass ) return;
// remove device
HTREEITEM hChildItem = m_ctrlTree.GetChildItem(hClass);
BOOL bFound = FALSE;
while (hChildItem != NULL) {
CString* pszData = (CString*)m_ctrlTree.GetItemData(hChildItem);
if ( szDevId == *pszData ) {
bFound = TRUE;
if ( DBT_DEVICEARRIVAL == wParam ) {
// add device, but seems the device already exists
break;
} else {
// remove device
delete pszData;
m_ctrlTree.DeleteItem(hChildItem);
if ( !m_ctrlTree.ItemHasChildren(hClass) ) {
HeapFree(GetProcessHeap(), 0, (LPVOID)m_ctrlTree.GetItemData(hClass));
m_ctrlTree.DeleteItem(hClass);
}
break;
}
} else {
hChildItem = m_ctrlTree.GetNextItem(hChildItem, TVGN_NEXT);
}
}
if ( DBT_DEVICEARRIVAL == wParam && !bFound ) {
// Add device
int n = GetClassImgIndex(&(spDevInfoData.ClassGuid));
HTREEITEM hItem = m_ctrlTree.InsertItem(buf, n, n, hClass);
m_ctrlTree.SetItemData(hItem, (DWORD_PTR)(new CString(szDevId)));
// set pspDevInfoData to NULL, otherwise it will be HeapFree
m_ctrlTree.Expand(hClass, TVE_EXPAND);
}
}

SetupDiDestroyDeviceInfoList(hDevInfo);
}
回复
ilovedrv 2008-12-08
直接响应DBT_DEVTYP_DEVICEINTERFACE就可以了,其它可以不用处理

看这个例子,基本上可以直接用

Detecting Hardware Insertion and/or Removal

http://www.codeproject.com/KB/system/HwDetect.aspx
回复
相关推荐
基于java的企业人事管理系统设计--软件工程课程设计(含源码论文设计).rar 1 引言 4 1.1 课程设计目标 4 1.2 编程工具(编程环境)介绍 4 1.3 实施时间及主要实施步骤 4 2 需求分析 5 3 系统总体设计 6 4 数据库设计 6 5 主要功能模块的设计实现 10 5.1 功能模块1详细设计(综合查询员工信息)..........................................................10 5.1.1 详细设计.......................................................................................................10 5.1.2 算法流程........................................................................................................15 5.1.3 界面设计及测试结果.....................................................................................15 6 调试分析 15 7 用户手册 16 8 测试结果 17 8.1 员工信息的添加...................................................................................................17 8.2 员工信息的修改...................................................................................................17 8.3 员工信息的删除...................................................................................................18 8.4 员工信息的综合查询...........................................................................................18 8.5 员工信息按性别统计的结果................................................................................19 8.6 员工信息按状态统计的结果...............................................................................19 8.7 员工信息按职称统计的结果................................................................................20 9 结论 20 10 参考文献 20
发帖
硬件/系统
创建于2007-09-28

2594

社区成员

VC/MFC 硬件/系统
申请成为版主
帖子事件
创建了帖子
2008-12-08 11:12
社区公告
暂无公告