public static string GetAdapterName(long adapterId)
        {
            var info = new DisplayConfigAdapterName(adapterId);
            var err  = DisplayConfigGetDeviceInfo_AdapterName(ref info);

            if (err != NO_ERROR)
            {
                throw Marshal.GetExceptionForHR(Win32ErrorToHResult(err));
            }
            return(info.adapterDevicePath);
        }
Esempio n. 2
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());
        }
 public static extern Win32Status DisplayConfigGetDeviceInfo(ref DisplayConfigAdapterName deviceName);
 private static extern int DisplayConfigGetDeviceInfo_AdapterName(ref DisplayConfigAdapterName info);