Exemple #1
0
 public static extern int HidP_GetCaps(IntPtr PreparsedData, ref USB.HIDP_CAPS Capabilities);
Exemple #2
0
        public static bool Find_This_Device(ushort p_VendorID, ushort p_PoductID, ushort p_index, ref IntPtr p_ReadHandle, ref IntPtr p_WriteHandle)
        {
            IntPtr deviceInfoSet = IntPtr.Zero;
            IntPtr zero          = IntPtr.Zero;

            USB.HIDP_CAPS hIDP_CAPS = default(USB.HIDP_CAPS);
            ushort        num       = 0;
            IntPtr        intPtr    = IntPtr.Zero;
            int           num2      = 0;

            USB.SECURITY_ATTRIBUTES sECURITY_ATTRIBUTES = default(USB.SECURITY_ATTRIBUTES);
            IntPtr value = new IntPtr(-1);

            byte[] array = new byte[64];
            sECURITY_ATTRIBUTES.lpSecurityDescriptor = 0;
            sECURITY_ATTRIBUTES.bInheritHandle       = Convert.ToInt32(true);
            sECURITY_ATTRIBUTES.nLength = Marshal.SizeOf(sECURITY_ATTRIBUTES);
            Guid empty = Guid.Empty;

            USB.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;
            USB.SP_DEVICE_INTERFACE_DETAIL_DATA sP_DEVICE_INTERFACE_DETAIL_DATA;
            sP_DEVICE_INTERFACE_DETAIL_DATA.cbSize     = 0;
            sP_DEVICE_INTERFACE_DETAIL_DATA.DevicePath = "";
            USB.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);
            USB.HidD_GetHidGuid(ref empty);
            deviceInfoSet = USB.SetupDiGetClassDevs(ref empty, null, 0, 18);
            sP_DEVICE_INTERFACE_DATA.cbSize = Marshal.SizeOf(sP_DEVICE_INTERFACE_DATA);
            for (int i = 0; i < 20; i++)
            {
                int num3 = USB.SetupDiEnumDeviceInterfaces(deviceInfoSet, 0, ref empty, i, ref sP_DEVICE_INTERFACE_DATA);
                if (num3 != 0)
                {
                    USB.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 intPtr2 = Marshal.AllocHGlobal(num2);
                    Marshal.WriteInt32(intPtr2, 4 + Marshal.SystemDefaultCharSize);
                    USB.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref sP_DEVICE_INTERFACE_DATA, intPtr2, num2, ref num2, IntPtr.Zero);
                    IntPtr ptr        = new IntPtr(intPtr2.ToInt32() + 4);
                    string lpFileName = Marshal.PtrToStringAuto(ptr);
                    intPtr = USB.CreateFile(lpFileName, 3221225472u, 3u, ref sECURITY_ATTRIBUTES, 3, 0u, 0);
                    if (intPtr != value)
                    {
                        hIDD_ATTRIBUTES.Size = Marshal.SizeOf(hIDD_ATTRIBUTES);
                        num3 = USB.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;
                                    USB.HidD_GetSerialNumberString(intPtr, array, 64uL);
                                    if (array[0] == 9 || array[0] == 0)
                                    {
                                        USB.UnitID = "-";
                                    }
                                    else
                                    {
                                        int j;
                                        for (j = 2; j < 28; j += 2)
                                        {
                                            array[j / 2] = array[j];
                                            if (array[j] == 0 || array[j] == 224)
                                            {
                                                break;
                                            }
                                            array[j]     = 0;
                                            array[j + 1] = 0;
                                        }
                                        j /= 2;
                                        char[] array2 = new char[Encoding.ASCII.GetCharCount(array, 0, j)];
                                        Encoding.ASCII.GetChars(array, 0, j, array2, 0);
                                        string unitID = new string(array2);
                                        USB.UnitID = unitID;
                                    }
                                    p_WriteHandle = intPtr;
                                    USB.HidD_GetPreparsedData(intPtr, ref zero);
                                    USB.HidP_GetCaps(zero, ref hIDP_CAPS);
                                    p_ReadHandle = USB.CreateFile(lpFileName, 3221225472u, 3u, ref sECURITY_ATTRIBUTES, 3, 0u, 0);
                                    USB.HidD_FreePreparsedData(ref zero);
                                    break;
                                }
                                USB.CloseHandle(intPtr);
                                num += 1;
                            }
                            else
                            {
                                result = false;
                                USB.CloseHandle(intPtr);
                            }
                        }
                        else
                        {
                            result = false;
                            USB.CloseHandle(intPtr);
                        }
                    }
                }
            }
            USB.SetupDiDestroyDeviceInfoList(deviceInfoSet);
            return(result);
        }