private static string GetStringPropertyForDevice(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP propId)
 {
     uint proptype;
     uint outsize = 0;
     var buffer = new byte[MAX_DEVICE_LEN];
     var result = DeviceDetector.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, (uint)propId, out proptype, buffer, (uint)buffer.Length, out outsize);
     if (!result)
     {
         var errcode = Marshal.GetLastWin32Error();
         if (errcode == ERROR_INVALID_DATA) return null;
         throw new Exception("Error calling SetupDiGetDeviceRegistryPropertyW: " + errcode);
     }
     var o = "";
     if (outsize > 0)
     {
         switch (proptype)
         {
             case (uint)REG.REG_SZ:
                 o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2);
                 break;
             case (uint)REG.REG_MULTI_SZ:
                 o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2);
                 o = o.Trim('\0').Replace("\0", "\r\n");
                 break;
             default:
                 break;
         }
     }
     return o;
 }
Example #2
0
 public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet,
                                                            ref SP_DEVINFO_DATA DeviceInfoData,
                                                            SPDRP Property,
                                                            out RegistryValueKind PropertyRegDataType,
                                                            byte[] PropertyBuffer,
                                                            int PropertyBufferSize,
                                                            out int RequiredSize);
Example #3
0
            string GetString(SPDRP property)
            {
                var resultString = new StringBuilder(KERNEL32.DefaultStructBufferSize);
                var devInfo      = deviceInfo;
                var result       = SETUPAPI.SetupDiGetDeviceRegistryProperty(Handle, ref devInfo, property, out var valueKind, resultString, (uint)resultString.Capacity, out _);

                if (!result)
                {
                    var error = Marshal.GetLastWin32Error();
                    if (error == 13)
                    {
                        return(null);
                    }
                    if (error != 0)
                    {
                        throw new Win32ErrorException(error);
                    }
                }
                switch (valueKind)
                {
                case RegistryValueKind.Binary: throw new InvalidCastException(string.Format("Please use GetData() for BINARY values!"));

                case RegistryValueKind.DWord: throw new InvalidCastException(string.Format("Please use GetUInt32() for DWORD values!"));

                case RegistryValueKind.QWord: throw new InvalidCastException(string.Format("Please use GetUInt64() for QWORD values!"));

                case RegistryValueKind.MultiString: break;

                case RegistryValueKind.String: break;

                default: throw new NotImplementedException(string.Format("Not implemented registry value kind {0}!", valueKind));
                }
                return(resultString.ToString());
            }
Example #4
0
 public Byte[] GetProperty(SPDRP property)
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) {
         if (dis.IsInvalid)
         {
             throw new Win32Exception();
         }
         SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true);
         if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd))
         {
             return(null);
         }
         RegistryValueKind propertyType;
         byte[]            propBuffer = new byte[256];
         int requiredSize;
         if (!SetupApi.SetupDiGetDeviceRegistryProperty(dis, ref dd, property, out propertyType, propBuffer, propBuffer.Length, out requiredSize))
         {
             return(null);
         }
         if (requiredSize > propBuffer.Length)
         {
             propBuffer = new Byte[requiredSize];
             if (!SetupApi.SetupDiGetDeviceRegistryProperty(dis, ref dd, property, out propertyType, propBuffer, propBuffer.Length, out requiredSize))
             {
                 throw new Win32Exception();
             }
         }
         if (requiredSize < propBuffer.Length)
         {
             Array.Resize(ref propBuffer, requiredSize);
         }
         return(propBuffer);
     }
 }
        /// <summary>
        /// Gets a device's registry property. Helper class that
        /// encapsulates the core functionality of the other get
        /// registry property functions.
        /// </summary>
        /// <param name="devInfoSet">The dev info set.</param>
        /// <param name="devInfoData">The dev info data.</param>
        /// <param name="propertyIndex">Index of the property.</param>
        /// <returns>The property buffer.</returns>
        private static StringBuilder GetProperty(
            IntPtr devInfoSet,
            SP_DEVINFO_DATA devInfoData,
            SPDRP propertyIndex)
        {
            Int32         requiredSize        = 0;
            Int32         propertyRegDataType = 0;
            StringBuilder propertyBuffer;

            Win32SetupApi.SetupDiGetDeviceRegistryProperty(
                devInfoSet,
                ref devInfoData,
                (int)propertyIndex,
                ref propertyRegDataType,
                null,
                0,
                ref requiredSize);

            propertyBuffer = new StringBuilder(requiredSize);

            propertyRegDataType = 0;
            Win32SetupApi.SetupDiGetDeviceRegistryProperty(
                devInfoSet,
                ref devInfoData,
                (int)propertyIndex,
                ref propertyRegDataType,
                propertyBuffer,
                propertyBuffer.Capacity,
                ref requiredSize);

            return(propertyBuffer);
        }
