Created
May 5, 2026 16:03
-
-
Save LambdaSix/4625ee776cf4b872737c5709fe9c28f9 to your computer and use it in GitHub Desktop.
MonitorInfo, using Microsoft.WIndows.CsWin32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Runtime.InteropServices; | |
| using Windows.Win32; | |
| using Windows.Win32.Devices.Display; | |
| using Windows.Win32.Foundation; | |
| const int QDC_ONLY_ACTIVE_PATHS = 2; | |
| const int ERROR_INSUFFICIENT_BUFFER = 122; | |
| Span<DISPLAYCONFIG_PATH_INFO> paths = new Span<DISPLAYCONFIG_PATH_INFO>(); | |
| Span<DISPLAYCONFIG_MODE_INFO> modes = new Span<DISPLAYCONFIG_MODE_INFO>(); | |
| var flags = QUERY_DISPLAY_CONFIG_FLAGS.QDC_ONLY_ACTIVE_PATHS; | |
| WIN32_ERROR result = 0; | |
| do | |
| { | |
| result = PInvoke.GetDisplayConfigBufferSizes(flags, out var pathCount, out var modeCount); | |
| if (result != WIN32_ERROR.ERROR_SUCCESS) | |
| return PInvoke.HRESULT_FROM_WIN32(result); | |
| paths = new Span<DISPLAYCONFIG_PATH_INFO>(new DISPLAYCONFIG_PATH_INFO[pathCount]); | |
| modes = new Span<DISPLAYCONFIG_MODE_INFO>(new DISPLAYCONFIG_MODE_INFO[modeCount]); | |
| result = PInvoke.QueryDisplayConfig(flags, ref pathCount, paths, ref modeCount, modes); | |
| } | |
| while (result == WIN32_ERROR.ERROR_INSUFFICIENT_BUFFER); | |
| if (result != WIN32_ERROR.ERROR_SUCCESS) | |
| return PInvoke.HRESULT_FROM_WIN32(result); | |
| foreach (var mode in modes) | |
| { | |
| if (mode.infoType != DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET) | |
| continue; | |
| DISPLAYCONFIG_TARGET_DEVICE_NAME targetName = new(); | |
| targetName.header.adapterId = mode.adapterId; | |
| targetName.header.id = mode.id; | |
| targetName.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME; | |
| targetName.header.size = (uint)Marshal.SizeOf(targetName); | |
| result = (WIN32_ERROR)PInvoke.DisplayConfigGetDeviceInfo(ref targetName.header); | |
| if (result != WIN32_ERROR.ERROR_SUCCESS) | |
| return PInvoke.HRESULT_FROM_WIN32(result); | |
| Console.WriteLine($"Device Path: {targetName.monitorDevicePath}\n" + | |
| $"Monitor FriendlyName: {targetName.monitorFriendlyDeviceName}\n" + | |
| $"Connector Instance: {targetName.connectorInstance}\n" + | |
| $"EDID ManufacturerID: {targetName.edidManufactureId}\n" + | |
| $"EDID ProductId: {targetName.edidProductCodeId}\n"); | |
| } | |
| Console.WriteLine("---------------------------"); | |
| foreach (var path in paths) | |
| { | |
| DISPLAYCONFIG_TARGET_DEVICE_NAME targetName = new(); | |
| targetName.header.adapterId = path.targetInfo.adapterId; | |
| targetName.header.id = path.targetInfo.id; | |
| targetName.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME; | |
| targetName.header.size = (uint)Marshal.SizeOf(targetName); | |
| result = (WIN32_ERROR)PInvoke.DisplayConfigGetDeviceInfo(ref targetName.header); | |
| if (result != WIN32_ERROR.ERROR_SUCCESS) | |
| return PInvoke.HRESULT_FROM_WIN32(result); | |
| DISPLAYCONFIG_ADAPTER_NAME adapterName = new(); | |
| adapterName.header.adapterId = path.targetInfo.adapterId; | |
| adapterName.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_ADAPTER_NAME; | |
| adapterName.header.size = (uint)Marshal.SizeOf(adapterName); | |
| result = (WIN32_ERROR)PInvoke.DisplayConfigGetDeviceInfo(ref adapterName.header); | |
| if (result != WIN32_ERROR.ERROR_SUCCESS) | |
| return PInvoke.HRESULT_FROM_WIN32(result); | |
| Console.WriteLine($"Monitor Name: {targetName.monitorFriendlyDeviceName}\n" + | |
| $"AdapterID: {adapterName.header.adapterId.HighPart}\n" + | |
| $"DevicePath: {adapterName.adapterDevicePath}\n" + | |
| $"Target: {path.targetInfo.id}"); | |
| } | |
| return 0; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DISPLAYCONFIG_PATH_INFO | |
| DISPLAYCONFIG_MODE_INFO | |
| DEV_QUERY_FLAGS | |
| GetDisplayConfigBufferSizes | |
| QueryDisplayConfig | |
| DisplayConfigGetDeviceInfo | |
| QUERY_DISPLAY_CONFIG_FLAGS | |
| HRESULT_FROM_WIN32 | |
| DISPLAYCONFIG_TARGET_DEVICE_NAME | |
| DISPLAYCONFIG_ADAPTER_NAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment