Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Add EnumDisplaySettings #510

Merged
merged 2 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/User32.Tests/User32Facts+EnumDisplaySettingsFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright © .NET Foundation and Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using PInvoke;
using Xunit;
using static PInvoke.User32;

public partial class User32Facts
{
[Fact]
public void EnumDisplaySettings_Test()
{
// Arrange
var mode = DEVMODE.Create();

// Act
var result = EnumDisplaySettings(null, ENUM_CURRENT_SETTINGS, ref mode);

// Assert
Assert.True(result);
Assert.True(mode.dmPelsWidth > 0);
Assert.True(mode.dmPelsHeight > 0);
}

[Fact]
public void EnumDisplaySettingsEx_Test()
{
// Arrange
var mode = DEVMODE.Create();

// Act
var result = EnumDisplaySettingsEx(null, ENUM_CURRENT_SETTINGS, ref mode, EnumDisplaySettingsExFlags.EDS_RAWMODE);

// Assert
Assert.True(result);
Assert.True(mode.dmPelsWidth > 0);
Assert.True(mode.dmPelsHeight > 0);
}
}
17 changes: 14 additions & 3 deletions src/User32/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
PInvoke.User32.EnumDisplayDevicesFlags
PInvoke.User32.EnumDisplayDevicesFlags.EDD_GET_DEVICE_INTERFACE_NAME = 1 -> PInvoke.User32.EnumDisplayDevicesFlags
static PInvoke.User32.EnumDisplayDevices(string lpDevice, uint iDevNum, System.IntPtr lpDisplayDevice, PInvoke.User32.EnumDisplayDevicesFlags dwFlags) -> bool
static PInvoke.User32.EnumDisplayDevices(string lpDevice, uint iDevNum, ref PInvoke.DISPLAY_DEVICE lpDisplayDevice, PInvoke.User32.EnumDisplayDevicesFlags dwFlags) -> bool
static extern PInvoke.User32.EnumDisplayDevices(string lpDevice, uint iDevNum, PInvoke.DISPLAY_DEVICE* lpDisplayDevice, PInvoke.User32.EnumDisplayDevicesFlags dwFlags) -> bool
PInvoke.User32.EnumDisplaySettingsExFlags
PInvoke.User32.EnumDisplaySettingsExFlags.EDS_RAWMODE = 2 -> PInvoke.User32.EnumDisplaySettingsExFlags
PInvoke.User32.EnumDisplaySettingsExFlags.EDS_ROTATEDMODE = 4 -> PInvoke.User32.EnumDisplaySettingsExFlags
const PInvoke.User32.ENUM_CURRENT_SETTINGS = 4294967295 -> uint
const PInvoke.User32.ENUM_REGISTRY_SETTINGS = 4294967294 -> uint
static PInvoke.User32.EnumDisplayDevices(System.IntPtr lpDevice, uint iDevNum, System.IntPtr lpDisplayDevice, PInvoke.User32.EnumDisplayDevicesFlags dwFlags) -> bool
static PInvoke.User32.EnumDisplayDevices(char[] lpDevice, uint iDevNum, ref PInvoke.DISPLAY_DEVICE lpDisplayDevice, PInvoke.User32.EnumDisplayDevicesFlags dwFlags) -> bool
static PInvoke.User32.EnumDisplaySettings(System.IntPtr lpszDeviceName, uint iModeNum, System.IntPtr lpDevMode) -> bool
static PInvoke.User32.EnumDisplaySettings(char[] lpszDeviceName, uint iModeNum, ref PInvoke.DEVMODE lpDevMode) -> bool
static PInvoke.User32.EnumDisplaySettingsEx(System.IntPtr lpszDeviceName, uint iModeNum, System.IntPtr lpDevMode, PInvoke.User32.EnumDisplaySettingsExFlags dwFlags) -> bool
static PInvoke.User32.EnumDisplaySettingsEx(char[] lpszDeviceName, uint iModeNum, ref PInvoke.DEVMODE lpDevMode, PInvoke.User32.EnumDisplaySettingsExFlags dwFlags) -> bool
static extern PInvoke.User32.EnumDisplayDevices(char* lpDevice, uint iDevNum, PInvoke.DISPLAY_DEVICE* lpDisplayDevice, PInvoke.User32.EnumDisplayDevicesFlags dwFlags) -> bool
static extern PInvoke.User32.EnumDisplaySettings(char* lpszDeviceName, uint iModeNum, PInvoke.DEVMODE* lpDevMode) -> bool
static extern PInvoke.User32.EnumDisplaySettingsEx(char* lpszDeviceName, uint iModeNum, PInvoke.DEVMODE* lpDevMode, PInvoke.User32.EnumDisplaySettingsExFlags dwFlags) -> bool
static extern PInvoke.User32.UnregisterClass(string lpClassName, System.IntPtr hInstance) -> bool
static readonly PInvoke.User32.DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED -> System.IntPtr
32 changes: 32 additions & 0 deletions src/User32/User32+EnumDisplaySettingsExFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright © .NET Foundation and Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace PInvoke
{
using System;

/// <content>
/// Contains the <see cref="EnumDisplaySettingsExFlags"/> nested type.
/// </content>
public partial class User32
{
/// <summary>
/// Flags for <see cref="EnumDisplaySettingsEx(char*, uint, DEVMODE*, EnumDisplaySettingsExFlags)"/>.
/// </summary>
[Flags]
public enum EnumDisplaySettingsExFlags : uint
{
/// <summary>
/// If set, the function will return all graphics modes reported by the adapter driver, regardless of monitor capabilities.
/// Otherwise, it will only return modes that are compatible with current monitors.
/// </summary>
EDS_RAWMODE = 0x00000002,

/// <summary>
/// If set, the function will return graphics modes in all orientations.
/// Otherwise, it will only return modes that have the same orientation as the one currently set for the requested display.
/// </summary>
EDS_ROTATEDMODE = 0x00000004,
}
}
}
90 changes: 87 additions & 3 deletions src/User32/User32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public static partial class User32