Example #6
0
 internal static extern bool SetupDiGetDeviceRegistryProperty(
     IntPtr deviceInfoSet,
     ref SP_DEVINFO_DATA deviceInfoData,
     SPDRP property,
     out UInt32 propertyRegDataType,
     StringBuilder propertyBuffer,
     uint propertyBufferSize,
     out UInt32 requiredSize);
 private static extern bool SetupDiGetDeviceRegistryProperty(
     IntPtr DeviceInfoSet,
     ref SP_DEVINFO_DATA DeviceInfoData,
     SPDRP Property,
     out UInt32 PropertyRegDataType,
     byte[] PropertyBuffer,
     uint PropertyBufferSize,
     out UInt32 RequiredSize);
Example #8
0
 public String GetPropertyString(SPDRP property)
 {
     Byte[] buffer = GetProperty(property);
     if (buffer == null)
     {
         return(null);
     }
     return(SetupApi.GetAsString(buffer, buffer.Length));
 }
Example #9
0
 public static extern bool SetupDiGetDeviceRegistryProperty(
     IntPtr deviceInfoSet,
     SP_DEVINFO_DATA deviceInfoData,
     SPDRP property,
     out int propertyRegDataType,
     byte[] propertyBuffer,
     int propertyBufferSize,
     out int requiredSize
     );
 private static extern bool SetupDiGetDeviceRegistryProperty(
     SafeDeviceInformationSetHandle DeviceInfoSet,
     [In] ref SP_DEVINFO_DATA DeviceInfoData,
     SPDRP Property,
     out RegistryValueKind PropertyRegDataType,
     [Out] byte[] PropertyBuffer,
     UInt32 PropertyBufferSize,
     out UInt32 RequiredSize
     );
Example #11
0
 static extern bool SetupDiGetDeviceRegistryProperty(
     IntPtr DeviceInfoSet,
     [In] ref SP_DEVINFO_DATA DeviceInfoData,
     [MarshalAs(UnmanagedType.U4)] SPDRP Property,
     [Out] out UInt32 PropertyRegDataType,
     IntPtr PropertyBuffer,
     UInt32 PropertyBufferSize,
     [In, Out] ref UInt32 RequiredSize
     );
Example #12
0
 /// <summary>
 /// Gets a device property/key from the registry.  See the <see cref="SPDRP"/> enumeration for more information.
 /// </summary>
 /// <param name="spdrp">The name of the property to retrieve.</param>
 /// <returns></returns>
 public object this[SPDRP spdrp]
 {
     get
     {
         object temp;
         mDeviceProperties.TryGetValue(spdrp.ToString(), out temp);
         return(temp);
     }
 }
Example #13
0
 public static extern bool SetupDiGetDeviceRegistryProperty(
     IntPtr deviceInfoSet,
     ref SP_DEVINFO_DATA deviceInfoData,
     SPDRP property,
     out UInt32 propertyRegDataType,
     IntPtr propertyBuffer,
     uint propertyBufferSize,
     out UInt32 requiredSize
     );
Example #14
0
            public string GetProperty(SPDRP property)
            {
                var stringBuilder = new StringBuilder(1024);

                if (!NativeMethods.SetupDiGetDeviceRegistryProperty(HDevInfo, ref _devinfoData, SPDRP.SPDRP_HARDWAREID, out _,
                                                                    stringBuilder, (uint)stringBuilder.Capacity, out _))
                {
                    return(null);
                }

                return(stringBuilder.ToString());
            }
Example #15
0
        public static bool SetupDiGetDeviceRegistryProperty(out string[] regMultiSZ,
                                                            IntPtr DeviceInfoSet,
                                                            ref SP_DEVINFO_DATA DeviceInfoData,
                                                            SPDRP Property)
        {
            regMultiSZ = null;
            string tmp;

            if (SetupDiGetDeviceRegistryProperty(out tmp, DeviceInfoSet, ref DeviceInfoData, Property))
            {
                regMultiSZ = tmp.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
                return(true);
            }
            return(false);
        }
Example #16
0
        public static bool SetupDiGetDeviceRegistryProperty(out uint value,
                                                            IntPtr DeviceInfoSet,
                                                            ref SP_DEVINFO_DATA DeviceInfoData,
                                                            SPDRP Property)
        {
            value = 0;
            uint[]            tmp = new uint[1];
            int               iReqSize;
            RegistryValueKind regValueType;

            if (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, Property, out regValueType, tmp, tmp.Length * Marshal.SizeOf(typeof(uint)), out iReqSize))
            {
                //usb_error("usb_registry_match_no_hubs(): getting hardware id failed");
                return(false);
            }
            value = tmp[0];
            return(true);
        }
Example #17
0
 public void SetProperty(SPDRP property, Byte[] value)
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) {
         if (dis.IsInvalid)
         {
             throw new Win32Exception();
         }
         SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true);
         if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd))
         {
             throw new Win32Exception();
         }
         if (!SetupApi.SetupDiSetDeviceRegistryProperty(dis, ref dd, property, value, (uint)value.Length))
         {
             throw new Win32Exception();
         }
     }
 }
Example #18
0
        public static bool SetupDiGetDeviceRegistryProperty(out byte[] regBytes,
                                                            IntPtr DeviceInfoSet,
                                                            ref SP_DEVINFO_DATA DeviceInfoData,
                                                            SPDRP Property)
        {
            regBytes = null;
            byte[]            tmp = new byte[1024];
            int               iReqSize;
            RegistryValueKind regValueType;

            if (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, Property, out regValueType, tmp, tmp.Length, out iReqSize))
            {
                UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "SetupDiGetDeviceRegistryProperty", typeof(SetupApi));
                return(false);
            }
            regBytes = new byte[iReqSize];
            Array.Copy(tmp, regBytes, regBytes.Length);
            return(true);
        }
Example #19
0
        public static bool SetupDiGetDeviceRegistryProperty(out byte[] regBytes,
                                                            IntPtr DeviceInfoSet,
                                                            ref SP_DEVINFO_DATA DeviceInfoData,
                                                            SPDRP Property)
        {
            regBytes = null;
            byte[]            tmp = new byte[1024];
            int               iReqSize;
            RegistryValueKind regValueType;

            if (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, Property, out regValueType, tmp, tmp.Length, out iReqSize))
            {
                //usb_error("usb_registry_match_no_hubs(): getting hardware id failed");
                return(false);
            }
            regBytes = new byte[iReqSize];
            Array.Copy(tmp, regBytes, regBytes.Length);
            return(true);
        }
Example #20
0
        private static byte[] GetProperty(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP property, out int regType)
        {
            uint requiredSize;

            if (!SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, property, IntPtr.Zero, IntPtr.Zero, 0, out requiredSize))
            {
                if (Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER)
                    throw APIException.Win32("Failed to get buffer size for device registry property.");
            }

            byte[] buffer = new byte[requiredSize];

            if (!SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, property, out regType, buffer, (uint)buffer.Length, out requiredSize))
                throw APIException.Win32("Failed to get device registry property.");

            return buffer;

           
           
        }
Example #21
0
            string[] GetStrings(SPDRP property)
            {
                var ptr = Marshal.AllocHGlobal(KERNEL32.DefaultStructBufferSize);

                try
                {
                    var  valueKind    = new RegistryValueKind();
                    uint requiredSize = 0;
                    var  devInfo      = deviceInfo;
                    var  result       = SetupDiGetDeviceRegistryProperty(Handle, ref devInfo, property, ref valueKind, ptr, (uint)KERNEL32.DefaultStructBufferSize, ref requiredSize);
                    if (!result)
                    {
                        var error = Marshal.GetLastWin32Error();
                        if (error == 13)
                        {
                            return(null);
                        }
                        if (error != 0)
                        {
                            throw new Win32ErrorException(error);
                        }
                    }
                    switch (valueKind)
                    {
                    case RegistryValueKind.Binary: throw new InvalidCastException(string.Format("Please use GetData() for BINARY values!"));

                    case RegistryValueKind.DWord: throw new InvalidCastException(string.Format("Please use GetUInt32() for DWORD values!"));

                    case RegistryValueKind.QWord: throw new InvalidCastException(string.Format("Please use GetUInt64() for QWORD values!"));

                    case RegistryValueKind.MultiString: break;

                    case RegistryValueKind.String: throw new InvalidCastException(string.Format("Please use GetString() for *_SZ values!"));

                    default: throw new NotImplementedException(string.Format("Not implemented registry value kind {0}!", valueKind));
                    }
                    var str = Marshal.PtrToStringAuto(ptr, (int)requiredSize - 1);
                    return(str.Split('\0'));
                }
                finally { Marshal.FreeHGlobal(ptr); }
            }
        private static string[] GetMultiStringProperty(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP property)
        {
            int regType;

            byte[] buffer = GetProperty(deviceInfoSet, deviceInfoData, property, out regType);
            if (regType != (int)RegTypes.REG_MULTI_SZ)
            {
                throw new APIException("Invalid registry type returned for device property.");
            }

            string fullString = System.Text.Encoding.Unicode.GetString(buffer);

            return(fullString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries));
        }
    private static string GetDeviceDescription(IntPtr hDeviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP property)
    {
        StringBuilder descriptionBuf = new StringBuilder(256);
        uint          propRegDataType;
        uint          length  = (uint)descriptionBuf.Capacity;
        bool          success = SetupDiGetDeviceRegistryProperty(hDeviceInfoSet, ref deviceInfoData, property,
                                                                 out propRegDataType, descriptionBuf, length, out length);

        if (!success)
        {
            throw new Exception("Can not read registry value PortName for device " + deviceInfoData.ClassGuid);
        }
        string deviceDescription = descriptionBuf.ToString();

        return(deviceDescription);
    }
