⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hid.cs

📁 usbio using microsoft visual c# example
💻 CS
📖 第 1 页 / 共 3 页
字号:
		
		internal HidApiDeclarations.HIDP_CAPS GetDeviceCapabilities (int hidHandle)
		{
			
			// Purpose    : Retrieves a structure with information about a device's capabilities.
			// Accepts    : HIDHandle - a handle to a device.
			// Returns    : An HIDP_CAPS structure.
			
			byte[] PreparsedDataBytes = new byte[30];
			string PreparsedDataString;
			IntPtr PreparsedDataPointer = new IntPtr();
			int Result;
			bool Success = false;
			byte[] ValueCaps = new byte[1024]; // (the array size is a guess)
			
			try {
				
				// ***
				// API function: HidD_GetPreparsedData
				// Purpose: retrieves a pointer to a buffer containing information about the device's capabilities.
				// HidP_GetCaps and other API functions require a pointer to the buffer.
				// Requires:
				// A handle returned by CreateFile.
				// A pointer to a buffer.
				// Returns:
				// True on success, False on failure.
				// ***
				
				Success = HidApiDeclarations.HidD_GetPreparsedData(hidHandle, ref PreparsedDataPointer);
				
				Debug.WriteLine(MyDebugging.ResultOfAPICall("HidD_GetPreparsedData"));
				Debug.WriteLine("");
				
				// Copy the data at PreparsedDataPointer into a byte array.
				
				PreparsedDataString = System.Convert.ToBase64String(PreparsedDataBytes);
				
				
				// ***
				// API function: HidP_GetCaps
				// Purpose: find out a device's capabilities.
				// For standard devices such as joysticks, you can find out the specific
				// capabilities of the device.
				// For a custom device where the software knows what the device is capable of,
				// this call may be unneeded.
				// Accepts:
				// A pointer returned by HidD_GetPreparsedData
				// A pointer to a HIDP_CAPS structure.
				// Returns: True on success, False on failure.
				// ***
				
				Result = HidApiDeclarations.HidP_GetCaps(PreparsedDataPointer, ref Capabilities);
				if (Result != 0) {
					//  Debug data:
					Debug.WriteLine(MyDebugging.ResultOfAPICall("HidP_GetCaps"));
					
					Debug.WriteLine("");
					
					Debug.WriteLine("  Usage: " + Capabilities.Usage.ToString("x"));
					Debug.WriteLine("  Usage Page: " + Capabilities.UsagePage.ToString("x"));
					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 function: HidP_GetValueCaps
					// Purpose: retrieves a buffer containing an array of HidP_ValueCaps structures.
					// Each structure defines the capabilities of one value.
					// This application doesn't use this data.
					// Accepts:
					// A report type enumerator from hidpi.h,
					// A pointer to a buffer for the returned array,
					// The NumberInputValueCaps member of the device's HidP_Caps structure,
					// A pointer to the PreparsedData structure returned by HidD_GetPreparsedData.
					// Returns: True on success, False on failure.
					// ***
					
					Result = HidApiDeclarations.HidP_GetValueCaps(HidApiDeclarations.HidP_Input, ref ValueCaps[0], ref Capabilities.NumberInputValueCaps, PreparsedDataPointer);
					
					Debug.WriteLine(MyDebugging.ResultOfAPICall("HidP_GetValueCaps"));
					Debug.WriteLine("");
					
					// (To use this data, copy the ValueCaps byte array into an array of structures.)
					// ***
					// API function: HidD_FreePreparsedData
					// Purpose: frees the buffer reserved by HidD_GetPreparsedData.
					// Accepts: A pointer to the PreparsedData structure returned by HidD_GetPreparsedData.
					// Returns: True on success, False on failure.
					// ***
					
					Success = HidApiDeclarations.HidD_FreePreparsedData(ref PreparsedDataPointer);
					
					Debug.WriteLine(MyDebugging.ResultOfAPICall("HidD_FreePreparsedData"));
					Debug.WriteLine("");
				}
				
			} catch (Exception ex) {
				HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
			}
      
      return Capabilities;
		}
		
		
		internal bool GetNumberOfInputBuffers (int hidDeviceObject, ref int numberOfInputBuffers)
		{
			
			// Purpose    : Retrieves the number of Input reports the host can store.
			// Accepts    : hidDeviceObject - a handle to a device
			//            : numberBuffers - an integer to hold the returned value.
			// Returns    : True on success, False on failure.
			
			bool Success = false;
			
			try {
				
				if (!(IsWindows98Gold())) {
					// ***
					// API function: HidD_GetNumInputBuffers
					// Purpose: retrieves the number of Input reports the host can store.
					// Not supported by Windows 98 Gold.
					// If the buffer is full and another report arrives, the host drops the
					// oldest report.
					// Accepts: a handle to a device and an integer to hold the number of buffers.
					// Returns: True on success, False on failure.
					// ***
					
					Success = HidApiDeclarations.HidD_GetNumInputBuffers(hidDeviceObject, ref numberOfInputBuffers);
					
				} else {
					
					// Under Windows 98 Gold, the number of buffers is fixed at 2.
					numberOfInputBuffers = 2;
					Success = true;
				}
				
			} catch (Exception ex) {
				HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
			}
      
      return Success;
		}
		
		
		internal bool SetNumberOfInputBuffers (int hidDeviceObject, int numberBuffers)
		{
			
			// Purpose    : sets the number of input reports the host will store.
			//            : Requires Windows XP or later.
			// Accepts    : hidDeviceObject - a handle to the device.
			//            : numberBuffers - the requested number of input reports.
			// Returns    : True on success. False on failure.
			
			bool Success = false;
			
			try {
				
				if (!(IsWindows98Gold())){
					// ***
					// API function: HidD_SetNumInputBuffers
					// Purpose: Sets the number of Input reports the host can store.
					// If the buffer is full and another report arrives, the host drops the
					// oldest report.
					// Requires:
					// A handle to a HID
					// An integer to hold the number of buffers.
					// Returns: true on success, false on failure.
					// ***
					
					Success = HidApiDeclarations.HidD_SetNumInputBuffers(hidDeviceObject, numberBuffers);
					
				} else {
					// Not supported under Windows 98 Gold.
					Success = false;
				}
				
			} catch (Exception ex) {
				HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
			}
      
      return Success;
		}
		
		
		internal bool IsWindowsXpOrLater()
		{
			
			// Find out if the current operating system is Windows XP or later.
			// (Windows XP or later is required for HidD_GetInputReport and HidD_SetInputReport.)
			bool Success = false;

			try {
				OperatingSystem MyEnvironment = Environment.OSVersion;
				
				// Windows XP is version 5.1.
				System.Version VersionXP = new System.Version(5, 1);
				
				if (MyEnvironment.Version >= VersionXP) {
					Debug.Write("The OS is Windows XP or later.");
					Success = true;
				} else {
					Debug.Write("The OS is earlier than Windows XP.");
					Success = false;
				}
				
			} catch (Exception ex) {
				HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
			}
			
      return Success;
		}
		
		internal bool IsWindows98Gold()
		{
			
			// Find out if the current operating system is Windows 98 Gold (original version).
			// Windows 98 Gold does not support the following:
			//   Interrupt OUT transfers (WriteFile uses control transfers and Set_Report).
			//   HidD_GetNumInputBuffers and HidD_SetNumInputBuffers
			
			// (Not yet tested on a Windows 98 Gold system.)
			bool Success = false;

			try {
				OperatingSystem MyEnvironment = Environment.OSVersion;
				
				// Windows 98 Gold is version 4.10 with a build number less than 2183.
				System.Version Version98SE = new System.Version(4, 10, 2183);
				
				if (MyEnvironment.Version >= Version98SE) {
					Debug.Write("The OS is Windows 98 Gold.");
					Success = true;
				} else {
					Debug.Write("The OS is more recent than Windows 98 Gold.");
					Success = false;
				}
				
			} catch (Exception ex) {
				HandleException(ModuleName + ":" + System.Reflection.MethodBase.GetCurrentMethod(), ex);
			}

      return Success;
		}
		
		static public void HandleException (string moduleName, Exception e)
		{
			
			// Purpose    : Provides a central mechanism for exception handling.
			//            : Displays a message box that describes the exception.
			
			// Accepts    : moduleName - the module where the exception occurred.
			//            : e - the exception
			
			string Message;
			string Caption;
			
			try {
				// Create an error message.
				Message = "Exception: " + e.Message + Environment.NewLine + "Module: " + moduleName + Environment.NewLine + "Method: " + e.TargetSite.Name;
				
				// Specify a caption.
				Caption = "Unexpected Exception";
				
				// Display the message in a message box.
				MessageBox.Show(Message, Caption, MessageBoxButtons.OK);
				Debug.Write(Message);
			} finally { }

		}
		
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -