阅读:1533回复:4
为什么USB应用程序中不能对设备进行写的操作
Result = WriteFile(hiddevice, SendBuffer(0), CLng(Capabilities.OutputReportByteLength), _
NumberOfBytesWritten, 0) 参数都有效,但结果就是失败,NumberOfBytesWritten为0 是不是与写入设备的数据格式有关啊, USB KEYBOARD有专门的格式吗? |
|
沙发#
发布于:2004-07-22 14:28
不好意思,我问你个问题,好吗?
我想问你是怎么得到 USB 键盘的,为何我用下面的代码 老是返回错误呢? BOOL OpenUserKeyboard () { // Search through the attached HID devices for the User Keyboard // I used a keyboard with a VID = 046E and PID = 6782, your's may be different // See TKFILTER.SYS documentation which describes how to create a "User Keyboard" // Declare the local data structures used struct _GUID HidGuid; SP_INTERFACE_DEVICE_DATA DeviceInterfaceData; struct { DWORD cbSize; char DevicePath[256]; } FunctionClassDeviceData; int Success, HidDevice; BOOL Openned; HANDLE PnPHandle; unsigned long BytesReturned; struct _HIDD_ATTRIBUTES Attributes; struct _HIDP_PREPARSED_DATA *PreparsedData; struct _HIDP_CAPS Capabilities; // First, get my class identifier HidD_GetHidGuid(&HidGuid); // Get a handle for the Plug and Play node and request currently active HID devices PnPHandle = SetupDiGetClassDevs(&HidGuid,0,0,0x12); if ((int)PnPHandle == -1) return DisplayError("Could not attach to PnP node"); Openned = false; // Lets look for a maximum of 20 HID devices for (HidDevice = 0; (HidDevice < 20) && !Openned; HidDevice++) { // Give the user some feedback MainWindow[MainWindowIndex++] = 45; // "-" InvalidateRect(hWnd, NULL, 1); // Initialize our Data DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA ); // Length of data structure in bytes // Is there a HID device at this table entry Success = SetupDiEnumDeviceInterfaces(PnPHandle, 0, &HidGuid, HidDevice, &DeviceInterfaceData); if (Success == 1) { // There is a device here, get it's name FunctionClassDeviceData.cbSize = 5; ZeroMemory(FunctionClassDeviceData.DevicePath,sizeof(char)*256); Success = SetupDiGetDeviceInterfaceDetail(PnPHandle, &DeviceInterfaceData, PSP_INTERFACE_DEVICE_DETAIL_DATA(&FunctionClassDeviceData), 256, &BytesReturned, 0); if (Success == 0) return DisplayError("Could not find the system name for this HID device"); char atemp[256]; strcpy(atemp,FunctionClassDeviceData.DevicePath); ZeroMemory(FunctionClassDeviceData.DevicePath,sizeof(char)*256); strcpy(FunctionClassDeviceData.DevicePath,"\\"); strcat(FunctionClassDeviceData.DevicePath,"\\"); strcat(FunctionClassDeviceData.DevicePath,"\\"); strcat(FunctionClassDeviceData.DevicePath,"\\"); strcat(FunctionClassDeviceData.DevicePath,"."); strcat(FunctionClassDeviceData.DevicePath,"\\"); strcat(FunctionClassDeviceData.DevicePath,"\\"); for(int iCount=0;iCount<240;iCount++) { FunctionClassDeviceData.DevicePath[7+iCount]=atemp[4+iCount]; } // Can now open this HID device HidHandle = CreateFile(FunctionClassDeviceData.DevicePath, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (HidHandle == INVALID_HANDLE_VALUE) return DisplayError("Could not open HID device");//到这个地方程序返回错误,为何? // Is it OUR HID device? if (!HidD_GetAttributes(HidHandle, &Attributes)) return DisplayError("Could not get VID/PID"); if ((Attributes.VendorID == 0x046e) && (Attributes.ProductID == 0x6782)) { // Note that most keyboards are composite desktop devices, //must find the "keyboard" usage (set to 0 by TKFILTER.SYS) if (!HidD_GetPreparsedData(HidHandle, &PreparsedData)) return DisplayError("Could not get Preparsed Data"); if (!HidP_GetCaps(PreparsedData, &Capabilities)) return DisplayError("Could not get device capabilities"); if ((Capabilities.UsagePage == 1) && (Capabilities.Usage == 0)) Openned = true; else CloseHandle(HidHandle); HidD_FreePreparsedData(PreparsedData); } } // if (SetupDiEnumDeviceInterfaces . . } // for (HidDevice = 0; (HidDevice < 20) && !Openned; HidDevice++) CloseHandle(PnPHandle); return Openned; } |
|
板凳#
发布于:2004-07-26 10:16
如果是WIN2K,USB鼠标和键盘好像不能通过访问普通USB设备那样打开
|
|
地板#
发布于:2004-07-27 10:21
不好意思,我问你个问题,好吗? 好象是GUID获取的不对 |
|
地下室#
发布于:2004-07-27 10:39
关注中。。。。
|
|