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); }
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) { Utilities.InitializeParams(); LIN.initialize_LIN_frames(); IntPtr deviceInfoSet = IntPtr.Zero; IntPtr zero = IntPtr.Zero; GenericHID.HIDP_CAPS hIDP_CAPS = default(GenericHID.HIDP_CAPS); ushort num = 0; IntPtr intPtr = IntPtr.Zero; int num2 = 0; GenericHID.SECURITY_ATTRIBUTES sECURITY_ATTRIBUTES = default(GenericHID.SECURITY_ATTRIBUTES); IntPtr intPtr2 = new IntPtr(-1); sECURITY_ATTRIBUTES.lpSecurityDescriptor = 0; sECURITY_ATTRIBUTES.bInheritHandle = Convert.ToInt32(true); sECURITY_ATTRIBUTES.nLength = Marshal.SizeOf(sECURITY_ATTRIBUTES); Guid empty = Guid.Empty; GenericHID.SP_DEVICE_INTERFACE_DATA sP_DEVICE_INTERFACE_DATA; 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; GenericHID.SP_DEVICE_INTERFACE_DETAIL_DATA sP_DEVICE_INTERFACE_DETAIL_DATA; sP_DEVICE_INTERFACE_DETAIL_DATA.cbSize = 0; sP_DEVICE_INTERFACE_DETAIL_DATA.DevicePath = ""; GenericHID.HIDD_ATTRIBUTES hIDD_ATTRIBUTES; hIDD_ATTRIBUTES.ProductID = 0; hIDD_ATTRIBUTES.Size = 0; hIDD_ATTRIBUTES.VendorID = 0; hIDD_ATTRIBUTES.VersionNumber = 0; bool result = false; sECURITY_ATTRIBUTES.lpSecurityDescriptor = 0; sECURITY_ATTRIBUTES.bInheritHandle = Convert.ToInt32(true); sECURITY_ATTRIBUTES.nLength = Marshal.SizeOf(sECURITY_ATTRIBUTES); /* * GenericHID.HidD_GetHidGuid(ref empty); * deviceInfoSet = GenericHID.SetupDiGetClassDevs(ref empty, null, 0, 18); * sP_DEVICE_INTERFACE_DATA.cbSize = Marshal.SizeOf(sP_DEVICE_INTERFACE_DATA); * for (int i = 0; i < 30; i++) * { * int num3 = GenericHID.SetupDiEnumDeviceInterfaces(deviceInfoSet, 0, ref empty, i, ref sP_DEVICE_INTERFACE_DATA); * if (num3 != 0) * { * GenericHID.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref sP_DEVICE_INTERFACE_DATA, IntPtr.Zero, 0, ref num2, IntPtr.Zero); * sP_DEVICE_INTERFACE_DETAIL_DATA.cbSize = Marshal.SizeOf(sP_DEVICE_INTERFACE_DETAIL_DATA); * IntPtr intPtr3 = Marshal.AllocHGlobal(num2); * Marshal.WriteInt32(intPtr3, 4 + Marshal.SystemDefaultCharSize); * GenericHID.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref sP_DEVICE_INTERFACE_DATA, intPtr3, num2, ref num2, IntPtr.Zero); * IntPtr intPtr4 = new IntPtr(intPtr3.ToInt32() + 4); * string text = Marshal.PtrToStringAuto(intPtr4); * intPtr = GenericHID.CreateFile(text, 3221225472u, 3u, ref sECURITY_ATTRIBUTES, 3, 0u, 0); * if (intPtr != intPtr2) * { * hIDD_ATTRIBUTES.Size = Marshal.SizeOf(hIDD_ATTRIBUTES); * num3 = GenericHID.HidD_GetAttributes(intPtr, ref hIDD_ATTRIBUTES); * if (num3 != 0) * { * if (hIDD_ATTRIBUTES.VendorID == p_VendorID && hIDD_ATTRIBUTES.ProductID == p_PoductID) * { * if (num == p_index) * { * result = true; * if (p_pass_ptr_to_handle) * { * p_WriteHandle = intPtr; * } * p_devicepath = text; * p_HidGuid = empty; * // Utilities.m_flags.HID_write_handle = intPtr; * GenericHID.HidD_GetPreparsedData(intPtr, ref zero); * GenericHID.HidP_GetCaps(zero, ref hIDP_CAPS); * Utilities.m_flags.irbl = (ushort)hIDP_CAPS.InputReportByteLength; * // Utilities.m_flags.HID_read_handle = GenericHID.CreateFile(text, 3221225472u, 3u, ref sECURITY_ATTRIBUTES, 3, 1073741824u, 0); * if (p_pass_ptr_to_handle) * { * // p_ReadHandle = Utilities.m_flags.HID_read_handle; * } * GenericHID.HidD_FreePreparsedData(ref zero); * break; * } * num += 1; * } * else * { * result = false; * GenericHID.CloseHandle(intPtr); * } * } * else * { * result = false; * GenericHID.CloseHandle(intPtr); * } * } * } * } * GenericHID.SetupDiDestroyDeviceInfoList(deviceInfoSet); * p_num_devices_attached = num; */ return(result); }