关于HID设备与WM_DEVICECHANGE

kgduwu 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,基本没发现什么硬件有问题啊,有没有谁知道是咋会事?
...全文
849 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
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
HID 设备类读写 #include //! Defines the maximum length of a serial number #define SERNUM_LEN 40 //! Defines the maximum number of physical devices #define MAX_PHYS_DEVICES 6 //! \name HID Device return codes //! @{ // //! HID action/transfer was successful #define HID_DEVICE_SUCCESS 0x00 //! HID device was not found #define HID_DEVICE_NOT_FOUND 0x01 //! HID device is not opened #define HID_DEVICE_NOT_OPENED 0x02 //! HID device is allready opened #define HID_DEVICE_ALREADY_OPENED 0x03 //! Timeout occurs during transfer #define HID_DEVICE_TRANSFER_TIMEOUT 0x04 //! HID transfer failed #define HID_DEVICE_TRANSFER_FAILED 0x05 //! Invalid handle #define HID_DEVICE_HANDLE_ERROR 0x06 //! Unknown error #define HID_DEVICE_UNKNOWN_ERROR 0xFF //! @} // Enabled only when debugging HID connection issues //#define DEBUG_MODE //****************************************************************************** // //! \brief Device information structure. // //****************************************************************************** struct strHidDevice{ //! Handle for hid device HANDLE hndHidDevice; //! Indicator if device is opened BOOL bDeviceOpen; //! Timeout for GetReport requests UINT uGetReportTimeout; //! Timeout for SetReport requests UINT uSetReportTimeout; //! Asynchronous I/O structure OVERLAPPED oRead; //! Asynchronous I/O structure OVERLAPPED oWrite; //! Maximum length of InReport's WORD wInReportBufferLength; //! Maximum length of OutReport's WORD wOutReportBufferLength; //! InBuffer contains data, if InReport provides more data then the application actual need BYTE inBuffer[8192]; //! Number of current used bytes in inBuffer WORD inBufferUsed; }; //****************************************************************************** // //! A structure that tracks the number of serial numbers // //****************************************************************************** struct strTrackSerialNumbers { //! Index number DWORD deviceNum; //! Serial number of physical device char serialNum[SERNUM_LEN]; }; //****************************************************************************** // //! \addtogroup hiddevice_api //! @{ // //****************************************************************************** //****************************************************************************** // //! \brief Close a HID Device. //! //! This function will close a HID device based on the HID structure //! //! \param pstrHidDevice Structure which contains important data of an HID //! device //! //! \return Returns the error status, as one of //! \n \b HID_DEVICE_SUCCESS //! \n \b HID_DEVICE_NOT_OPENED //! \n \b HID_DEVICE_HANDLE_ERROR // //****************************************************************************** BYTE HID_Close(struct strHidDevice* pstrHidDevice); //****************************************************************************** // //! \brief Flush USB buffer for the given device //! //! \param pstrHidDevice Structure which contains important data of an HID //! device. //! //! \return Returns the error status, as one of //! \n \b HID_DEVICE_SUCCESS //! \n \b HID_DEVICE_HANDLE_ERROR //! \n \b HID_DEVICE_UNKNOWN_ERROR // //****************************************************************************** BYTE HID_FlushBuffer(struct strHidDevice* pstrHidDevice); //****************************************************************************** // //! \brief Gets the number of HID devices //! //! This function will return the number of interfaces connected with a //! specified VID, PID and serial number, if no devices are connected, //! it will return a 0 //! //! \param vid Vendor-Id of the device //! \param pid Product-Id of the device //! \param numSerNums Total number of connected physical devices //! //! \return Return the number of connected devices with the specific VID, PID, //! and serial number. // //****************************************************************************** DWORD HID_GetNumOfInterfaces(WORD vid, WORD pid, DWORD numSerNums); //****************************************************************************** // //! \brief Gets the number of serial number and serial number list //! //! Scans the HID Devices on the system for any whose VID/PID match the //! ones specified. For every one it finds, it returns that device's //! serial number in serialNumList. Every physical USB device within a //! given VID/PID space has a unique serial number; therefore, each //! item in the list corresponds with a separate physical USB device //! attached to this host; that is, different physical instances of the //! same product or design. The function returns the total number of //! serial numbers found; if none are found, it returns 0. //! //! \param vid Vendor-ID of the device //! \param pid Product-ID of the device //! \param serialNumList List of serial numbers corresponding to the passed //! VID and PID //! //! \return Returns the number of connected physical devices with the specific //! VID and PID // //****************************************************************************** DWORD HID_GetSerNums(WORD vid, WORD pid, struct strTrackSerialNumbers * serialNumList); //****************************************************************************** // //! \brief Returns the version number of a device. //! //! \param pstrHidDevice Structure which contains important data of an HID //! device. //! \param VersionNumber Pointer to USHORT variable. //! //! \return Returns the error status, as one of //! \n \b HID_DEVICE_SUCCESS //! \n \b HID_DEVICE_HANDLE_ERROR // //****************************************************************************** BYTE HID_GetVersionNumber(struct strHidDevice* pstrHidDevice, USHORT * VersionNumber); //****************************************************************************** // //! \brief Init structure with default values. //! //! It is important to call HID_Init() before calling HID_Open() to //! avoid unpredictable behavoir. //! //! \param pstrHidDevice Structure which contains important data of a HID //! device //! //! \return None // //****************************************************************************** void HID_Init(struct strHidDevice* pstrHidDevice); //****************************************************************************** // //! \brief This has to be called inside WM_ON_DEVICECHANGE notification window //! //! This function checks if the particular HID device structure is //! still connected or disconnected. //! //! \param pstrHidDevice Structure which contains important data of an HID //! device. //! //! \return Returns the error status, as one of //! \n \b HID_DEVICE_SUCCESS //! \n \b HID_DEVICE_HANDLE_ERROR // //****************************************************************************** BOOL HID_IsDeviceAffected(struct strHidDevice* pstrHidDevice); //****************************************************************************** // //! \brief Open a HID Device. //! //! This function opens the HID device associated with the HID interface //! 'deviceIndex' (0-7), on the physical device described by the VID, //! PID, and serial number. //! \param pstrHidDevice Structure which contains important data of an HID //! device //! \param vid Vendor-ID of the device //! \param pid Product-ID of the device //! \param deviceIndex Index of the device.If only one HID is connected, //! deviceIndex is 0. //! - Starts with zero //! - Maximum value is (HID_GetNumOfInterfaces() - 1) //! \param serialNumber Serial number of device to be opened. //! \param totalDevNum Total number of interfaces associated with the //! serial number //! \param totalSerNum Total number of physical devices associated with //! the VID/PID //! //! \return Returns the error status, as one of //! \n \b HID_DEVICE_SUCCESS //! \n \b HID_DEVICE_NOT_FOUND //! \n \b HID_DEVICE_ALREADY_OPENED // //****************************************************************************** BYTE HID_Open(struct strHidDevice* pstrHidDevice, WORD vid, WORD pid, DWORD deviceIndex, char serialNumber[SERNUM_LEN], DWORD totalDevNum, DWORD totalSerNum); //****************************************************************************** // //! \brief Reads a data stream from the given HID device. //! //! Prefixed report ID will be skipped. //! //! \param pstrHidDevice Structure which contains important data of an HID //! device //! \param buffer Pointer to buffer in which will be written //! \param bufferSize Number of bytes to read //! \param bytesReturned Number of actual read bytes //! //! \return Returns the error status, as one of //! \n \b HID_DEVICE_SUCCESS //! \n \b HID_DEVICE_NOT_OPENED //! \n \b HID_DEVICE_TRANSFER_TIMEOUT //! \n \b HID_DEVICE_TRANSFER_FAILED // //****************************************************************************** BYTE HID_ReadFile(struct strHidDevice* pstrHidDevice, BYTE* buffer, DWORD bufferSize, DWORD* bytesReturned); //****************************************************************************** // //! \brief Registers a device for program Windows notification. //! //! Registers the window pointed to by handle hWnd to receive //! notification when devices are added or removed from the system. //! //! \param hWnd Windows handle //! \param diNotifyHandle Device notification handle pointer address //! //! \return Returns the error status, as one of //! \n \b HID_DEVICE_SUCCESS //! \n \b HID_DEVICE_HANDLE_ERROR // //****************************************************************************** BYTE HID_RegisterForDeviceNotification(HWND hWnd, HDEVNOTIFY* diNotifyHandle); //****************************************************************************** // //! \brief Un-Registers a device from Windows notification. //! //! Un-registers the window pointed to by handle hWnd to receive //! notification when devices are added or removed from the system. //! //! \param diNotifyHandle: Device notification handle pointer address. //! //! \return Returns the error status, as one of //! \n \b HID_DEVICE_SUCCESS //! \n \b HID_DEVICE_HANDLE_ERROR // //****************************************************************************** BYTE HID_UnRegisterForDeviceNotification(HDEVNOTIFY* diNotifyHandle); //****************************************************************************** // //! \brief Writes a data stream to the given HID device. //! //! Needed report IDs will be generated automatically. //! //! \param pstrHidDevice Structure which contains important data of an HID //! device //! \param buffer Buffer which will be send //! \param bufferSize Number of bytes to send //! //! \return Returns the error status, as one of //! \n \b HID_DEVICE_SUCCESS //! \n \b HID_DEVICE_NOT_OPENED //! \n \b HID_DEVICE_TRANSFER_TIMEOUT //! \n \b HID_DEVICE_TRANSFER_FAILED // //****************************************************************************** BYTE HID_WriteFile(struct strHidDevice* pstrHidDevice, BYTE* buffer, DWORD bufferSize);

2,644

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