//---#+************************************************************************ //---NOTATION: //- int CT_HidP_GetCaps(int pPreparsedData) //- //--- DESCRIPTION: //-- Gets the capabilities report // // Autor: F.L. //-*************************************************************************+#* public unsafe int CT_HidP_GetCaps(int pPreparsedData) { myHIDP_CAPS = new HIDP_CAPS(); return(HidP_GetCaps( pPreparsedData, ref myHIDP_CAPS)); }
/// <summary> /// Gets the capabilities report /// </summary> /// <param name="pPreparsedData"></param> /// <returns></returns> internal bool CT_HidP_GetCaps(IntPtr pPreparsedData) { myHIDP_CAPS = new HIDP_CAPS(); return(HidP_GetCaps( pPreparsedData, ref myHIDP_CAPS)); }
/// <summary> /// 得到输入ID /// </summary> /// <param name="handle"></param> /// <param name="outputValueCaps"></param> /// <returns></returns> public unsafe bool GetDeviceCapabilities(int handle, ref string outputValueCaps, ref string outputLength) { HIDP_CAPS capabilities = new HIDP_CAPS(); int PreparsedDataPointer = 0; int Result; try { Result = HidD_GetPreparsedData(handle, ref PreparsedDataPointer); Result = HidP_GetCaps(PreparsedDataPointer, ref capabilities); if (Result != 0) { outputValueCaps = capabilities.NumberOutputValueCaps.ToString(); outputLength = capabilities.OutputReportByteLength.ToString(); return(true); } else { return(false); } } catch { return(false); } }
/// <summary> /// Creates a 32-bit Usage from the Usage Page and Usage ID. /// Determines whether the Usage is a system mouse or keyboard. /// Can be modified to detect other Usages. /// </summary> /// /// <param name="MyCapabilities"> a HIDP_CAPS structure retrieved with HidP_GetCaps. </param> /// /// <returns> /// A String describing the Usage. /// </returns> internal String GetHidUsage(HIDP_CAPS MyCapabilities) { Int32 usage = 0; String usageDescription = ""; try { // Create32-bit Usage from Usage Page and Usage ID. usage = MyCapabilities.UsagePage * 256 + MyCapabilities.Usage; if (usage == Convert.ToInt32(0X102)) { usageDescription = "mouse"; } if (usage == Convert.ToInt32(0X106)) { usageDescription = "keyboard"; } } catch (Exception ex) { DisplayException(MODULE_NAME, ex); throw; } return(usageDescription); }
public HidDeviceCapabilities GetDeviceCapabilities(IDeviceHandle deviceHandle) { var safeHandle = (deviceHandle as DeviceHandle).SafeFileHandle; IntPtr preparsedData = new IntPtr(); HidD_GetPreparsedData(safeHandle, ref preparsedData); HIDP_CAPS caps = new HIDP_CAPS(); int result = HidP_GetCaps(preparsedData, ref caps); if (preparsedData != IntPtr.Zero) { HidD_FreePreparsedData(preparsedData); } HIDD_ATTRIBUTES attrib = new HIDD_ATTRIBUTES(); attrib.Size = Marshal.SizeOf(attrib); HidD_GetAttributes(safeHandle, ref attrib); return(new HidDeviceCapabilities { Usage = (ushort)caps.Usage, UsagePage = (ushort)caps.UsagePage, InputReportByteLength = (ushort)caps.InputReportByteLength, OutputReportByteLength = (ushort)caps.OutputReportByteLength, VersionNumber = (ushort)attrib.VersionNumber }); }
private static List <ushort> GetPressedButtons(HIDP_CAPS hidCaps, IntPtr preparsedData, byte[] rawInputData) { var buttonCapsLength = hidCaps.NumberInputButtonCaps; var buttonCaps = new HIDP_BUTTON_CAPS[buttonCapsLength]; CheckError(HidP_GetButtonCaps(HIDP_REPORT_TYPE.HidP_Input, buttonCaps, ref buttonCapsLength, preparsedData)); var usagePages = new HashSet <ushort>(); foreach (var bc in buttonCaps) { usagePages.Add(bc.UsagePage); } var res = new List <ushort>(); foreach (var usagePage in usagePages) { int usageListLength = hidCaps.NumberInputButtonCaps; var usageList = new ushort[usageListLength]; CheckError(HidP_GetUsages(HIDP_REPORT_TYPE.HidP_Input, usagePage, 0, usageList, ref usageListLength, preparsedData, rawInputData, rawInputData.Length)); for (var i = 0; i < usageListLength; ++i) { res.Add(usageList[i]); } } return(res); }
private bool GetDeviceCapabilities() { IntPtr preparsedDataPointer = IntPtr.Zero; try { if (HidD_GetPreparsedData(FileHandle, ref preparsedDataPointer)) { HIDP_CAPS deviceCapabilities = new HIDP_CAPS(); HidP_GetCaps(preparsedDataPointer, ref deviceCapabilities); Capabilities = new HidDeviceCapabilities(deviceCapabilities); return(true); } else { Debug.WriteLine("Failed to get device capabilities."); return(false); } } catch (Exception ex) { Debug.WriteLine("Failed to get device capabilities: " + ex.Message); return(false); } finally { if (preparsedDataPointer != IntPtr.Zero) { HidD_FreePreparsedData(preparsedDataPointer); } } }
public Ps3UsbDeviceInterface(Ps3UsbDeviceInfo deviceInfo) { _deviceInfo = Guard.NotNull(deviceInfo, nameof(deviceInfo)); var fileHandle = INVALID_HANDLE_VALUE; var preparsedData = INVALID_HANDLE_VALUE; var caps = new HIDP_CAPS(); var success = false; try { fileHandle = CreateFile( deviceInfo.DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, IntPtr.Zero ); if (fileHandle == INVALID_HANDLE_VALUE) { CheckError(false); } CheckError(HidD_GetPreparsedData(fileHandle, out preparsedData)); var status = HidP_GetCaps(preparsedData, caps); if (status != NTSTATUS.HIDP_STATUS_SUCCESS) { throw new Win32Exception($"HidP_GetCaps returned non-success status code {status}."); } _fileHandle = fileHandle; _caps = caps; _fileStream = new FileStream(new SafeFileHandle(_fileHandle, false), FileAccess.ReadWrite, 4096, true); _inputBuffer = new byte[caps.InputReportByteLength]; _inputData = new Ps3InputData(_inputBuffer); success = true; } finally { if (preparsedData != INVALID_HANDLE_VALUE) { HidD_FreePreparsedData(preparsedData); } if (!success) { if (fileHandle != INVALID_HANDLE_VALUE) { CloseHandle(fileHandle); } } } }
public HidDevice(string path, HIDD_ATTRIBUTES attributes, HIDP_CAPS caps, SafeFileHandle handle) { this.Path = path; this.Attributes = attributes; this.Caps = caps; this.Handle = handle; this.Stream = new FileStream(this.Handle, FileAccess.ReadWrite, 4096, true); }
private int InitKeyboard() { bool flag = true; int num1 = 0; Guid guid = new Guid("4d1e55b2-f16f-11cf-88cb-001111000030"); IntPtr classDevs = Native.SetupDiGetClassDevs(ref guid, 0, IntPtr.Zero, 18); classDevs.ToInt32(); int memberIndex = 0; while (flag) { Native.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new Native.SP_DEVICE_INTERFACE_DATA(); flag = Native.SetupDiEnumDeviceInterfaces(classDevs, (Native.SP_DEVINFO_DATA)null, ref guid, memberIndex, deviceInterfaceData); if (flag) { Native.SP_DEVINFO_DATA deviceInfoData = new Native.SP_DEVINFO_DATA(); int requiredSize = 0; Native.SetupDiGetDeviceInterfaceDetail(classDevs, deviceInterfaceData, IntPtr.Zero, 0, ref requiredSize, deviceInfoData); IntPtr num2 = Marshal.AllocHGlobal(requiredSize); Marshal.StructureToPtr((object)new Native.SP_DEVICE_INTERFACE_DETAIL_DATA() { cbSize = (IntPtr.Size != 8 ? 4 + Marshal.SystemDefaultCharSize : 8) }, num2, false); Native.SetupDiGetDeviceInterfaceDetail(classDevs, deviceInterfaceData, num2, requiredSize, ref requiredSize, deviceInfoData); string stringAuto = Marshal.PtrToStringAuto((IntPtr)((int)num2 + Marshal.SizeOf(typeof(int)))); Marshal.FreeHGlobal(num2); if (stringAuto.IndexOf("04d9") > 0 && stringAuto.IndexOf("8008") > 0 || stringAuto.IndexOf("1044") > 0 && stringAuto.IndexOf("7a38") > 0 || stringAuto.IndexOf("1044") > 0 && stringAuto.IndexOf("7a39") > 0) { SECURITY_ATTRIBUTES securityAttributes = new SECURITY_ATTRIBUTES(); securityAttributes.nLength = Marshal.SizeOf((object)securityAttributes); securityAttributes.lpSecurityDescriptor = IntPtr.Zero; securityAttributes.bInheritHandle = 0; IntPtr file = Win32.CreateFile(stringAuto, 3221225472U, 3, IntPtr.Zero, 3, 0, 0); if (file.ToInt32() != -1) { IntPtr PreparsedData = new IntPtr(); Win32.HidD_GetPreparsedData(file, ref PreparsedData); HIDP_CAPS Capabilities = new HIDP_CAPS(); Win32.HidP_GetCaps(PreparsedData, ref Capabilities); if ((int)Capabilities.FeatureReportByteLength > 0) { _deviceHandle_2016 = file; break; } } } else if ((stringAuto.IndexOf("1044") <= 0 || stringAuto.IndexOf("7a03") <= 0) && (stringAuto.IndexOf("1044") <= 0 || stringAuto.IndexOf("7a04") <= 0) && stringAuto.IndexOf("1044") > 0) { stringAuto.IndexOf("7a06"); } ++memberIndex; } } return(num1); }
int GetReportLenth() { int myPtrToPreparsedData = -1; int result1 = HidD_GetPreparsedData(HidHandle, ref myPtrToPreparsedData); HIDP_CAPS myHIDP_CAPS = new HIDP_CAPS(); int result2 = HidP_GetCaps(myPtrToPreparsedData, ref myHIDP_CAPS); int reportLength = myHIDP_CAPS.InputReportByteLength; return(reportLength); }
/// <summary> /// Opens and requests information from USB device /// Creates file stream to carry data. /// </summary> /// <param name="devicePath">The USB device path - from getConnectedDevices</param> private void initDevice(string devicePath) { deviceConnected = false; //create file handle using CT_CreateFile handle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HidD_GetPreparsedData(handle, ref ptrToPreParsedData); capabilities = new HIDP_CAPS(); int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); bool hidAttribSucsess = HidD_GetAttributes(handle, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HidD_GetProductString(handle, buffer, 126)) { productName = Marshal.PtrToStringAuto(buffer); } if (HidD_GetSerialNumberString(handle, buffer, 126)) { SN = Marshal.PtrToStringAuto(buffer); } if (HidD_GetManufacturerString(handle, buffer, 126)) { manfString = Marshal.PtrToStringAuto(buffer); } Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HidD_FreePreparsedData(ref ptrToPreParsedData); //SetupDiDestroyDeviceInfoList(i); if (handle.IsInvalid) { return; } deviceConnected = true; //use a filestream object to bring this stuff into .NET FS = new FileStream(handle, FileAccess.ReadWrite, capabilities.FeatureReportByteLength, false); }
internal bool CT_HidP_GetCaps(IntPtr pPreparsedData) { myHIDP_CAPS = new HIDP_CAPS(); if (HidP_GetCaps(pPreparsedData, ref myHIDP_CAPS)) { unmanagedInputBuffer = Marshal.AllocHGlobal(myHIDP_CAPS.InputReportByteLength); return(true); } else { int err = Marshal.GetLastWin32Error(); return(false); } }
public unsafe bool USBDataWrite(string sendValue) { try { byte[] data = System.Text.ASCIIEncoding.Default.GetBytes(sendValue); //byte[] array = new byte[] { 0x00, 0xce, 0x02, 0x31, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, // }; HID_ATTRIBUTES hid_ATTRIBUTES; HidD_GetAttributes(hDevInfo, out hid_ATTRIBUTES); IntPtr preparseData = new IntPtr(); HIDP_CAPS caps = new HIDP_CAPS(); HidD_GetPreparsedData(hDevInfo, out preparseData); HidP_GetCaps(preparseData, out caps); outputReportLength = caps.OutputReportByteLength; inputReportLenght = caps.InputReportByteLength; hidDevice = new FileStream(new SafeFileHandle(hDevInfo, false), FileAccess.ReadWrite, inputReportLenght, false); Array.Resize(ref data, inputReportLenght); //数据长度由设备决定的 hidDevice.Write(data, 0, data.Length); // USBDataRead((int)hDevInfo); // HidD_FreePreparsedData(hDevInfo); // SetupDiDestroyDeviceInfoList(hDevInfo); // hidDevice.Dispose(); if (inputReportLenght > 0) { return(true); } else { return(false); } } catch { return(false); } }
public HidDeviceCapabilities(HIDP_CAPS capabilities) { UsageGeneric = capabilities.UsageGeneric; UsagePage = capabilities.UsagePage; InputReportByteLength = capabilities.InputReportByteLength; OutputReportByteLength = capabilities.OutputReportByteLength; FeatureReportByteLength = capabilities.FeatureReportByteLength; Reserved = capabilities.Reserved; NumberLinkCollectionNodes = capabilities.NumberLinkCollectionNodes; NumberInputButtonCaps = capabilities.NumberInputButtonCaps; NumberInputValueCaps = capabilities.NumberInputValueCaps; NumberInputDataIndices = capabilities.NumberInputDataIndices; NumberOutputButtonCaps = capabilities.NumberOutputButtonCaps; NumberOutputValueCaps = capabilities.NumberOutputValueCaps; NumberOutputDataIndices = capabilities.NumberOutputDataIndices; NumberFeatureButtonCaps = capabilities.NumberFeatureButtonCaps; NumberFeatureValueCaps = capabilities.NumberFeatureValueCaps; NumberFeatureDataIndices = capabilities.NumberFeatureDataIndices; }
/// <summary> /// /// </summary> public bool USBDataWrite(byte[] data) //hDevInfo { try { // string devicePathName = "\\\\?\\hid#vid_0a12&pid_1111#7&24f3a59d&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"; 蓝牙设备 // hDevInfo = CT_CreateFile(devicePathName); // hDevInfo = CT_CreateFileReadWrite(devicePathName); HID_ATTRIBUTES hid_ATTRIBUTES; HidD_GetAttributes(hDevInfo, out hid_ATTRIBUTES); IntPtr preparseData = new IntPtr(); HIDP_CAPS caps = new HIDP_CAPS(); HidD_GetPreparsedData(hDevInfo, out preparseData); try { HidP_GetCaps(preparseData, out caps); } catch (AccessViolationException ex) { string a = ex.Message; return(false); } outputReportLength = caps.OutputReportByteLength; inputReportLenght = caps.InputReportByteLength; hidDevice = new FileStream(new SafeFileHandle(hDevInfo, false), FileAccess.ReadWrite, inputReportLenght, false); Array.Resize(ref data, inputReportLenght); //数据长度由设备决定的 hidDevice.Write(data, 0, data.Length); // USBDataRead((int)hDevInfo); // HidD_FreePreparsedData(hDevInfo); // SetupDiDestroyDeviceInfoList(hDevInfo); // hidDevice.Dispose(); return(inputReportLenght > 0); } catch (Exception ex) { string err = ex.Message; return(false); } }
private unsafe void _ReadDataThread() { while (true) { int preparsedDataPtr = -1; if (HidD_GetPreparsedData(_hidHandle, ref preparsedDataPtr) != 0) { var caps = new HIDP_CAPS(); HidP_GetCaps(preparsedDataPtr, ref caps); int reportLength = caps.InputReportByteLength; while (true) { byte[] receivedData = null; int bytesRead = 0; byte[] buffer = new byte[reportLength]; if (ReadFile(_hidHandle, buffer, reportLength, ref bytesRead, null)) { receivedData = new byte[bytesRead - 1]; Array.Copy(buffer, 1, receivedData, 0, bytesRead - 1); } if (receivedData != null) { if (DataReceived != null) { DataReceived(this, new DataReceivedEventArgs(receivedData)); } } //Don't do this... //Thread.Sleep(1); } } } }
private static unsafe extern int HidP_GetCaps( int pPHIDP_PREPARSED_DATA, ref HIDP_CAPS myPHIDP_CAPS);
private void _Init(string devicePath, bool throwNotFoundError) { bool result; int deviceCount = 0; uint size; uint requiredSize; _guid = new Guid(); HidD_GetHidGuid(ref _guid); _hDeviceInfo = SetupDiGetClassDevs(ref _guid, IntPtr.Zero, IntPtr.Zero, DIGCF_INTERFACEDEVICE | DIGCF_PRESENT); do { _SP_DEVICE_INTERFACE_DATA = new SP_DEVICE_INTERFACE_DATA(); _SP_DEVICE_INTERFACE_DATA.cbSize = Marshal.SizeOf(_SP_DEVICE_INTERFACE_DATA); result = SetupDiEnumDeviceInterfaces(_hDeviceInfo, IntPtr.Zero, ref _guid, deviceCount, ref _SP_DEVICE_INTERFACE_DATA); SetupDiGetDeviceInterfaceDetail(_hDeviceInfo, ref _SP_DEVICE_INTERFACE_DATA, IntPtr.Zero, 0, out requiredSize, IntPtr.Zero); size = requiredSize; var diDetail = new SP_DEVICE_INTERFACE_DETAIL_DATA(); diDetail.cbSize = (uint)(IntPtr.Size == 8 ? 8 : 5); SetupDiGetDeviceInterfaceDetail(_hDeviceInfo, ref _SP_DEVICE_INTERFACE_DATA, ref diDetail, size, out requiredSize, IntPtr.Zero); _devicePath = diDetail.DevicePath; if (_devicePath == devicePath) { _found = true; _SP_DEVICE_INTERFACE_DATA = new SP_DEVICE_INTERFACE_DATA(); _SP_DEVICE_INTERFACE_DATA.cbSize = Marshal.SizeOf(_SP_DEVICE_INTERFACE_DATA); SetupDiEnumDeviceInterfaces(_hDeviceInfo, IntPtr.Zero, ref _guid, deviceCount, ref _SP_DEVICE_INTERFACE_DATA); size = 0; requiredSize = 0; SetupDiGetDeviceInterfaceDetail(_hDeviceInfo, ref _SP_DEVICE_INTERFACE_DATA, IntPtr.Zero, size, out requiredSize, IntPtr.Zero); SetupDiGetDeviceInterfaceDetail(_hDeviceInfo, ref _SP_DEVICE_INTERFACE_DATA, IntPtr.Zero, size, out requiredSize, IntPtr.Zero); _hidHandle = CreateFile(_devicePath, (uint)FileAccess.ReadWrite, (uint)FileShare.ReadWrite, IntPtr.Zero, (uint)FileMode.Open, (uint)EFileAttributes.Overlapped, IntPtr.Zero); //Get report lengths IntPtr preparsedDataPtr = (IntPtr)0xffffffff; if (HidD_GetPreparsedData(_hidHandle, ref preparsedDataPtr) != 0) { var caps = new HIDP_CAPS(); HidP_GetCaps(preparsedDataPtr, ref caps); OutputReportLength = caps.OutputReportByteLength; InputReportLength = caps.InputReportByteLength; _stream = new FileStream(_hidHandle, FileAccess.ReadWrite, InputReportLength, true); var buffer = new byte[InputReportLength]; _stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadData), buffer); } break; } deviceCount++; } while (result); if (!_found) { if (throwNotFoundError) throw new InvalidOperationException("Device not found"); } }
private void initDevice(string devicePath) { deviceConnected = false; //create file handles using CT_CreateFile handle = Kernel32.CreateFile(devicePath, Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); HID.HidD_GetPreparsedData(handle, ref ptrToPreParsedData); capabilities = new HIDP_CAPS(); HID.HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); HID.HidD_GetAttributes(handle, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HID.HidD_GetProductString(handle, buffer, 126)) { productName = Marshal.PtrToStringAuto(buffer); } if (HID.HidD_GetSerialNumberString(handle, buffer, 126)) { SN = Marshal.PtrToStringAuto(buffer); } if (HID.HidD_GetManufacturerString(handle, buffer, 126)) { manfString = Marshal.PtrToStringAuto(buffer); } Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HID.HidD_FreePreparsedData(ref ptrToPreParsedData); if (handle.IsInvalid) { return; } deviceConnected = true; //If connection was sucsessful, record the values in a global struct productInfo = new InterfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.serialNumber = SN; productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; }
internal static extern Int32 HidP_GetCaps(IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
private void initDevice(string devicePath, bool useAsyncReads) { deviceConnected = false; //create file handles using CT_CreateFile handle_read = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); handle_write = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HidD_GetPreparsedData(handle_read, ref ptrToPreParsedData); capabilities = new HIDP_CAPS(); int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); bool hidAttribSucsess = HidD_GetAttributes(handle_read, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HidD_GetProductString(handle_read, buffer, 126)) { productName = Marshal.PtrToStringAuto(buffer); } if (HidD_GetSerialNumberString(handle_read, buffer, 126)) { SN = Marshal.PtrToStringAuto(buffer); } if (HidD_GetManufacturerString(handle_read, buffer, 126)) { manfString = Marshal.PtrToStringAuto(buffer); } Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HidD_FreePreparsedData(ref ptrToPreParsedData); //SetupDiDestroyDeviceInfoList(i); if (handle_read.IsInvalid) { return; } deviceConnected = true; //If connection was sucsessful, record the values in a global struct productInfo = new interfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; //productInfo.serialNumber = Convert.ToInt32(SN); productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; //productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; //productInfo.OUT_reportByteLength =(int)capabilities.OutputReportByteLength; //use a filestream object to bring this stuff into .NET FS_read = new FileStream(handle_read, FileAccess.ReadWrite, 8 /*capabilities.OutputReportByteLength*/, false); FS_write = new FileStream(handle_write, FileAccess.ReadWrite, 8 /*capabilities.OutputReportByteLength*/, false); this.useAsyncReads = useAsyncReads; if (useAsyncReads) { readAsync(); } }
public unsafe static extern int HidP_GetCaps(IntPtr preparsed, out HIDP_CAPS caps);
public static extern int HidP_GetCaps(IntPtr lpData, out HIDP_CAPS oCaps);
public static extern int HidP_GetCaps( int pPHIDP_PREPARSED_DATA, ref HIDP_CAPS myPHIDP_CAPS);
public static bool Find_This_Device(ushort p_VendorID, ushort p_PoductID, ushort p_index, ref IntPtr p_ReadHandle, ref IntPtr p_WriteHandle) { //return false; //? timijk 2016.01.19 // Zero based p_index is used to identify which PICkit 2 we wish to talk to IntPtr DeviceInfoSet = IntPtr.Zero; IntPtr PreparsedDataPointer = IntPtr.Zero; HIDP_CAPS Capabilities = new HIDP_CAPS(); System.Guid HidGuid; int Result; bool l_found_device; ushort l_num_found_devices = 0; IntPtr l_temp_handle = IntPtr.Zero; int BufferSize = 0; SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData; SP_DEVICE_INTERFACE_DETAIL_DATA MyDeviceInterfaceDetailData; string SingledevicePathName; SECURITY_ATTRIBUTES Security = new SECURITY_ATTRIBUTES(); HIDD_ATTRIBUTES DeviceAttributes; IntPtr InvalidHandle = new IntPtr(-1); byte[] unitIDSerial = new byte[64]; // // initialize all // Security.lpSecurityDescriptor = 0; Security.bInheritHandle = System.Convert.ToInt32(true); Security.nLength = Marshal.SizeOf(Security); // HidGuid = Guid.Empty; // MyDeviceInterfaceData.cbSize = 0; MyDeviceInterfaceData.Flags = 0; MyDeviceInterfaceData.InterfaceClassGuid = Guid.Empty; MyDeviceInterfaceData.Reserved = 0; // MyDeviceInterfaceDetailData.cbSize = 0; MyDeviceInterfaceDetailData.DevicePath = ""; // DeviceAttributes.ProductID = 0; DeviceAttributes.Size = 0; DeviceAttributes.VendorID = 0; DeviceAttributes.VersionNumber = 0; // l_found_device = false; Security.lpSecurityDescriptor = 0; Security.bInheritHandle = System.Convert.ToInt32(true); Security.nLength = Marshal.SizeOf(Security); HidD_GetHidGuid(ref HidGuid); DeviceInfoSet = SetupDiGetClassDevs( ref HidGuid, null, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData); for (int l_loop = 0; l_loop < 20; l_loop++) { Result = SetupDiEnumDeviceInterfaces( DeviceInfoSet, 0, ref HidGuid, l_loop, ref MyDeviceInterfaceData); if (Result != 0) { SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref MyDeviceInterfaceData, IntPtr.Zero, 0, ref BufferSize, IntPtr.Zero); // Store the structure's size. MyDeviceInterfaceDetailData.cbSize = Marshal.SizeOf(MyDeviceInterfaceDetailData); // Allocate memory for the MyDeviceInterfaceDetailData Structure using the returned buffer size. IntPtr DetailDataBuffer = Marshal.AllocHGlobal(BufferSize); // Store cbSize in the first 4 bytes of the array Marshal.WriteInt32(DetailDataBuffer, 4 + Marshal.SystemDefaultCharSize); //Call SetupDiGetDeviceInterfaceDetail again. // This time, pass a pointer to DetailDataBuffer and the returned required buffer size. SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref MyDeviceInterfaceData, DetailDataBuffer, BufferSize, ref BufferSize, IntPtr.Zero); // Skip over cbsize (4 bytes) to get the address of the devicePathName. IntPtr pdevicePathName = new IntPtr(DetailDataBuffer.ToInt32() + 4); // Get the String containing the devicePathName. SingledevicePathName = Marshal.PtrToStringAuto(pdevicePathName); l_temp_handle = CreateFile( SingledevicePathName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, ref Security, OPEN_EXISTING, 0, 0); if (l_temp_handle != InvalidHandle) { // tried to use System.Threading.WaitHandle.InvalidHandle, but had access problems since it's protected // The returned handle is valid, so find out if this is the device we're looking for. // Set the Size property of DeviceAttributes to the number of bytes in the structure. DeviceAttributes.Size = Marshal.SizeOf(DeviceAttributes); Result = HidD_GetAttributes(l_temp_handle, ref DeviceAttributes); if (Result != 0) { if (DeviceAttributes.VendorID == p_VendorID && DeviceAttributes.ProductID == p_PoductID) { if (l_num_found_devices == p_index) { // found the correct one l_found_device = true; // get serial string (UnitID) HidD_GetSerialNumberString(l_temp_handle, unitIDSerial, 64); if ((unitIDSerial[0] == 0x09) || (unitIDSerial[0] == 0x00)) { UnitID = "-"; // blank } else { int x=2; for (; x < 28; x+=2) { // wide chars to chars unitIDSerial[x/2] = unitIDSerial[x]; if ((unitIDSerial[x] == 0) || (unitIDSerial[x] == 0xE0)) break; unitIDSerial[x] = 0; unitIDSerial[x+1] = 0; } x/=2; char[] asciiChars = new char[Encoding.ASCII.GetCharCount(unitIDSerial, 0, x)]; Encoding.ASCII.GetChars(unitIDSerial, 0, x, asciiChars, 0); string newString = new string(asciiChars); UnitID = newString; } // set return value p_WriteHandle = l_temp_handle; // get the device capabilities HidD_GetPreparsedData(l_temp_handle, ref PreparsedDataPointer); HidP_GetCaps(PreparsedDataPointer, ref Capabilities); // now create read handle p_ReadHandle = CreateFile( SingledevicePathName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, ref Security, OPEN_EXISTING, 0, 0); // now free up the resource, don't need anymore HidD_FreePreparsedData(ref PreparsedDataPointer); // get out of loop break; } CloseHandle(l_temp_handle); l_num_found_devices++; } else { l_found_device = false; CloseHandle(l_temp_handle); } } else { // There was a problem w/ HidD_GetAttributes l_found_device = false; CloseHandle(l_temp_handle); } // if result == true } // if HIDHandle } // if result == true } // end for //Free the memory reserved for the DeviceInfoSet returned by SetupDiGetClassDevs. SetupDiDestroyDeviceInfoList(DeviceInfoSet); return l_found_device; }
private unsafe void _ReadDataThread() { while (true) { int preparsedDataPtr = -1; if (HidD_GetPreparsedData(_hidHandle, ref preparsedDataPtr) != 0) { var caps = new HIDP_CAPS(); HidP_GetCaps(preparsedDataPtr, ref caps); int reportLength = caps.InputReportByteLength; while (true) { byte[] receivedData = null; int bytesRead = 0; byte[] buffer = new byte[reportLength]; if (ReadFile(_hidHandle, buffer, reportLength, ref bytesRead, null)) { receivedData = new byte[bytesRead - 1]; Array.Copy(buffer, 1, receivedData, 0, bytesRead - 1); } if (receivedData != null) { if (DataReceived != null) DataReceived(this, new DataReceivedEventArgs(receivedData)); } Thread.Sleep(1); } } } }
public static extern int HidP_GetCaps(IntPtr pPHIDP_PREPARSED_DATA, ref HIDP_CAPS myPHIDP_CAPS);
static public extern int HidP_GetCaps(IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
internal static extern bool HidP_GetCaps( IntPtr pp, ref HIDP_CAPS caps);
//---#+************************************************************************ //---NOTATION: //- int CT_HidP_GetCaps(int pPreparsedData) //- //--- DESCRIPTION: //-- Gets the capabilities report // // Autor: F.L. //-*************************************************************************+#* public unsafe int CT_HidP_GetCaps(int pPreparsedData) { myHIDP_CAPS = new HIDP_CAPS(); return HidP_GetCaps( pPreparsedData, ref myHIDP_CAPS); }
internal static extern int HidP_GetCaps(IntPtr preparsedData, ref HIDP_CAPS capabilities);
public static bool Find_This_Device(ushort p_VendorID, ushort p_PoductID, ushort p_index, ref IntPtr p_ReadHandle, ref IntPtr p_WriteHandle) { // Zero based p_index is used to identify which PICkit 2 we wish to talk to IntPtr DeviceInfoSet = IntPtr.Zero; IntPtr PreparsedDataPointer = IntPtr.Zero; HIDP_CAPS Capabilities = new HIDP_CAPS(); System.Guid HidGuid; int Result; bool l_found_device; ushort l_num_found_devices = 0; IntPtr l_temp_handle = IntPtr.Zero; int BufferSize = 0; SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData; SP_DEVICE_INTERFACE_DETAIL_DATA MyDeviceInterfaceDetailData; string SingledevicePathName; SECURITY_ATTRIBUTES Security = new SECURITY_ATTRIBUTES(); HIDD_ATTRIBUTES DeviceAttributes; IntPtr InvalidHandle = new IntPtr(-1); byte[] unitIDSerial = new byte[64]; // // initialize all // Security.lpSecurityDescriptor = 0; Security.bInheritHandle = System.Convert.ToInt32(true); Security.nLength = Marshal.SizeOf(Security); // HidGuid = Guid.Empty; // MyDeviceInterfaceData.cbSize = 0; MyDeviceInterfaceData.Flags = 0; MyDeviceInterfaceData.InterfaceClassGuid = Guid.Empty; MyDeviceInterfaceData.Reserved = 0; // MyDeviceInterfaceDetailData.cbSize = 0; MyDeviceInterfaceDetailData.DevicePath = ""; // DeviceAttributes.ProductID = 0; DeviceAttributes.Size = 0; DeviceAttributes.VendorID = 0; DeviceAttributes.VersionNumber = 0; // l_found_device = false; Security.lpSecurityDescriptor = 0; Security.bInheritHandle = System.Convert.ToInt32(true); Security.nLength = Marshal.SizeOf(Security); HidD_GetHidGuid(ref HidGuid); DeviceInfoSet = SetupDiGetClassDevs( ref HidGuid, null, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData); for (int l_loop = 0; l_loop < 20; l_loop++) { Result = SetupDiEnumDeviceInterfaces( DeviceInfoSet, 0, ref HidGuid, l_loop, ref MyDeviceInterfaceData); if (Result != 0) { SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref MyDeviceInterfaceData, IntPtr.Zero, 0, ref BufferSize, IntPtr.Zero); // Store the structure's size. MyDeviceInterfaceDetailData.cbSize = Marshal.SizeOf(MyDeviceInterfaceDetailData); // Allocate memory for the MyDeviceInterfaceDetailData Structure using the returned buffer size. IntPtr DetailDataBuffer = Marshal.AllocHGlobal(BufferSize); // Store cbSize in the first 4 bytes of the array Marshal.WriteInt32(DetailDataBuffer, 4 + Marshal.SystemDefaultCharSize); //Call SetupDiGetDeviceInterfaceDetail again. // This time, pass a pointer to DetailDataBuffer and the returned required buffer size. SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, ref MyDeviceInterfaceData, DetailDataBuffer, BufferSize, ref BufferSize, IntPtr.Zero); // Skip over cbsize (4 bytes) to get the address of the devicePathName. IntPtr pdevicePathName = new IntPtr(DetailDataBuffer.ToInt32() + 4); // Get the String containing the devicePathName. SingledevicePathName = Marshal.PtrToStringAuto(pdevicePathName); l_temp_handle = CreateFile( SingledevicePathName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, ref Security, OPEN_EXISTING, 0, 0); if (l_temp_handle != InvalidHandle) { // tried to use System.Threading.WaitHandle.InvalidHandle, but had access problems since it's protected // The returned handle is valid, so find out if this is the device we're looking for. // Set the Size property of DeviceAttributes to the number of bytes in the structure. DeviceAttributes.Size = Marshal.SizeOf(DeviceAttributes); Result = HidD_GetAttributes(l_temp_handle, ref DeviceAttributes); if (Result != 0) { if (DeviceAttributes.VendorID == p_VendorID && DeviceAttributes.ProductID == p_PoductID) { if (l_num_found_devices == p_index) { // found the correct one l_found_device = true; // get serial string (UnitID) HidD_GetSerialNumberString(l_temp_handle, unitIDSerial, 64); if ((unitIDSerial[0] == 0x09) || (unitIDSerial[0] == 0x00)) { UnitID = "-"; // blank } else { int x = 2; for (; x < 28; x += 2) { // wide chars to chars unitIDSerial[x / 2] = unitIDSerial[x]; if ((unitIDSerial[x] == 0) || (unitIDSerial[x] == 0xE0)) { break; } unitIDSerial[x] = 0; unitIDSerial[x + 1] = 0; } x /= 2; char[] asciiChars = new char[Encoding.ASCII.GetCharCount(unitIDSerial, 0, x)]; Encoding.ASCII.GetChars(unitIDSerial, 0, x, asciiChars, 0); string newString = new string(asciiChars); UnitID = newString; } // set return value p_WriteHandle = l_temp_handle; // get the device capabilities HidD_GetPreparsedData(l_temp_handle, ref PreparsedDataPointer); HidP_GetCaps(PreparsedDataPointer, ref Capabilities); // now create read handle p_ReadHandle = CreateFile( SingledevicePathName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, ref Security, OPEN_EXISTING, 0, 0); // now free up the resource, don't need anymore HidD_FreePreparsedData(ref PreparsedDataPointer); // get out of loop break; } CloseHandle(l_temp_handle); l_num_found_devices++; } else { l_found_device = false; CloseHandle(l_temp_handle); } } else { // There was a problem w/ HidD_GetAttributes l_found_device = false; CloseHandle(l_temp_handle); } // if result == true } // if HIDHandle } // if result == true } // end for //Free the memory reserved for the DeviceInfoSet returned by SetupDiGetClassDevs. SetupDiDestroyDeviceInfoList(DeviceInfoSet); return(l_found_device); }
/// <summary> /// Creates a 32-bit Usage from the Usage Page and Usage ID. /// Determines whether the Usage is a system mouse or keyboard. /// Can be modified to detect other Usages. /// </summary> /// /// <param name="MyCapabilities"> a HIDP_CAPS structure retrieved with HidP_GetCaps. </param> /// /// <returns> /// A String describing the Usage. /// </returns> internal String GetHidUsage(HIDP_CAPS MyCapabilities) { Int32 usage = 0; String usageDescription = ""; try { // Create32-bit Usage from Usage Page and Usage ID. usage = MyCapabilities.UsagePage * 256 + MyCapabilities.Usage; if (usage == Convert.ToInt32(0X102)) { usageDescription = "mouse"; } if (usage == Convert.ToInt32(0X106)) { usageDescription = "keyboard"; } } catch (Exception) { throw; } return usageDescription; }
public static extern int HidP_GetCaps(IntPtr PreparsedData, ref HIDP_CAPS Capabilities);
public static InterfaceDetails[] getConnectedDevices() { InterfaceDetails[] devices = new InterfaceDetails[0]; //Create structs to hold interface information SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA(); SP_DEVICE_INTERFACE_DATA devIface = new SP_DEVICE_INTERFACE_DATA(); devInfo.cbSize = (uint)Marshal.SizeOf(devInfo); devIface.cbSize = (uint)(Marshal.SizeOf(devIface)); Guid G = new Guid(); HID.HidD_GetHidGuid(ref G); //Get the guid of the HID device class IntPtr i = SetupAPI.SetupDiGetClassDevs(ref G, IntPtr.Zero, IntPtr.Zero, SetupAPI.DIGCF_DEVICEINTERFACE | SetupAPI.DIGCF_PRESENT); //Loop through all available entries in the device list, until false SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA(); if (IntPtr.Size == 8) // for 64 bit operating systems { didd.cbSize = 8; } else { didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // for 32 bit systems } int j = -1; bool b = true; SafeFileHandle tempHandle; while (b) { ++j; b = SetupAPI.SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref G, (uint)j, ref devIface); if (b == false) { break; } uint requiredSize = 0; SetupAPI.SetupDiGetDeviceInterfaceDetail(i, ref devIface, ref didd, 256, out requiredSize, ref devInfo); string devicePath = didd.DevicePath; //create file handles using CT_CreateFile tempHandle = Kernel32.CreateFile(devicePath, Kernel32.GENERIC_READ | Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_READ | Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HID.HidD_GetPreparsedData(tempHandle, ref ptrToPreParsedData); if (ppdSucsess == false) { continue; } HIDP_CAPS capabilities = new HIDP_CAPS(); HID.HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); HID.HidD_GetAttributes(tempHandle, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HID.HidD_GetProductString(tempHandle, buffer, 126)) { productName = Marshal.PtrToStringAuto(buffer); } if (HID.HidD_GetSerialNumberString(tempHandle, buffer, 126)) { SN = Marshal.PtrToStringAuto(buffer); } if (HID.HidD_GetManufacturerString(tempHandle, buffer, 126)) { manfString = Marshal.PtrToStringAuto(buffer); } Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HID.HidD_FreePreparsedData(ref ptrToPreParsedData); //If connection was sucsessful, record the values in a global struct InterfaceDetails productInfo = new InterfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; productInfo.serialNumber = SN; //Check that serial number is actually a number int newSize = devices.Length + 1; Array.Resize(ref devices, newSize); devices[newSize - 1] = productInfo; } SetupAPI.SetupDiDestroyDeviceInfoList(i); return(devices); }
private static extern int HidP_GetCaps( IntPtr pPHIDP_PREPARSED_DATA, ref HIDP_CAPS myPHIDP_CAPS);
public static bool Get_HID_Device(ushort p_VendorID, ushort p_PoductID, ushort p_index, ref IntPtr p_ReadHandle, ref IntPtr p_WriteHandle, ref string p_devicepath, bool p_pass_ptr_to_handle, ref Guid p_HidGuid, ref ushort p_num_devices_attached) { SP_DEVICE_INTERFACE_DATA sp_device_interface_data; SP_DEVICE_INTERFACE_DETAIL_DATA sp_device_interface_detail_data; HIDD_ATTRIBUTES hidd_attributes; Utilities.InitializeParams(); LIN.initialize_LIN_frames(); IntPtr zero = IntPtr.Zero; IntPtr preparsedData = IntPtr.Zero; HIDP_CAPS capabilities = new HIDP_CAPS(); ushort num2 = 0; IntPtr hidDeviceObject = IntPtr.Zero; int requiredSize = 0; SECURITY_ATTRIBUTES structure = new SECURITY_ATTRIBUTES(); IntPtr ptr4 = new IntPtr(-1); structure.lpSecurityDescriptor = 0; structure.bInheritHandle = Convert.ToInt32(true); structure.nLength = Marshal.SizeOf(structure); Guid empty = Guid.Empty; sp_device_interface_data.cbSize = 0; sp_device_interface_data.Flags = 0; sp_device_interface_data.InterfaceClassGuid = Guid.Empty; sp_device_interface_data.Reserved = 0; sp_device_interface_detail_data.cbSize = 0; sp_device_interface_detail_data.DevicePath = ""; hidd_attributes.ProductID = 0; hidd_attributes.Size = 0; hidd_attributes.VendorID = 0; hidd_attributes.VersionNumber = 0; bool flag = false; structure.lpSecurityDescriptor = 0; structure.bInheritHandle = Convert.ToInt32(true); structure.nLength = Marshal.SizeOf(structure); HidD_GetHidGuid(ref empty); zero = SetupDiGetClassDevs(ref empty, null, 0, 0x12); sp_device_interface_data.cbSize = Marshal.SizeOf(sp_device_interface_data); for (int i = 0; i < 30; i++) { if (SetupDiEnumDeviceInterfaces(zero, 0, ref empty, i, ref sp_device_interface_data) != 0) { SetupDiGetDeviceInterfaceDetail(zero, ref sp_device_interface_data, IntPtr.Zero, 0, ref requiredSize, IntPtr.Zero); sp_device_interface_detail_data.cbSize = Marshal.SizeOf(sp_device_interface_detail_data); IntPtr ptr = Marshal.AllocHGlobal(requiredSize); Marshal.WriteInt32(ptr, 4 + Marshal.SystemDefaultCharSize); SetupDiGetDeviceInterfaceDetail(zero, ref sp_device_interface_data, ptr, requiredSize, ref requiredSize, IntPtr.Zero); IntPtr ptr6 = new IntPtr(ptr.ToInt32() + 4); string lpFileName = Marshal.PtrToStringAuto(ptr6); hidDeviceObject = CreateFile(lpFileName, 0xc0000000, 3, ref structure, 3, 0, 0); if (hidDeviceObject != ptr4) { hidd_attributes.Size = Marshal.SizeOf(hidd_attributes); if (HidD_GetAttributes(hidDeviceObject, ref hidd_attributes) != 0) { if ((hidd_attributes.VendorID == p_VendorID) && (hidd_attributes.ProductID == p_PoductID)) { if (num2 == p_index) { flag = true; if (p_pass_ptr_to_handle) { p_WriteHandle = hidDeviceObject; } p_devicepath = lpFileName; p_HidGuid = empty; Utilities.m_flags.HID_write_handle = hidDeviceObject; HidD_GetPreparsedData(hidDeviceObject, ref preparsedData); HidP_GetCaps(preparsedData, ref capabilities); Utilities.m_flags.irbl = (ushort) capabilities.InputReportByteLength; Utilities.m_flags.HID_read_handle = CreateFile(lpFileName, 0xc0000000, 3, ref structure, 3, 0x40000000, 0); if (p_pass_ptr_to_handle) { p_ReadHandle = Utilities.m_flags.HID_read_handle; } HidD_FreePreparsedData(ref preparsedData); break; } num2 = (ushort) (num2 + 1); } else { flag = false; CloseHandle(hidDeviceObject); } } else { flag = false; CloseHandle(hidDeviceObject); } } } } SetupDiDestroyDeviceInfoList(zero); p_num_devices_attached = num2; return flag; }
public static interfaceDetails[] getConnectedDevices() { interfaceDetails[] devices = new interfaceDetails[0]; //Create structs to hold interface information SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA(); SP_DEVICE_INTERFACE_DATA devIface = new SP_DEVICE_INTERFACE_DATA(); devInfo.cbSize = (uint)Marshal.SizeOf(devInfo); devIface.cbSize = (uint)(Marshal.SizeOf(devIface)); Guid G = new Guid(); HidD_GetHidGuid(ref G); //Get the guid of the HID device class IntPtr i = SetupDiGetClassDevs(ref G, IntPtr.Zero, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); //Loop through all available entries in the device list, until false SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA(); if (IntPtr.Size == 8) // for 64 bit operating systems didd.cbSize = 8; else didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // for 32 bit systems int j = -1; bool b = true; int error; SafeFileHandle tempHandle; while (b) { j++; b = SetupDiEnumDeviceInterfaces(i, IntPtr.Zero, ref G, (uint)j, ref devIface); error = Marshal.GetLastWin32Error(); if (b == false) break; uint requiredSize = 0; bool b1 = SetupDiGetDeviceInterfaceDetail(i, ref devIface, ref didd, 256, out requiredSize, ref devInfo); string devicePath = didd.DevicePath; //create file handles using CT_CreateFile tempHandle = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HidD_GetPreparsedData(tempHandle, ref ptrToPreParsedData); if (ppdSucsess == false) continue; HIDP_CAPS capabilities = new HIDP_CAPS(); int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); bool hidAttribSucsess = HidD_GetAttributes(tempHandle, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HidD_GetProductString(tempHandle, buffer, 126)) productName = Marshal.PtrToStringAuto(buffer); if (HidD_GetSerialNumberString(tempHandle, buffer, 126)) SN = Marshal.PtrToStringAuto(buffer); if (HidD_GetManufacturerString(tempHandle, buffer, 126)) manfString = Marshal.PtrToStringAuto(buffer); Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HidD_FreePreparsedData(ref ptrToPreParsedData); //If connection was sucsessful, record the values in a global struct interfaceDetails productInfo = new interfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; if (stringIsInteger(SN)) productInfo.serialNumber = Convert.ToInt32(SN); //Check that serial number is actually a number int newSize = devices.Length + 1; Array.Resize(ref devices, newSize); devices[newSize - 1] = productInfo; } SetupDiDestroyDeviceInfoList(i); return devices; }
static internal extern int HidP_GetCaps(IntPtr preparsedData, ref HIDP_CAPS capabilities);
private static extern int HidP_GetCaps( IntPtr pPHIDP_PREPARSED_DATA, // IN PHIDP_PREPARSED_DATA PreparsedData, ref HIDP_CAPS myPHIDP_CAPS);
private unsafe static extern int HidP_GetCaps( int pPHIDP_PREPARSED_DATA, // IN PHIDP_PREPARSED_DATA PreparsedData, ref HIDP_CAPS myPHIDP_CAPS); // OUT PHIDP_CAPS Capabilities
private void initDevice(string devicePath, bool useAsyncReads) { deviceConnected = false; //create file handles using CT_CreateFile handle_read = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); handle_write = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); //get capabilites - use getPreParsedData, and getCaps //store the reportlengths IntPtr ptrToPreParsedData = new IntPtr(); bool ppdSucsess = HidD_GetPreparsedData(handle_read, ref ptrToPreParsedData); capabilities = new HIDP_CAPS(); int hidCapsSucsess = HidP_GetCaps(ptrToPreParsedData, ref capabilities); HIDD_ATTRIBUTES attributes = new HIDD_ATTRIBUTES(); bool hidAttribSucsess = HidD_GetAttributes(handle_read, ref attributes); string productName = ""; string SN = ""; string manfString = ""; IntPtr buffer = Marshal.AllocHGlobal(126);//max alloc for string; if (HidD_GetProductString(handle_read, buffer, 126)) productName = Marshal.PtrToStringAuto(buffer); if (HidD_GetSerialNumberString(handle_read, buffer, 126)) SN = Marshal.PtrToStringAuto(buffer); if (HidD_GetManufacturerString(handle_read, buffer, 126)) manfString = Marshal.PtrToStringAuto(buffer); Marshal.FreeHGlobal(buffer); //Call freePreParsedData to release some stuff HidD_FreePreparsedData(ref ptrToPreParsedData); //SetupDiDestroyDeviceInfoList(i); if (handle_read.IsInvalid) return; deviceConnected = true; //If connection was sucsessful, record the values in a global struct productInfo = new interfaceDetails(); productInfo.devicePath = devicePath; productInfo.manufacturer = manfString; productInfo.product = productName; productInfo.serialNumber = Convert.ToInt32(SN); productInfo.PID = (ushort)attributes.ProductID; productInfo.VID = (ushort)attributes.VendorID; productInfo.versionNumber = (ushort)attributes.VersionNumber; productInfo.IN_reportByteLength = (int)capabilities.InputReportByteLength; productInfo.OUT_reportByteLength = (int)capabilities.OutputReportByteLength; //use a filestream object to bring this stuff into .NET FS_read = new FileStream(handle_read, FileAccess.ReadWrite, capabilities.OutputReportByteLength, false); FS_write = new FileStream(handle_write, FileAccess.ReadWrite, capabilities.InputReportByteLength, false); this.useAsyncReads = useAsyncReads; if (useAsyncReads) readAsync(); }
public static extern int HidP_GetCaps( IntPtr pPHIDP_PREPARSED_DATA, // IN PHIDP_PREPARSED_DATA PreparsedData, ref HIDP_CAPS myPHIDP_CAPS); // OUT PHIDP_CAPS Capabilities
/// <summary> /// Creates a 32-bit Usage from the Usage Page and Usage ID. /// Determines whether the Usage is a system mouse or keyboard. /// Can be modified to detect other Usages. /// </summary> /// /// <param name="myCapabilities"> a HIDP_CAPS structure retrieved with HidP_GetCaps. </param> /// /// <returns> /// A String describing the Usage. /// </returns> internal String GetHidUsage( HIDP_CAPS myCapabilities ) { var usageDescription = ""; // Create32-bit Usage from Usage Page and Usage ID. int usage = myCapabilities.UsagePage * 256 + myCapabilities.Usage; if ( usage == Convert.ToInt32( 0X102 ) ) { usageDescription = "mouse"; } if ( usage == Convert.ToInt32( 0X106 ) ) { usageDescription = "keyboard"; } return usageDescription; }
private static unsafe extern int HidP_GetCaps( int pPHIDP_PREPARSED_DATA, // IN PHIDP_PREPARSED_DATA PreparsedData, ref HIDP_CAPS myPHIDP_CAPS);
public static bool Get_This_Device(ushort p_VendorID, ushort p_PoductID, ushort p_index, ref IntPtr p_ReadHandle, ref IntPtr p_WriteHandle, ref string p_devicepath, bool p_pass_ptr_to_handle, ref Guid p_HidGuid, ref ushort p_num_devices_attached) { SP_DEVICE_INTERFACE_DATA sp_device_interface_data; SP_DEVICE_INTERFACE_DETAIL_DATA sp_device_interface_detail_data; HIDD_ATTRIBUTES hidd_attributes; Utilities.InitializeParams(); LIN.initialize_LIN_frames(); IntPtr zero = IntPtr.Zero; IntPtr preparsedData = IntPtr.Zero; HIDP_CAPS capabilities = new HIDP_CAPS(); ushort num2 = 0; IntPtr hidDeviceObject = IntPtr.Zero; int requiredSize = 0; SECURITY_ATTRIBUTES structure = new SECURITY_ATTRIBUTES(); IntPtr ptr4 = new IntPtr(-1); structure.lpSecurityDescriptor = 0; structure.bInheritHandle = Convert.ToInt32(true); structure.nLength = Marshal.SizeOf(structure); Guid empty = Guid.Empty; sp_device_interface_data.cbSize = 0; sp_device_interface_data.Flags = 0; sp_device_interface_data.InterfaceClassGuid = Guid.Empty; sp_device_interface_data.Reserved = 0; sp_device_interface_detail_data.cbSize = 0; sp_device_interface_detail_data.DevicePath = ""; hidd_attributes.ProductID = 0; hidd_attributes.Size = 0; hidd_attributes.VendorID = 0; hidd_attributes.VersionNumber = 0; bool flag = false; structure.lpSecurityDescriptor = 0; structure.bInheritHandle = Convert.ToInt32(true); structure.nLength = Marshal.SizeOf(structure); HidD_GetHidGuid(ref empty); zero = SetupDiGetClassDevs(ref empty, null, 0, 0x12); sp_device_interface_data.cbSize = Marshal.SizeOf(sp_device_interface_data); for (int i = 0; i < 30; i++) { if (SetupDiEnumDeviceInterfaces(zero, 0, ref empty, i, ref sp_device_interface_data) != 0) { SetupDiGetDeviceInterfaceDetail(zero, ref sp_device_interface_data, IntPtr.Zero, 0, ref requiredSize, IntPtr.Zero); sp_device_interface_detail_data.cbSize = Marshal.SizeOf(sp_device_interface_detail_data); IntPtr ptr = Marshal.AllocHGlobal(requiredSize); Marshal.WriteInt32(ptr, 4 + Marshal.SystemDefaultCharSize); SetupDiGetDeviceInterfaceDetail(zero, ref sp_device_interface_data, ptr, requiredSize, ref requiredSize, IntPtr.Zero); IntPtr ptr6 = new IntPtr(ptr.ToInt32() + 4); string lpFileName = Marshal.PtrToStringAuto(ptr6); hidDeviceObject = CreateFile(lpFileName, 0xc0000000, 3, ref structure, 3, 0, 0); if (hidDeviceObject != ptr4) { hidd_attributes.Size = Marshal.SizeOf(hidd_attributes); if (HidD_GetAttributes(hidDeviceObject, ref hidd_attributes) != 0) { if ((hidd_attributes.VendorID == p_VendorID) && (hidd_attributes.ProductID == p_PoductID)) { if (num2 == p_index) { flag = true; if (p_pass_ptr_to_handle) { p_WriteHandle = hidDeviceObject; } p_devicepath = lpFileName; p_HidGuid = empty; Utilities.m_flags.HID_write_handle = hidDeviceObject; HidD_GetPreparsedData(hidDeviceObject, ref preparsedData); HidP_GetCaps(preparsedData, ref capabilities); Utilities.m_flags.irbl = (ushort)capabilities.InputReportByteLength; Utilities.m_flags.HID_read_handle = CreateFile(lpFileName, 0xc0000000, 3, ref structure, 3, 0, 0); if (p_pass_ptr_to_handle) { p_ReadHandle = Utilities.m_flags.HID_read_handle; } HidD_FreePreparsedData(ref preparsedData); break; } num2 = (ushort)(num2 + 1); } else { flag = false; CloseHandle(hidDeviceObject); } } else { flag = false; CloseHandle(hidDeviceObject); } } } } SetupDiDestroyDeviceInfoList(zero); p_num_devices_attached = num2; return(flag); }
internal static extern bool HidP_GetCaps( IntPtr hData, out HIDP_CAPS capabilities );
public void Write(byte[] data) { int preparsedDataPtr = -1; HidD_GetPreparsedData(_hidHandle, ref preparsedDataPtr); var caps = new HIDP_CAPS(); HidP_GetCaps(preparsedDataPtr, ref caps); int outputReportByteLength = caps.OutputReportByteLength; int bytesSent = 0; while (bytesSent < data.Length) { byte[] buffer = new byte[outputReportByteLength]; buffer[0] = 0; for (int i = 1; i < buffer.Length; i++) { if (bytesSent < data.Length) { buffer[i] = data[bytesSent]; bytesSent++; } else { buffer[i] = 0; } } int bytesWritten = 0; WriteFile(_hidHandle, ref buffer[0], buffer.Length, ref bytesWritten, 0); } }