Created
May 18, 2018 10:55
-
-
Save jonitis/c4d35dd3a7ce8cf47d1f4cd5bf665f01 to your computer and use it in GitHub Desktop.
DisplayMonitor::FromIdAsync() failure
This file contains 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
#pragma comment(lib, "windowsapp") | |
#include <cstdio> | |
#include "winrt/base.h" | |
#include "winrt/Windows.Devices.Enumeration.h" | |
#include "winrt/Windows.Devices.Display.h" | |
using namespace std; | |
using namespace winrt; | |
using namespace Windows::Devices::Enumeration; | |
int main() | |
{ | |
init_apartment(); | |
using namespace Windows::Devices::Display; | |
const auto monitorSelector = DisplayMonitor::GetDeviceSelector(); | |
const auto devices = DeviceInformation::FindAllAsync(monitorSelector).get(); | |
for (const auto& device : devices) { | |
const auto id = device.Id(); | |
printf("id=%ls\n", id.c_str()); | |
DisplayMonitor monitor(nullptr); | |
try { | |
monitor = DisplayMonitor::FromIdAsync(id).get(); | |
} | |
catch (...) { | |
printf(" exception!\n"); | |
} | |
if (!monitor) | |
continue; | |
printf(" DisplayName = %ls\n", monitor.DisplayName().c_str()); | |
} | |
} |
BTW creating instance with DisplayMonitor::FromInterfaceIdAsync(interfaceId) works fine. But then you have to resort to win32 EnumDisplayDevices() function to obtain interfaceId.
Display adapter 0:
DeviceName = \\.\DISPLAY1
DeviceString = Intel(R) HD Graphics
StateFlags = 0x00000005
DeviceId =
DeviceKey = \Registry\Machine\System\CurrentControlSet\Control\Video\{0C0D3190-4DE2-11E8-9D24-00249B13949F}\0000
Display monitor 0:
DeviceName = \\.\DISPLAY1\Monitor0
DeviceString = Generic PnP Monitor
StateFlags = 0x00000003
DeviceId = MONITOR\DELA0A3\{4d36e96e-e325-11ce-bfc1-08002be10318}\0001
DeviceKey = \Registry\Machine\System\CurrentControlSet\Control\Class\{4d36e96e-e325-11ce-bfc1-08002be10318}\0001
DeviceId[DEVICE_INTERFACE_NAME] = \\?\DISPLAY#DELA0A3#4&27e3cd2c&0&UID51249920#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}
Display adapter 1:
DeviceName = \\.\DISPLAY2
DeviceString = Intel(R) HD Graphics
StateFlags = 0x00000001
DeviceId =
DeviceKey = \Registry\Machine\System\CurrentControlSet\Control\Video\{0C0D3190-4DE2-11E8-9D24-00249B13949F}\0001
Display monitor 0:
DeviceName = \\.\DISPLAY2\Monitor0
DeviceString = Generic PnP Monitor
StateFlags = 0x00000003
DeviceId = MONITOR\DELA0A3\{4d36e96e-e325-11ce-bfc1-08002be10318}\0002
DeviceKey = \Registry\Machine\System\CurrentControlSet\Control\Class\{4d36e96e-e325-11ce-bfc1-08002be10318}\0002
DeviceId[DEVICE_INTERFACE_NAME] = \\?\DISPLAY#DELA0A3#4&27e3cd2c&0&UID53349120#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DeviceInformation::FindAllAsync() finds two monitors:
DisplayMonitor::FromIdAsync() always throws exception ERROR_FILE_NOT_FOUND.