Example #24
0
 public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet,
     ref SP_DEVINFO_DATA DeviceInfoData,
     SPDRP Property,
     out RegistryValueKind PropertyRegDataType,
     byte[] PropertyBuffer,
     int PropertyBufferSize,
     out int RequiredSize);
Example #25
0
        private static string GetStringPropertyForDevice(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP propId)
        {
            uint proptype;
            uint outsize = 0;
            var  buffer  = new byte[MAX_DEVICE_LEN];
            var  result  = DeviceDetector.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, (uint)propId, out proptype, buffer, (uint)buffer.Length, out outsize);

            if (!result)
            {
                var errcode = Marshal.GetLastWin32Error();
                if (errcode == ERROR_INVALID_DATA)
                {
                    return(null);
                }
                throw new Exception("Error calling SetupDiGetDeviceRegistryPropertyW: " + errcode);
            }
            var o = "";

            if (outsize > 0)
            {
                switch (proptype)
                {
                case (uint)REG.REG_SZ:
                    o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2);
                    break;

                case (uint)REG.REG_MULTI_SZ:
                    o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2);
                    o = o.Trim('\0').Replace("\0", "\r\n");
                    break;

                default:
                    break;
                }
            }
            return(o);
        }
Example #26
0
 /// <summary>
 /// Gets a device property/key from the registry.  See the <see cref="SPDRP"/> enumeration for more information.
 /// </summary>
 /// <param name="spdrp">The name of the property to retrieve.</param>
 /// <returns></returns>
 public object this[SPDRP spdrp]
 {
     get
     {
         object temp;
         mDeviceProperties.TryGetValue(spdrp.ToString(), out temp);
         return temp;
     }
 }
Example #27
0
        private static uint GetDevicePropertyUInt(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, SPDRP property)
        {
            var buffer = GetDevicePropertyBytes(DeviceInfoSet, DeviceInfoData, property);

            if (buffer is null)
            {
                return(0);
            }

            return(BitConverter.ToUInt32(buffer, 0));
        }
Example #28
0
        private static byte[] GetDevicePropertyBytes(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, SPDRP property)
        {
            byte[] buffer     = null;
            uint   bufferSize = 0;

            SetupDiGetDeviceRegistryProperty(
                DeviceInfoSet,
                ref DeviceInfoData,
                property,
                out _,
                buffer,
                bufferSize,
                out uint requiredSize);

            buffer     = new byte[requiredSize];
            bufferSize = requiredSize;

            if (!SetupDiGetDeviceRegistryProperty(
                    DeviceInfoSet,
                    ref DeviceInfoData,
                    property,
                    out _,
                    buffer,
                    bufferSize,
                    out requiredSize))
            {
                return(Array.Empty <byte>());
            }

            return(buffer);
        }
Example #29
0
 public static extern bool SetupDiGetDeviceRegistryProperty(Handle deviceInfoSet, [In] ref SP_DEVINFO_DATA deviceInfoData, SPDRP property, ref RegistryValueKind propertyRegDataType, IntPtr propertyBuffer, uint propertyBufferSize, ref uint requiredSize);
 private static extern bool SetupDiGetDeviceRegistryProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, SPDRP Property, IntPtr PropertyRegDataType, IntPtr PropertyBuffer, uint PropertyBufferSize, out UInt32 RequiredSize);
