示例#1
0
        private static string SetupDiGetDeviceProperty(IntPtr deviceInfoSet, ref SpDevInfoData deviceInfoData, DevPropKey propertyKey)
        {
            var  localPropertyKey = propertyKey;
            uint propertyType;
            var  buffer = new byte[0];
            uint requiredSize;

            // First call retrieves the necessary buffer size
            SetupDiGetDeviceProperty(deviceInfoSet, ref deviceInfoData, ref localPropertyKey, out propertyType, buffer, (uint)buffer.Length, out requiredSize, 0);
            var err = Marshal.GetLastWin32Error();

            if (err != ERROR_INSUFICCIENT_BUFFER)
            {
                throw Marshal.GetExceptionForHR(Win32ErrorToHResult(err));
            }

            buffer = new byte[requiredSize];
            if (!SetupDiGetDeviceProperty(deviceInfoSet, ref deviceInfoData, ref localPropertyKey, out propertyType, buffer, (uint)buffer.Length, out requiredSize, 0))
            {
                throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            // We only understand strings.
            if (propertyType != DEVPROP_TYPE_STRING)
            {
                throw new NotSupportedException("Unknown property type " + propertyType);
            }
            return(Encoding.Unicode.GetString(buffer).TrimEnd('\0'));
        }
示例#2
0
        void GetSizeForDisplayDevices(List <Models.Monitor> monitors, CancellationToken token)
        {
            IntPtr devInfo = _nativeMethods.SetupDiGetClassDevsEx(new[] { NativeConstants.MonitorClassId }, null,
                                                                  IntPtr.Zero, NativeConstants.DIGCF_PRESENT | NativeConstants.DIGCF_PROFILE, IntPtr.Zero, null,
                                                                  IntPtr.Zero);

            if (devInfo == IntPtr.Zero)
            {
                return;
            }

            for (uint i = 0; ShouldDisplayDeviceLoopContinue(token); ++i)
            {
                var devInfoData = new SpDevInfoData {
                    Size = (uint)_marshal.SizeOf <SpDevInfoData>()
                };

                if (!_nativeMethods.SetupDiEnumDeviceInfo(devInfo, i, ref devInfoData))
                {
                    continue;
                }

                var instanceId = new char[NativeConstants.MAX_PATH];

                if (!_nativeMethods.SetupDiGetDeviceInstanceId(devInfo, ref devInfoData, instanceId,
                                                               NativeConstants.MAX_PATH, IntPtr.Zero))
                {
                    continue;
                }

                var instanceIdSize = Array.IndexOf(instanceId, '\0');

                var instanceIdString =
                    new string(instanceId, 0, instanceIdSize > 0 ? instanceIdSize : instanceId.Length);
                var monitor = monitors.Find(m => instanceIdString.Contains(Helpers.GetDeviceId(m.Id)));

                if (monitor == null)
                {
                    continue;
                }

                IntPtr edidRegKey = _nativeMethods.SetupDiOpenDevRegKey(devInfo, ref devInfoData,
                                                                        NativeConstants.DICS_FLAG_GLOBAL, 0, NativeConstants.DIREG_DEV, NativeConstants.KEY_READ);

                if (edidRegKey != NativeConstants.INVALID_HANDLE_VALUE)
                {
                    var edid = GetMonitorEdidFromRegistry(edidRegKey);
                    if (edid.Length > 0)
                    {
                        monitor.Edid       = _edidFactory.Create(edid);
                        monitor.Dimensions = Helpers.GetMonitorSizeFromEdid(edid);
                    }
                }

                _nativeMethods.RegCloseKey(edidRegKey);
            }

            _nativeMethods.SetupDiDestroyDeviceInfoList(devInfo);
        }
 public static extern bool SetupDiGetDeviceProperty([In] IntPtr deviceInfoSet,
                                                    [In, Out] ref SpDevInfoData deviceInfoData,
                                                    [In, Out] ref DevPropKey propertyKey,
                                                    [Out] out int propertyType,
                                                    [In, Out] IntPtr propertyBuffer,
                                                    [In] int propertyBufferSize,
                                                    [Out] out int requiredSize,
                                                    [In, Optional] int flags);
示例#4
0
 private static void SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, out SpDevInfoData deviceInfoData)
 {
     // This wrapper only enumerates the first device in the device info set.
     deviceInfoData = new SpDevInfoData {
         cbSize = (uint)Marshal.SizeOf(typeof(SpDevInfoData))
     };
     if (!SetupDiEnumDeviceInfo(deviceInfoSet, 0, ref deviceInfoData))
     {
         throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
     }
 }
