public static extern bool SetupDiGetDeviceRegistryProperty(
     SafeHandle deviceInfoSet,
     ref SP_DEVINFO_DATA deviceInfoData,
     SetupDiGetDeviceRegistryPropertyEnum property,
     [MarshalAs(UnmanagedType.U4)] out uint propertyRegDataType,
     SafeGlobalMemoryBufferHandle propertyBuffer,
     [MarshalAs(UnmanagedType.U4)] uint propertyBufferSize,
     IntPtr requiredSize);
示例#2
0
 private static extern bool SetupDiGetDeviceRegistryProperty(
     IntPtr DeviceInfoSet,
     ref SP_DEVINFO_DATA DeviceInfoData,
     SetupDiGetDeviceRegistryPropertyEnum Property,
     out int PropertyRegDataType,
     IntPtr PropertyBuffer,
     int PropertyBufferSize,
     out int RequiredSize
     );
        private string GetDeviceProperty(IntPtr deviceEnumHandle, SetupDiGetDeviceRegistryPropertyEnum property, SP_DEVINFO_DATA devInfodata)
        {
            string devicePropertyValue = null;

            byte[] ptrBuf = new byte[256];

            if (!SetupDiGetDeviceRegistryProperty(deviceEnumHandle, ref devInfodata, (UInt32)property, out UInt32 RegType, ptrBuf, 256, out UInt32 RequiredSize))
            {
                LogHelper.Log("GetDeviceProperty: Failed: Error: {0}", Marshal.GetLastWin32Error());
            }
示例#4
0
        static uint getDWORDProp(IntPtr h, SP_DEVINFO_DATA da, SetupDiGetDeviceRegistryPropertyEnum prop)
        {
            UInt32 requiredSize;
            UInt32 regType;

            byte[] ptrBuf = new byte[4];
            if (!SetupDiGetDeviceRegistryProperty(h, ref da, (uint)prop, out regType, ptrBuf, 4, out requiredSize))
            {
                throw new InvalidOperationException("Error getting DWORD property");
            }

            if (regType != (uint)RegType.REG_DWORD || requiredSize != 4)
            {
                throw new InvalidOperationException("Property is not a REG_DWORD");
            }

            return(BitConverter.ToUInt32(ptrBuf, 0));
        }
        /// <summary>
        /// Gets the property for SetupDiXXX function
        /// </summary>
        /// <param name="deviceHandle">The device handle.</param>
        /// <param name="deviceInformationData">The device information data.</param>
        /// <param name="property">The property.</param>
        /// <returns>String representation of the property</returns>
        internal static string GetProperty(IntPtr deviceHandle, SP_DEVINFO_DATA deviceInformationData, SetupDiGetDeviceRegistryPropertyEnum property)
        {
            bool functionAnswer = false;
            UInt32 propertyRegDataType = 0;
            IntPtr propertyBuffer = IntPtr.Zero;
            uint requireSize = 0;
            uint propertyMaximumSize = 1024;
            string actualData = string.Empty;

            try
            {
                propertyBuffer = Marshal.AllocHGlobal((int)propertyMaximumSize);

                functionAnswer = UnsafeNativeMethods.SetupDiGetDeviceRegistryProperty(deviceHandle,
                                                                                        ref deviceInformationData,
                                                                                        (uint)property,
                                                                                        out propertyRegDataType,
                                                                                        propertyBuffer,
                                                                                        propertyMaximumSize,
                                                                                        out requireSize);

                if (functionAnswer == false)
                {
                    if (Marshal.GetLastWin32Error() == UnsafeNativeMethods.ERROR_INVALID_DATA)
                    {
                        Marshal.ThrowExceptionForHR(UnsafeNativeMethods.ERROR_INVALID_DATA);
                    }
                }

                // We have the answer
                actualData = Marshal.PtrToStringAuto(propertyBuffer);
                return actualData;
            }
            catch (Exception exp_gen)
            {
                throw new Win32Exception("Could not receive property", exp_gen);
            }
            finally
            {
                if (propertyBuffer != IntPtr.Zero)
                    Marshal.FreeHGlobal(propertyBuffer);
            }
        }
示例#6
0
        private static string getStrProp(IntPtr hDevInfo, SP_DEVINFO_DATA cDevInfo, SetupDiGetDeviceRegistryPropertyEnum p)
        {
            string result   = null;
            int    buffSize = 256;

            while (true)
            {
                int    cbBuff = buffSize;
                IntPtr pBuff  = Marshal.AllocHGlobal(cbBuff);
                try
                {
                    int unused_propType = 0;

                    if (!SetupDiGetDeviceRegistryProperty(hDevInfo, ref cDevInfo, p, out unused_propType, pBuff, cbBuff, out cbBuff))
                    {
                        var lastError = Marshal.GetLastWin32Error();
                        if (lastError != ERROR_INSUFFICIENT_BUFFER)
                        {
                            throw new Win32Exception(lastError);
                        }

                        buffSize = buffSize * 2;
                    }
                    else
                    {
                        result = Marshal.PtrToStringUni(pBuff);
                        break;
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(pBuff);
                }
            }

            return(result);
        }
        /// <summary>
        /// Gets the property for SetupDiXXX function
        /// </summary>
        /// <param name="deviceHandle">The device handle.</param>
        /// <param name="deviceInformationData">The device information data.</param>
        /// <param name="property">The property.</param>
        /// <returns>String representation of the property</returns>
        internal static string GetProperty(IntPtr deviceHandle, SP_DEVINFO_DATA deviceInformationData, SetupDiGetDeviceRegistryPropertyEnum property)
        {
            bool   functionAnswer      = false;
            UInt32 propertyRegDataType = 0;
            IntPtr propertyBuffer      = IntPtr.Zero;
            uint   requireSize         = 0;
            uint   propertyMaximumSize = 1024;
            string actualData          = string.Empty;

            try
            {
                propertyBuffer = Marshal.AllocHGlobal((int)propertyMaximumSize);

                functionAnswer = UnsafeNativeMethods.SetupDiGetDeviceRegistryProperty(deviceHandle,
                                                                                      ref deviceInformationData,
                                                                                      (uint)property,
                                                                                      out propertyRegDataType,
                                                                                      propertyBuffer,
                                                                                      propertyMaximumSize,
                                                                                      out requireSize);

                if (functionAnswer == false)
                {
                    if (Marshal.GetLastWin32Error() == UnsafeNativeMethods.ERROR_INVALID_DATA)
                    {
                        Marshal.ThrowExceptionForHR(UnsafeNativeMethods.ERROR_INVALID_DATA);
                    }
                }

                // We have the answer
                actualData = Marshal.PtrToStringAuto(propertyBuffer);
                return(actualData);
            }
            catch (Exception exp_gen)
            {
                throw new Win32Exception("Could not receive property", exp_gen);
            }
            finally
            {
                if (propertyBuffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(propertyBuffer);
                }
            }
        }
示例#8
0
    static byte[] GetDeviceProperty(IntPtr DeviceInfoSet, Win32DeviceMgmt.SP_DEVINFO_DATA DeviceInfoData, SetupDiGetDeviceRegistryPropertyEnum property)
    {
        StringBuilder strId = new StringBuilder(0);

        byte[] ptrBuf = null;
        UInt32 RegType;
        UInt32 iRequiredSize = 0;
        UInt32 iSize         = 0;
        bool   iRet          = Win32DeviceMgmt.SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData,
                                                                                (uint)property, out RegType, ptrBuf, iSize, out iRequiredSize);

        ptrBuf = new byte[iRequiredSize];
        iSize  = iRequiredSize;
        iRet   = Win32DeviceMgmt.SetupDiGetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData,
                                                                  (uint)property, out RegType, ptrBuf, iSize, out iRequiredSize);
        if (iRet)
        {
            return(ptrBuf);
        }
        return(new byte[0]);
    }