Example #31
0
 public static bool SetupDiGetDeviceRegistryProperty(out string[] regMultiSZ,
     IntPtr DeviceInfoSet,
     ref SP_DEVINFO_DATA DeviceInfoData,
     SPDRP Property)
 {
     regMultiSZ = null;
     string tmp;
     if (SetupDiGetDeviceRegistryProperty(out tmp, DeviceInfoSet, ref DeviceInfoData, Property))
     {
         regMultiSZ = tmp.Split(new char[] {'\0'}, StringSplitOptions.RemoveEmptyEntries);
         return true;
     }
     return false;
 }
Example #32
0
 public static bool SetupDiGetDeviceRegistryProperty(out uint value,
                                             IntPtr DeviceInfoSet,
                                             ref SP_DEVINFO_DATA DeviceInfoData,
                                             SPDRP Property)
 {
     value = 0;
     uint[] tmp = new uint[1];
     int iReqSize;
     RegistryValueKind regValueType;
     if (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, Property, out regValueType, tmp,tmp.Length*Marshal.SizeOf(typeof(uint)), out iReqSize))
     {
         //usb_error("usb_registry_match_no_hubs(): getting hardware id failed");
         return false;
     }
     value = tmp[0];
     return true;
 }
Example #33
0
        private static string[] GetMultiStringProperty(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP property)
        {
            int regType;
            byte[] buffer = GetProperty(deviceInfoSet, deviceInfoData, property, out regType);
            if (regType != (int)RegTypes.REG_MULTI_SZ)
                throw new APIException("Invalid registry type returned for device property.");

            string fullString = System.Text.Encoding.Unicode.GetString(buffer);

            return fullString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);

        }
Example #34
0
        private static string GetStringPropertyForDevice(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP propId)
        {
            uint proptype;
            uint outsize = 0;
            // Get buffer size.
            var result = NativeMethods.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, (uint)propId, out proptype, null, 0, out outsize);

            if (!result)
            {
                var errorCode = Marshal.GetLastWin32Error();
                if (errorCode == ERROR_INVALID_DATA)
                {
                    return(null);
                }
                // We can safely ignore other errors when retrieving buffer size.
            }
            var buffer = new byte[outsize];

            // Get data.
            result = NativeMethods.SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, (uint)propId, out proptype, buffer, buffer.Length, out outsize);
            if (!result)
            {
                var errorCode = Marshal.GetLastWin32Error();
                if (errorCode == ERROR_INVALID_DATA)
                {
                    return(null);
                }
                var error = new Win32Exception(errorCode);
                throw new Exception("Error calling SetupDiGetDeviceRegistryPropertyW: " + error.ToString());
            }
            var o = "";

            if (outsize > 0)
            {
                var type = (REG)proptype;
                switch (type)
                {
                case REG.REG_SZ:
                    o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2);
                    break;

                case REG.REG_MULTI_SZ:
                    o = Encoding.Unicode.GetString(buffer, 0, (int)outsize - 2);
                    o = o.Trim('\0').Replace("\0", "\r\n");
                    break;

                default:
                    o = string.Format("{0} 0x{1}", type, string.Join("", buffer.Take((int)outsize).Select(x => x.ToString("X2"))));
                    break;
                }
            }
            return(o);
        }
        private static byte[] GetProperty(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP property, out int regType)
        {
            uint requiredSize;

            if (!SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, property, IntPtr.Zero, IntPtr.Zero, 0, out requiredSize))
            {
                if (Marshal.GetLastWin32Error() != ERROR_INSUFFICIENT_BUFFER)
                {
                    throw APIException.Win32("Failed to get buffer size for device registry property.");
                }
            }

            byte[] buffer = new byte[requiredSize];

            if (!SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, property, out regType, buffer, (uint)buffer.Length, out requiredSize))
            {
                throw APIException.Win32("Failed to get device registry property.");
            }

            return(buffer);
        }
        // todo: is the queried data always available, or should we check ERROR_INVALID_DATA?
        private static string GetStringProperty(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP property)
        {
            int regType;

            byte[] buffer = GetProperty(deviceInfoSet, deviceInfoData, property, out regType);
            if (regType != (int)RegTypes.REG_SZ)
            {
                throw new APIException("Invalid registry type returned for device property.");
            }

            // sizof(char), 2 bytes, are removed to leave out the string terminator
            return(System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length - sizeof(char)));
        }