public const uint WAIT_FAILED = 0xFFFFFFFF;

public const uint ENUM_CURRENT_SETTINGS = unchecked((uint)-1);

public const uint ENUM_REGISTRY_SETTINGS = unchecked((uint)-2);

/// <summary>
/// A bitmap that is drawn by the window that owns the menu. The application must process the WM_MEASUREITEM and
/// WM_DRAWITEM messages.
Expand Down Expand Up @@ -1673,7 +1677,7 @@ public static extern unsafe int LookupIconIdFromDirectoryEx(
/// </param>
/// <param name="lpDisplayDevice">
/// A pointer to a <see cref="DISPLAY_DEVICE"/> structure that receives information about the display device specified by <paramref name="iDevNum"/>.
/// Before calling <see cref="EnumDisplayDevices(string, uint, DISPLAY_DEVICE*, EnumDisplayDevicesFlags)"/>, you must initialize the member <see cref="DISPLAY_DEVICE.cb"/> to the size, in bytes, of <see cref="DISPLAY_DEVICE"/>.
/// Before calling <see cref="EnumDisplayDevices(char*, uint, DISPLAY_DEVICE*, EnumDisplayDevicesFlags)"/>, you must initialize the member <see cref="DISPLAY_DEVICE.cb"/> to the size, in bytes, of <see cref="DISPLAY_DEVICE"/>.
/// </param>
/// <param name="dwFlags">
/// Set this flag to <see cref="EnumDisplayDevicesFlags.EDD_GET_DEVICE_INTERFACE_NAME"/> to retrieve the device interface name for <c>GUID_DEVINTERFACE_MONITOR</c>, which is registered by the operating system on a per monitor basis.
Expand All @@ -1688,14 +1692,94 @@ public static extern unsafe int LookupIconIdFromDirectoryEx(
/// <remarks>
/// See https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaydevicesa#remarks.
/// </remarks>
[DllImport(nameof(User32), SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "EnumDisplayDevicesW")]
[DllImport(nameof(User32), SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern unsafe bool EnumDisplayDevices(
[MarshalAs(UnmanagedType.LPWStr)] string lpDevice,
[Friendly(FriendlyFlags.Array | FriendlyFlags.In)] char* lpDevice,
uint iDevNum,
[Friendly(FriendlyFlags.Bidirectional)] DISPLAY_DEVICE* lpDisplayDevice,
EnumDisplayDevicesFlags dwFlags);

/// <summary>
/// Retrieves information about one of the graphics modes for a display device. To retrieve information for all the graphics modes of a display device, make a series of calls to this function.
/// </summary>
/// <param name="lpszDeviceName">
/// A pointer to a null-terminated string that specifies the display device about whose graphics mode the function will obtain information.
/// This parameter is either <c>NULL</c> or a <see cref="DISPLAY_DEVICE.DeviceName"/> returned from <see cref="EnumDisplayDevices(char*, uint, DISPLAY_DEVICE*, EnumDisplayDevicesFlags)"/>.
/// A <c>NULL</c> value specifies the current display device on the computer on which the calling thread is running.
/// </param>
/// <param name="iModeNum">
/// <para>The type of information to be retrieved. This value can be a graphics mode index or one of the following values.</para>
/// <para><see cref="ENUM_CURRENT_SETTINGS"/>: Retrieve the current settings for the display device.</para>
/// <para><see cref="ENUM_REGISTRY_SETTINGS"/>: Retrieve the settings for the display device that are currently stored in the registry.</para>
/// <para>
/// Graphics mode indexes start at zero. To obtain information for all of a display device's graphics modes, make a series of calls to <see cref="EnumDisplaySettings(char*, uint, DEVMODE*)"/>, as follows: Set <paramref name="iModeNum"/> to zero for the first call,
/// and increment <paramref name="iModeNum"/> by one for each subsequent call.
/// </para>
/// <para>
/// Continue calling the function until the return value is zero.<br/>
/// When you call <see cref="EnumDisplaySettings(char*, uint, DEVMODE*)"/> with <paramref name="iModeNum"/> set to zero, the operating system initializes and caches information about the display device.<br/>
/// When you call <see cref="EnumDisplaySettings(char*, uint, DEVMODE*)"/> with <paramref name="iModeNum"/> set to a nonzero value,
/// the function returns the information that was cached the last time the function was called with iModeNum set to zero.
/// </para>
/// </param>
/// <param name="lpDevMode">
/// A pointer to a <see cref="DEVMODE"/> structure into which the function stores information about the specified graphics mode.
/// </param>
/// <remarks>
/// The function fails if <paramref name="iModeNum"/> is greater than the index of the display device's last graphics mode.
/// As noted in the description of the <paramref name="iModeNum"/> parameter, you can use this behavior to enumerate all of a display device's graphics modes.
/// </remarks>
/// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.</returns>
[DllImport(nameof(User32))]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern unsafe bool EnumDisplaySettings(
[Friendly(FriendlyFlags.Array | FriendlyFlags.In)] char* lpszDeviceName,
uint iModeNum,
[Friendly(FriendlyFlags.Bidirectional)]DEVMODE* lpDevMode);

/// <summary>
/// Retrieves information about one of the graphics modes for a display device. To retrieve information for all the graphics modes of a display device, make a series of calls to this function.
/// </summary>
/// <param name="lpszDeviceName">
/// A pointer to a null-terminated string that specifies the display device about whose graphics mode the function will obtain information.
/// This parameter is either <c>NULL</c> or a <see cref="DISPLAY_DEVICE.DeviceName"/> returned from <see cref="EnumDisplayDevices(char*, uint, DISPLAY_DEVICE*, EnumDisplayDevicesFlags)"/>.
/// A <c>NULL</c> value specifies the current display device on the computer on which the calling thread is running.
/// </param>
/// <param name="iModeNum">
/// <para>The type of information to be retrieved. This value can be a graphics mode index or one of the following values.</para>
/// <para><see cref="ENUM_CURRENT_SETTINGS"/>: Retrieve the current settings for the display device.</para>
/// <para><see cref="ENUM_REGISTRY_SETTINGS"/>: Retrieve the settings for the display device that are currently stored in the registry.</para>
/// <para>
/// Graphics mode indexes start at zero. To obtain information for all of a display device's graphics modes, make a series of calls to <see cref="EnumDisplaySettingsEx(char*, uint, DEVMODE*, EnumDisplaySettingsExFlags)"/>, as follows: Set <paramref name="iModeNum"/> to zero for the first call,
/// and increment <paramref name="iModeNum"/> by one for each subsequent call.
/// </para>
/// <para>
/// Continue calling the function until the return value is zero.<br/>
/// When you call <see cref="EnumDisplaySettingsEx(char*, uint, DEVMODE*, EnumDisplaySettingsExFlags)"/> with <paramref name="iModeNum"/> set to zero, the operating system initializes and caches information about the display device.<br/>
/// When you call <see cref="EnumDisplaySettingsEx(char*, uint, DEVMODE*, EnumDisplaySettingsExFlags)"/> with <paramref name="iModeNum"/> set to a nonzero value,
/// the function returns the information that was cached the last time the function was called with iModeNum set to zero.
/// </para>
/// </param>
/// <param name="lpDevMode">
/// A pointer to a <see cref="DEVMODE"/> structure into which the function stores information about the specified graphics mode.
/// </param>
/// <param name="dwFlags">
/// See documentation on the fields of <see cref="EnumDisplaySettingsExFlags"/>.
/// </param>
/// <remarks>
/// The function fails if <paramref name="iModeNum"/> is greater than the index of the display device's last graphics mode.
/// As noted in the description of the <paramref name="iModeNum"/> parameter, you can use this behavior to enumerate all of a display device's graphics modes.
/// </remarks>
/// <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.</returns>
[DllImport(nameof(User32))]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern unsafe bool EnumDisplaySettingsEx(
[Friendly(FriendlyFlags.Array | FriendlyFlags.In)] char* lpszDeviceName,
uint iModeNum,
[Friendly(FriendlyFlags.Bidirectional)] DEVMODE* lpDevMode,
EnumDisplaySettingsExFlags dwFlags);

[DllImport(nameof(User32), SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetClassInfoEx(
Expand Down
Loading