示例#9
0
 static Guid GetDevicePropertyGuid(IntPtr DeviceInfoSet, Win32DeviceMgmt.SP_DEVINFO_DATA DeviceInfoData, SetupDiGetDeviceRegistryPropertyEnum property)
 {
     byte[] ptrBuf = GetDeviceProperty(DeviceInfoSet, DeviceInfoData, property);
     return(new Guid(ptrBuf));
 }
示例#10
0
 static String GetDevicePropertyString(IntPtr DeviceInfoSet, Win32DeviceMgmt.SP_DEVINFO_DATA DeviceInfoData, SetupDiGetDeviceRegistryPropertyEnum property)
 {
     byte[] ptrBuf = GetDeviceProperty(DeviceInfoSet, DeviceInfoData, property);
     return(ptrBuf.ToStrAuto());
 }
示例#11
0
 static extern bool SetupDiGetDeviceRegistryProperty(
     IntPtr deviceInfoSet,
     ref SP_DEVINFO_DATA deviceInfoData,
     SetupDiGetDeviceRegistryPropertyEnum property,
     IntPtr propertyRegDataType,
     ushort[] propertyBuffer,
     uint propertyBufferSize,
     IntPtr requiredSize
     );
 internal static extern bool SetupDiGetDeviceRegistryProperty(SafeHandle deviceInfoSet, ref SpDeviceInfoData deviceInfoData, SetupDiGetDeviceRegistryPropertyEnum property, [MarshalAs(UnmanagedType.U4)] out uint propertyRegDataType, SafeGlobalMemoryBufferHandle propertyBuffer, [MarshalAs(UnmanagedType.U4)] uint propertyBufferSize, IntPtr requiredSize);