Example #37
0
 public static extern bool SetupDiGetDeviceRegistryProperty(
     IntPtr deviceInfoSet,
     SP_DEVINFO_DATA deviceInfoData,
     SPDRP property,
     out int propertyRegDataType,
     byte[] propertyBuffer,
     int propertyBufferSize,
     out int requiredSize
 );
Example #38
0
        // todo: is the queried data always available, or should we check ERROR_INVALID_DATA?
        private static string GetStringProperty(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, SPDRP property)
        {
            int regType;
            byte[] buffer = GetProperty(deviceInfoSet, deviceInfoData, property, out regType);
            if (regType != (int)RegTypes.REG_SZ)
                throw new APIException("Invalid registry type returned for device property.");

            // sizof(char), 2 bytes, are removed to leave out the string terminator
            return System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length - sizeof(char));
        }
Example #39
0
 public static bool SetupDiGetDeviceRegistryProperty(out string regSZ, IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, SPDRP Property)
 {
     regSZ = null;
     byte[] tmp;
     if (SetupDiGetDeviceRegistryProperty(out tmp, DeviceInfoSet, ref DeviceInfoData, Property))
     {
         regSZ = Encoding.Unicode.GetString(tmp).TrimEnd(new char[] { '\0' });
         return(true);
     }
     return(false);
 }
Example #40
0
        private static string GetDevicePropertyString(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, SPDRP property)
        {
            var buffer = GetDevicePropertyBytes(DeviceInfoSet, DeviceInfoData, property);

            if (buffer is null)
            {
                return(string.Empty);
            }

            return(Encoding.Unicode.GetString(buffer).TrimEnd((char)0));
        }
Example #41
0
 public static bool SetupDiGetDeviceRegistryProperty(out string regSZ, IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, SPDRP Property)
 {
     regSZ = null;
     byte[] tmp;
     if (SetupDiGetDeviceRegistryProperty(out tmp, DeviceInfoSet, ref DeviceInfoData, Property))
     {
         regSZ = Encoding.Unicode.GetString(tmp).TrimEnd(new char[] {'\0'});
         return true;
     }
     return false;
 }
Example #42
0
        private static byte[] GetDevicePropertyBytes(IntPtr DeviceInfoSet, SP_DEVINFO_DATA DeviceInfoData, SPDRP property)
        {
            SetupDiGetDeviceRegistryProperty(
                DeviceInfoSet,
                ref DeviceInfoData,
                property,
                out _,
                null,
                0,
                out uint requiredSize);

            var buffer = new byte[requiredSize];

            if (SetupDiGetDeviceRegistryProperty(
                    DeviceInfoSet,
                    ref DeviceInfoData,
                    property,
                    out _,
                    buffer,
                    requiredSize,
                    out _))
            {
                return(buffer);
            }
            return(null);
        }
Example #43
0
 public static bool SetupDiGetDeviceRegistryProperty(out byte[] regBytes,
     IntPtr DeviceInfoSet,
     ref SP_DEVINFO_DATA DeviceInfoData,
     SPDRP Property)
 {
     regBytes = null;
     byte[] tmp = new byte[1024];
     int iReqSize;
     RegistryValueKind regValueType;
     if (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, Property, out regValueType, tmp, tmp.Length, out iReqSize))
     {
         UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "SetupDiGetDeviceRegistryProperty", typeof(SetupApi));
         return false;
     }
     regBytes = new byte[iReqSize];
     Array.Copy(tmp, regBytes, regBytes.Length);
     return true;
 }
Example #44
0
 public static bool SetupDiGetDeviceRegistryProperty(out byte[] regBytes,
                                                     IntPtr DeviceInfoSet,
                                                     ref SP_DEVINFO_DATA DeviceInfoData,
                                                     SPDRP Property)
 {
     regBytes = null;
     byte[] tmp = new byte[1024];
     int iReqSize;
     RegistryValueKind regValueType;
     if (!SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, Property, out regValueType, tmp, tmp.Length, out iReqSize))
     {
         //usb_error("usb_registry_match_no_hubs(): getting hardware id failed");
         return false;
     }
     regBytes = new byte[iReqSize];
     Array.Copy(tmp, regBytes, regBytes.Length);
     return true;
 }
Example #45
0
 public static extern bool SetupDiSetDeviceRegistryProperty(IntPtr DeviceInfoSet,
                                                            ref SP_DEVINFO_DATA DeviceInfoData,
                                                            SPDRP Property,
                                                            [MarshalAs(UnmanagedType.AsAny),In] object PropertyBuffer,
                                                            int PropertyBufferSize);