Exemplo n.º 1
0
        private static DisplayConfigTargetDeviceName[] GetDisplayConfigs()
        {
            var error = NativeMethods.GetDisplayConfigBufferSizes(QueryDeviceConfigFlags.QdcOnlyActivePaths, out var pathInfoElementsCount, out var modeInfoElementsCount);

            if (error != 0)
            {
                throw Log.ErrorAndCreateException <Win32Exception>($"Function {nameof(NativeMethods.GetDisplayConfigBufferSizes)} returns error code '{error}'");
            }

            var displayConfigs = new List <DisplayConfigTargetDeviceName>();

            var displayConfigPathInfos = new DisplayConfigPathInfo[pathInfoElementsCount];
            var displayConfigModeInfos = new DisplayConfigModeInfo[modeInfoElementsCount];

            error = NativeMethods.QueryDisplayConfig(QueryDeviceConfigFlags.QdcOnlyActivePaths, ref pathInfoElementsCount, displayConfigPathInfos, ref modeInfoElementsCount,
                                                     displayConfigModeInfos, IntPtr.Zero);

            if (error != 0)
            {
                throw Log.ErrorAndCreateException <Win32Exception>($"Function {nameof(NativeMethods.QueryDisplayConfig)} returns error code '{error}'");
            }

            foreach (var pathInfo in displayConfigPathInfos)
            {
                var adapterName = new DisplayConfigAdapterName(pathInfo.TargetInfo.AdapterId, DisplayConfig.DeviceInfoType.DisplayConfigDeviceInfoGetAdapterName);

                var targetDeviceName = new DisplayConfigTargetDeviceName()
                {
                    Header = DisplayConfigDeviceInfoHeader.Initialize(pathInfo.TargetInfo.AdapterId,
                                                                      pathInfo.TargetInfo.Id,
                                                                      DisplayConfig.DeviceInfoType.DisplayConfigDeviceInfoGetTargetName,
                                                                      (uint)Marshal.SizeOf <DisplayConfigTargetDeviceName>())
                };

                error = (int)NativeMethods.DisplayConfigGetDeviceInfo(ref targetDeviceName);

                try
                {
                    if (error != 0)
                    {
                        throw Log.ErrorAndCreateException <Win32Exception>($"Function {nameof(NativeMethods.DisplayConfigGetDeviceInfo)} returns error code '{error}'");
                    }

                    displayConfigs.Add(targetDeviceName);
                }
                catch (Win32Exception ex)
                {
                    Log.Error(ex);
                }
            }

            return(displayConfigs.ToArray());
        }
Exemplo n.º 2
0
 public DisplayConfigAdapterName(LUID adapter, DisplayConfig.DeviceInfoType deviceInfoType) : this()
 {
     _header = DisplayConfigDeviceInfoHeader.Initialize(adapter, deviceInfoType, (uint)Marshal.SizeOf <DisplayConfigAdapterName>());
 }