Bill Walton
Bill Walton

Reputation: 823

Getting the monitor's name with EnumDisplayDevices

I came across this post in which someone wants to find out the name of their monitor using EnumDisplayDevices.

This is exactly what I want, and I tried to do something similar in C++ but the second call to EnumDisplayDevices never seems to return anything, I only get information about the graphics card.

DISPLAY_DEVICE dd;
memset(&dd, 0, sizeof(DISPLAY_DEVICE));
dd.cb = sizeof(dd);
int i = 0;
while(EnumDisplayDevices(NULL, i, &dd, 0))
{
    Log(_T("Device Name: %s Device String: %s"), dd.DeviceName, dd.DeviceString);

    if(EnumDisplayDevices(dd.DeviceName, 0, &dd, 0))
    {
        Log(_T("Monitor Name: %s Monitor String: %s"), dd.DeviceName, dd.DeviceString);
    }

    i++;
} 

The output I get is

Device Name: \\.\DISPLAY1 Device String: NVIDIA GeForce 9300 GE
Device Name: \\.\DISPLAYV1 Device String: NetMeeting driver
Device Name: \\.\DISPLAYV2 Device String: RDPDD Chained DD

The target platform is XP, and I can't any standard way of finding out the monitor name. Any ideas?

Thanks.

Upvotes: 4

Views: 13367

Answers (2)

Flot2011
Flot2011

Reputation: 4671

After the first call to EnumDisplayDevices DispDev.DeviceString contains graphic card's name. After the second call DispDev.DeviceString contains monitor's name.

Also see this link for other ways to get this info

BOOL GetMonitorInfo(int nDeviceIndex, LPSTR lpszMonitorInfo) {
    BOOL bResult = TRUE;
    FARPROC EnumDisplayDevices;
    HINSTANCE  hInstUserLib;
    DISPLAY_DEVICE DispDev;
    char szDeviceName[32];

    hInstUserLib = LoadLibrary("User32.DLL");

    EnumDisplayDevices = (FARPROC)GetProcAddress(hInstUserLib,
                                                 "EnumDisplayDevicesA");
    if(!EnumDisplayDevices) {
        FreeLibrary(hInstUserLib);
        return FALSE;
    }

    ZeroMemory(&DispDev, sizeof(DISPLAY_DEVICE));
    DispDev.cb = sizeof(DISPLAY_DEVICE);

    // After first call to EnumDisplayDevices DispDev.DeviceString 
    //contains graphic card name
    if(EnumDisplayDevices(NULL, nDeviceIndex, &DispDev, 0)) {
        lstrcpy(szDeviceName, DispDev.DeviceName);

        // after second call DispDev.DeviceString contains monitor's name 
        EnumDisplayDevices(szDeviceName, 0, &DispDev, 0);

        lstrcpy(lpszMonitorInfo, DispDev.DeviceString);
    }
    else {
        bResult = FALSE;
    }

    FreeLibrary(hInstUserLib);

    return bResult;
}

Upvotes: 5

Mike Kwan
Mike Kwan

Reputation: 24447

I think Win32_DesktopMonitor may be more suited to what you are trying to do.

Upvotes: 0

Related Questions