/// <summary> /// Retrieves the number of Input reports the host can store. /// </summary> /// /// <param name="hidDeviceObject"> a handle to a device </param> /// <param name="numberOfInputBuffers"> an integer to hold the returned value. </param> /// /// <returns> /// True on success, False on failure. /// </returns> public Boolean GetNumberOfInputBuffers(SafeFileHandle hidDeviceObject, ref Int32 numberOfInputBuffers) { Boolean success = false; try { if (!((DeviceManagement.IsWindows98Gold()))) { // *** // 调用API函数: HidD_GetNumInputBuffers success = HidDeclarations.HidD_GetNumInputBuffers(hidDeviceObject, ref numberOfInputBuffers); Debug.WriteLine(this.MyDebugging.ResultOfAPICall("HidD_GetNumInputBuffers")); } else { // Under Windows 98 Gold, the number of buffers is fixed at 2. numberOfInputBuffers = 2; success = true; } return(success); } catch (Exception ex) { DisplayException(MODULE_NAME, ex); throw; } }
/// <summary> /// 检索设备的参数性能信息,存储到结构中 /// </summary> /// <param name="hidHandle"> 设备句柄 </param> /// <returns> /// 一个 HIDP_CAPS structure结构. /// </returns> public HidDeclarations.HIDP_CAPS GetDeviceCapabilities(SafeFileHandle hidHandle) { IntPtr preparsedData = new System.IntPtr(); Int32 result = 0; Boolean success = false; try { success = HidDeclarations.HidD_GetPreparsedData(hidHandle, ref preparsedData); Debug.WriteLine(this.MyDebugging.ResultOfAPICall("HidD_GetPreparsedData")); // 调用API函数: HidP_GetCaps result = HidDeclarations.HidP_GetCaps(preparsedData, ref Capabilities); Debug.WriteLine(this.MyDebugging.ResultOfAPICall("HidP_GetCaps")); if ((result != 0)) { Debug.WriteLine(""); Debug.WriteLine(" Usage: " + Convert.ToString(Capabilities.Usage, 16)); Debug.WriteLine(" Usage Page: " + Convert.ToString(Capabilities.UsagePage, 16)); Debug.WriteLine(" Input Report Byte Length: " + Capabilities.InputReportByteLength); Debug.WriteLine(" Output Report Byte Length: " + Capabilities.OutputReportByteLength); Debug.WriteLine(" Feature Report Byte Length: " + Capabilities.FeatureReportByteLength); Debug.WriteLine(" Number of Link Collection Nodes: " + Capabilities.NumberLinkCollectionNodes); Debug.WriteLine(" Number of Input Button Caps: " + Capabilities.NumberInputButtonCaps); Debug.WriteLine(" Number of Input Value Caps: " + Capabilities.NumberInputValueCaps); Debug.WriteLine(" Number of Input Data Indices: " + Capabilities.NumberInputDataIndices); Debug.WriteLine(" Number of Output Button Caps: " + Capabilities.NumberOutputButtonCaps); Debug.WriteLine(" Number of Output Value Caps: " + Capabilities.NumberOutputValueCaps); Debug.WriteLine(" Number of Output Data Indices: " + Capabilities.NumberOutputDataIndices); Debug.WriteLine(" Number of Feature Button Caps: " + Capabilities.NumberFeatureButtonCaps); Debug.WriteLine(" Number of Feature Value Caps: " + Capabilities.NumberFeatureValueCaps); Debug.WriteLine(" Number of Feature Data Indices: " + Capabilities.NumberFeatureDataIndices); // 调用API函数: HidP_GetValueCaps Int32 vcSize = Capabilities.NumberInputValueCaps; Byte[] valueCaps = new Byte[vcSize]; result = HidDeclarations.HidP_GetValueCaps(HidDeclarations.HidP_Input, valueCaps, ref vcSize, preparsedData); Debug.WriteLine(this.MyDebugging.ResultOfAPICall("HidP_GetValueCaps")); } } catch (Exception ex) { DisplayException(MODULE_NAME, ex); throw; } finally { // API function: HidD_FreePreparsedData if (preparsedData != IntPtr.Zero) { success = HidDeclarations.HidD_FreePreparsedData(preparsedData); Debug.WriteLine(MyDebugging.ResultOfAPICall("HidD_FreePreparsedData")); } } return(Capabilities); }
/// <summary> /// 清空等待中的报告输入队列 /// </summary> /// <param name="hidHandle">设备句柄</param> /// <returns> /// 成功:true, 失败:false. /// </returns> public Boolean FlushQueue(SafeFileHandle hidHandle) { Boolean success = false; try { // 调用API函数: HidD_FlushQueue success = HidDeclarations.HidD_FlushQueue(hidHandle); Debug.WriteLine(MyDebugging.ResultOfAPICall("HidD_FlushQueue")); return(success); } catch (Exception ex) { DisplayException(MODULE_NAME, ex); throw; } }
/// <summary> /// reads an Input report from the device using a control transfer. /// </summary> /// /// <param name="hidHandle"> the handle for learning about the device and exchanging Feature reports. </param> /// <param name="myDeviceDetected"> tells whether the device is currently attached. </param> /// <param name="inputReportBuffer"> contains the requested report. </param> /// <param name="success"> read success </param> public Boolean GetInputReportViaControlTransfer(SafeFileHandle hidHandle, ref Byte[] inputReportBuffer) { Boolean success; try { // 调用API 函数: HidD_GetInputReport success = HidDeclarations.HidD_GetInputReport(hidHandle, inputReportBuffer, inputReportBuffer.Length + 1); Debug.WriteLine(MyDebugging.ResultOfAPICall("HidD_GetInputReport")); return(success); } catch (Exception ex) { DisplayException(MODULE_NAME, ex); throw; } }
/// <summary> /// Writes an Output report to the device using a control transfer. /// </summary> /// /// <param name="outputReportBuffer"> contains the report ID and report data. </param> /// <param name="hidHandle"> handle to the device. </param> /// /// <returns> /// True on success. False on failure. /// </returns> public Boolean SendOutputReportViaControlTransfer(SafeFileHandle hidHandle, Byte[] outputReportBuffer) { Boolean success = false; try { // 调用API函数: HidD_SetOutputReport success = HidDeclarations.HidD_SetOutputReport(hidHandle, outputReportBuffer, outputReportBuffer.Length + 1); Debug.WriteLine(this.MyDebugging.ResultOfAPICall("HidD_SetOutputReport")); return(success); } catch (Exception ex) { DisplayException(MODULE_NAME, ex); throw; } }
/// <summary> /// reads a Feature report from the device. /// </summary> /// /// <param name="hidHandle"> the handle for learning about the device and exchanging Feature reports. </param> /// <param name="myDeviceDetected"> tells whether the device is currently attached.</param> /// <param name="inFeatureReportBuffer"> contains the requested report.</param> /// <param name="success"> read success</param> public Boolean GetFeatureReport(SafeFileHandle hidHandle, ref Byte[] inFeatureReportBuffer) { Boolean success; try { // *** // API function: HidD_GetFeature success = HidDeclarations.HidD_GetFeature(hidHandle, inFeatureReportBuffer, inFeatureReportBuffer.Length); Debug.WriteLine(this.MyDebugging.ResultOfAPICall("HidD_GetFeature")); return(success); } catch (Exception ex) { DisplayException(MODULE_NAME, ex); throw; } }
/// <summary> /// writes a Feature report to the device. /// </summary> /// /// <param name="outFeatureReportBuffer"> contains the report ID and report data. </param> /// <param name="hidHandle"> handle to the device. </param> /// /// <returns> /// True on success. False on failure. /// </returns> public Boolean SendFeatureReport(SafeFileHandle hidHandle, Byte[] outFeatureReportBuffer) { Boolean success = false; try { // *** // 调用API函数: HidD_SetFeature success = HidDeclarations.HidD_SetFeature(hidHandle, outFeatureReportBuffer, outFeatureReportBuffer.Length); Debug.WriteLine(this.MyDebugging.ResultOfAPICall("HidD_SetFeature")); return(success); } catch (Exception ex) { DisplayException(MODULE_NAME, ex); throw; } }
/// <summary> /// sets the number of input reports the host will store. /// Requires Windows XP or later. /// </summary> /// /// <param name="hidDeviceObject"> a handle to the device.</param> /// <param name="numberBuffers"> the requested number of input reports. </param> /// /// <returns> /// True on success. False on failure. /// </returns> public Boolean SetNumberOfInputBuffers(SafeFileHandle hidDeviceObject, Int32 numberBuffers) { try { if (!DeviceManagement.IsWindows98Gold()) { // *** // 调用API函数: HidD_SetNumInputBuffers HidDeclarations.HidD_SetNumInputBuffers(hidDeviceObject, numberBuffers); Debug.WriteLine(this.MyDebugging.ResultOfAPICall("HidD_SetNumInputBuffers")); return(true); } else { // Not supported under Windows 98 Gold. return(false); } } catch (Exception ex) { DisplayException(MODULE_NAME, ex); throw; } }
/// <summary> /// 根据给定的VendorID和ProductID查找设备,并将其事件注册到窗体. /// </summary> /// <returns> /// 探测到返回True,没有返回False /// </returns> public Boolean findHidDevices(ref int myVendorID, ref int myProductID)//, Form FrmMy { Boolean deviceFound = false; String[] devicePathName = new String[128]; String functionName = ""; Guid hidGuid = Guid.Empty; Int32 memberIndex = 0; Boolean success = false; // 检查是否在重入 if (this._findHidIsBusy == true) { return(false); } else { this._findHidIsBusy = true; } if (this._deviceVID != myVendorID) { this._deviceVID = myVendorID; } if (this._devicePID != myVendorID) { this._devicePID = myProductID; } try { DeviceInformation device = new DeviceInformation(); device.DeviceIsDetected = false; // CloseCommunications(); // 调用API 函数: 'HidD_GetHidGuid HidDeclarations.HidD_GetHidGuid(ref hidGuid); Debug.WriteLine("在函数FindTheHid中" + this.MyDebugging.ResultOfAPICall("HidD_GetHidGuid")); // 获取连接的HID路径名称数组 deviceFound = this.FindDeviceFromGuid(hidGuid, ref devicePathName); // 如果至少有一个HID,获取每个设备的Vendor ID,Product ID,直到发现一个或者所有设备 if (deviceFound) { memberIndex = 0; do { // 调用API函数CreateFile if (devicePathName[memberIndex] != null && this[devicePathName[memberIndex]] != null) { if (this[devicePathName[memberIndex]] != null && this[devicePathName[memberIndex]].DeviceIsDetected) { memberIndex++; continue; // 继续查找下一个设备 } else { device = this[devicePathName[memberIndex]]; } } // 在不使用Read/write权限的情况下打开句柄,以获取HID的信息, 甚至系统键盘鼠标等 device.HidHandle = FileIODeclarations.CreateFile(devicePathName[memberIndex], 0, FileIODeclarations.FILE_SHARE_READ | FileIODeclarations.FILE_SHARE_WRITE, IntPtr.Zero, FileIODeclarations.OPEN_EXISTING, 0, 0); Debug.WriteLine("在函数FindTheHid中" + this.MyDebugging.ResultOfAPICall("CreateFile")); if (!device.HidHandle.IsInvalid) { // 返回的句柄是合法的,确定此设备是否我们要找的设备 // 设置DeviceAttributes中的数据字节结构 _hidObject.DeviceAttributes.Size = Marshal.SizeOf(_hidObject.DeviceAttributes); // 调用API函数 HidD_GetAttributes success = HidDeclarations.HidD_GetAttributes(device.HidHandle, ref _hidObject.DeviceAttributes); Debug.WriteLine("在函数FindTheHid中" + this.MyDebugging.ResultOfAPICall("HidD_GetAttributes")); if (success) { //Debug.WriteLine(" HIDD_ATTRIBUTES 结构被成功填充."); //Debug.WriteLine(" 结构大小: " + MyHid.DeviceAttributes.Size); //Debug.WriteLine(" Vendor ID: " + Convert.ToString(MyHid.DeviceAttributes.VendorID, 16)); //Debug.WriteLine(" Product ID: " + Convert.ToString(MyHid.DeviceAttributes.ProductID, 16)); //Debug.WriteLine(" 版本号: " + Convert.ToString(MyHid.DeviceAttributes.VersionNumber, 16)); // 确定是否我们要找的设备 if ((_hidObject.DeviceAttributes.VendorID == myVendorID) && (_hidObject.DeviceAttributes.ProductID == myProductID)) { // 窗口列表框中显示设备信息 Debug.WriteLine(" 探测到设备:" + " Vendor ID = " + Convert.ToString(_hidObject.DeviceAttributes.VendorID, 16) + " Product ID = " + Convert.ToString(_hidObject.DeviceAttributes.ProductID, 16)); device.DeviceIsDetected = true; // 为OnDeviceChange()函数保存设备路径名 if (this[devicePathName[memberIndex]] == null) { device.myDevicePathName = devicePathName[memberIndex]; device.DeviceAttributes = _hidObject.DeviceAttributes; this._deviceList.Add(device); device = new DeviceInformation(); _hidObject = new Hid(); device.DeviceIsDetected = false; Debug.WriteLine("将设备" + device.myDevicePathName + "加入到设备列表中"); } } else { // 如果不匹配关闭设备句柄 device.DeviceIsDetected = false; device.HidHandle.Close(); } } else { // 提示获取设备信息出现问题. Debug.WriteLine("Error in filling HIDD_ATTRIBUTES structure."); device.DeviceIsDetected = false; device.HidHandle.Close(); } } // 继续查找,直到我们发现给定设备或者检查完成 memberIndex = memberIndex + 1; }while (!((device.DeviceIsDetected || (memberIndex == devicePathName.Length)))); } if (this._deviceList.Count > 0) { for (int i = 0; i < this._deviceList.Count; i++) { DeviceInformation findDevice = this._deviceList[i]; if (findDevice.Capabilities.InputReportByteLength > 0) { continue; } // 如果探测到设备 // 注册关注设备事件 Register to receive notifications if the device is removed or attached. success = this.RegisterForDeviceNotifications(findDevice.myDevicePathName, this.Handle /*FrmMy.Handle*/, hidGuid, ref findDevice.DeviceNotificationHandle); Debug.WriteLine("RegisterForDeviceNotifications = " + success); // 获取设备信息 findDevice.Capabilities = _hidObject.GetDeviceCapabilities(findDevice.HidHandle); if (success) { // 确定是否鼠标或键盘设备 findDevice.HidUsage = _hidObject.GetHidUsage(_hidObject.Capabilities); // 获取报告的缓冲区大小 GetInputReportBufferSize(findDevice); this.ReOpenDeviceFileStreamHandler(findDevice); } } for (int i = this._deviceList.Count - 1; i >= 0; i--) { if (!this._deviceList[i].DeviceIsDetected) { this._deviceList.RemoveAt(i); } } } else { // 没有检测到设备 Debug.WriteLine("没发现设备."); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } this._findHidIsBusy = false; return(this._deviceList.Count > 0); }