示例#5
0
 public static extern IntPtr SetupDiOpenDevRegKey(IntPtr hDeviceInfoSet, SpDevInfoData deviceInfoData, uint scope,
                                                  uint hwProfile, uint parameterRegistryValueKind, uint samDesired);
示例#6
0
 public static extern bool SetupDiSetDeviceRegistryProperty(IntPtr deviceInfoSet, SpDevInfoData devInfoData,
                                                            int property, byte[] propertyBuffer, int propertyBufferSize);
示例#7
0
 public static extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoSet, SpDevInfoData deviceInfoData,
                                                       ref Guid interfaceClassGuid, uint memberIndex, SpDeviceInterfaceData deviceInterfaceData);
示例#8
0
 public static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, uint memberIndex, SpDevInfoData deviceInfoData);
示例#9
0
 internal static extern IntPtr SetupDiOpenDevRegKey(IntPtr deviceInfoSet,
                                                    ref SpDevInfoData deviceInfoData,
                                                    uint scope,
                                                    uint hwProfile,
                                                    uint keyType,
                                                    uint samDesired);
示例#10
0
 public static extern bool SetupDiGetDeviceInstanceId(IntPtr deviceInfoSet, SpDevInfoData devInfoData,
                                                      [Out] byte[] deviceInstanceIdBuffer, int deviceInstanceIdSize, out int requiredSize);
 public static extern bool SetupDiEnumDeviceInfo([In] IntPtr deviceInfoSet,
                                                 [In] int memberIndex,
                                                 [In, Out] ref SpDevInfoData deviceInfoData);
示例#12
0
 public IntPtr SetupDiOpenDevRegKey(IntPtr deviceInfoSet, ref SpDevInfoData deviceInfoData, uint scope, uint hwProfile,
                                    uint keyType, uint samDesired)
 {
     return(NativeMethods.SetupDiOpenDevRegKey(deviceInfoSet, ref deviceInfoData, scope, hwProfile, keyType, samDesired));
 }
示例#13
0
 public bool SetupDiGetDeviceInstanceId(IntPtr deviceInfoSet, ref SpDevInfoData deviceInfoData, char[] deviceInstanceId,
                                        uint deviceInstanceIdSize, IntPtr requiredSize)
 {
     return(NativeMethods.SetupDiGetDeviceInstanceId(deviceInfoSet, ref deviceInfoData, deviceInstanceId, deviceInstanceIdSize,
                                                     requiredSize));
 }
示例#14
0
 public bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, uint memberIndex, ref SpDevInfoData deviceInfoData)
 {
     return(NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, memberIndex, ref deviceInfoData));
 }
示例#15
0
 private static extern bool SetupDiGetDeviceProperty(IntPtr deviceInfoSet, [In] ref SpDevInfoData deviceInfoData, [In] ref DevPropKey propertyKey, out uint propertyType,
                                                     [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5)][Out] byte[] buffer, uint bufferSize, out uint requiredSize, uint flags);
示例#16
0
 public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, SpDevInfoData devInfoData,
                                                            int property, out int regDataType, [Out] byte[] propertyBuffer, int propertyBufferSize, out int requiredSize);
示例#17
0
 public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoSet, SpDevInfoData deviceInfoData,
                                                            uint property, uint propertyRegDataType, StringBuilder propertyBuffer, uint propertyBufferSize, out uint requiredSize);
示例#18
0
 public static extern bool SetupDiEnumDeviceInfo(SafeDeviceInfoSetHandle DeviceInfoSet, uint MemberIndex, ref SpDevInfoData DeviceInfoData);
示例#19
0
 internal static extern bool SetupDiGetDeviceInstanceId(IntPtr deviceInfoSet,
                                                        ref SpDevInfoData deviceInfoData,
                                                        char[] deviceInstanceId,
                                                        uint deviceInstanceIdSize,
                                                        IntPtr requiredSize);
示例#20
0
 private static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, uint memberIndex, ref SpDevInfoData deviceInfoData);