为什么对USB(HID)接口的设备WriteFile()返回都是0?
void CVCUSBHIDDlg::OnBnClickedButton1(){ // TODO: 在此添加控件通知处理程序代码 ::HANDLE m_Handle = INVALID_HANDLE_VALUE ; WORD wVID = 4292; WORD wPID = 33896; OpenDevice( m_Handle,wVID,wPID); if(m_Handle == INVALID_HANDLE_VALUE) return; BYTE temp[6] ={0x32,0x33,0x34,0x35,0x36,0x37}; if(WriteDevice(m_Handle,&temp,6) == TRUE) { AfxMessageBox(_T("Test")); }}// 打开设备BOOL CVCUSBHIDDlg::OpenDevice(HANDLE & handle, WORD wVID, WORD wPID){ BOOL bRet = false; ::GUID hidGuid; ::HDEVINFO hardwareDeviceInfo; ::SP_INTERFACE_DEVICE_DATA deviceInfoData; ::PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL; ::ULONG predictedLength = 0; ::ULONG requiredLength = 0; //::CloseHandle(handle); handle = INVALID_HANDLE_VALUE; deviceInfoData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA); ::HidD_GetHidGuid(&hidGuid); hardwareDeviceInfo = ::SetupDiGetClassDevs(&hidGuid,NULL,NULL,(DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)); for(int i=0;i<128;i++) { if (!SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, 0, &hidGuid, i, &deviceInfoData)) continue; SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo, &deviceInfoData, NULL, 0, &requiredLength, NULL); predictedLength = requiredLength; functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(predictedLength); if (!functionClassDeviceData) continue; functionClassDeviceData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfo, &deviceInfoData, functionClassDeviceData, predictedLength, &requiredLength, NULL)) break; handle = CreateFile(functionClassDeviceData->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (handle != INVALID_HANDLE_VALUE) { HIDD_ATTRIBUTES attri; HidD_GetAttributes(handle, &attri); // CString strAttributes; // strAttributes.Format(_T("VID:%d;PID:%d"),attri.ProductID,attri.VendorID); // AfxMessageBox(strAttributes); if ((attri.VendorID == wVID) && (attri.ProductID == wPID)) { AfxMessageBox(_T("Find")); bRet = TRUE; break; } CloseHandle(handle); handle = INVALID_HANDLE_VALUE; } } SetupDiDestroyDeviceInfoList(hardwareDeviceInfo); return bRet;}// 写操作BOOL CVCUSBHIDDlg::WriteDevice(HANDLE handle , LPCVOID lpBuffer , DWORD dwSize){ BYTE wBuffer[8] = {0}; DWORD dwRet; BOOL bRet; wBuffer[0] = 0x01; wBuffer[1] = 0x00; memcpy(&wBuffer[2], lpBuffer, min(6, dwSize)); bRet = WriteFile(handle, wBuffer, 8, &dwRet, NULL); return bRet;}
提供给请求操作的用户缓冲区无效。
[解决办法]
USB设备WriteFile的时候要看设备的报告描述符是怎样写的