7

I'm having a bit of trouble retrieving the name of a monitor with winapi. According to other entries on stackoverflow, the correct way to get the name of a monitor is this:

EnumDisplayDevices(nullptr, 0, &oDisplayDevice, 0);

char lpszDeviceName[32];
memcpy(lpszDeviceName, oDisplayDevice.DeviceName, 32);

EnumDisplayDevices(lpszDeviceName, 0, &oDisplayDevice, 0);

char lpszMonitorName[128];
memcpy(lpszMonitorName, oDisplayDevice.DeviceString, 128);

However, EnumDisplayDevices returns FALSE the second time around. The first time around, DeviceName is \\DISPLAY1 and DeviceString is the GPU vendor. Using the MONITORINFOEX struct gives me the same value as DeviceName.

To be clear I'm looking for something like "Samsung blah blah," or what appears in the control panel on the screen resolution page.

NmdMystery
  • 2,708
  • 2
  • 28
  • 57

1 Answers1

7

This seems to return the proper data for me:

#include <Windows.h>
#include <iostream>
#include <string>

int main()
{
    DISPLAY_DEVICE dd;
    dd.cb = sizeof(dd);
    int deviceIndex = 0;
    while(EnumDisplayDevices(0, deviceIndex, &dd, 0))
    {
        std::string deviceName = dd.DeviceName;
        int monitorIndex = 0;
        while(EnumDisplayDevices(deviceName.c_str(), monitorIndex, &dd, 0))
        {
            std::cout << dd.DeviceName << ", " << dd.DeviceString << "\n";
            ++monitorIndex;
        }
        ++deviceIndex;
    }
    return 0;
}

If you're compiling for UNICODE then use this instead:

#include <Windows.h>
#include <iostream>
#include <string>

int main()
{
    DISPLAY_DEVICE dd;
    dd.cb = sizeof(dd);
    int deviceIndex = 0;
    while(EnumDisplayDevices(0, deviceIndex, &dd, 0))
    {
        std::wstring deviceName = dd.DeviceName;
        int monitorIndex = 0;
        while(EnumDisplayDevices(deviceName.c_str(), monitorIndex, &dd, 0))
        {
            std::wcout << dd.DeviceName << L", " << dd.DeviceString << L"\n";
            ++monitorIndex;
        }
        ++deviceIndex;
    }
    return 0;
}

Here's an example of the output:

\.\DISPLAY1\Monitor0, Dell U2410(DP)
\.\DISPLAY2\Monitor0, Dell 2407WFP-HC (Digital)

Retired Ninja
  • 4,620
  • 3
  • 23
  • 35
  • I still get nothing. I'm using LPWSTR (or std::wstring) and the W version of the function instead of a plain LPSTR, could that be why? – NmdMystery Nov 19 '13 at 00:53
  • Hmm... I tried switching to non-wide strings and I got Generic PnP monitor, is that just because there's no vendor string? – NmdMystery Nov 19 '13 at 00:58
  • I added a Unicode example. For me, this returns the same strings I see in the display control panel and device manager. Presumably if you're using the generic driver for the monitor then it would show the generic string. – Retired Ninja Nov 19 '13 at 01:00
  • Would pretty much every external monitor be something besides Generic PnP? I'm trying to use this as a way to distinguish a monitor preference during the startup of my program. – NmdMystery Nov 19 '13 at 01:03
  • 2
    It's hard to say. The 3 monitors I currently have available all have specific drivers for them, but I have seen the generic driver in use before. I would assume that unless they start switching cables around the combination of the device name and string would be a unique identifier. – Retired Ninja Nov 19 '13 at 01:06