Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Monitor information using EnumDisplayDevices

Posted on 2004-04-16
5
Medium Priority
?
2,920 Views
Last Modified: 2013-12-03
Hi,

I'm trying to get the name of my monitor using the following code - it's giving me the name of the adapter instead:

bool MonitorInfo::Acquire()
{
    TCHAR msg[10000] = {0};
      DISPLAY_DEVICE dd;

      dd.cb = sizeof(dd);

      DWORD dev = 0; //device index

      int id = 1; //monitor number as used by Display Properties -> Settings

      while(EnumDisplayDevices(0, dev, &dd, 0))
      {
            if(!(dd.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
            {
                  
                  DISPLAY_DEVICE ddMon;
                  ZeroMemory(&ddMon, sizeof(ddMon));
                  ddMon.cb = sizeof(&ddMon);

                  DWORD devMon = 0;

                  
                  while(EnumDisplayDevices(dd.DeviceName, devMon, &ddMon, 0))
                  {
                        if(ddMon.StateFlags & DISPLAY_DEVICE_ACTIVE)

                              break;
                        devMon++;

                  }

                  if(!*ddMon.DeviceString)
                  {
                        EnumDisplayDevices(dd.DeviceName, 0, &ddMon, 0);
                        if(!*ddMon.DeviceString)
                        {
                              HRESULT hr = StringCchCopy(dd.DeviceString, sizeof(dd.DeviceString), TEXT("Default Monitor"));
                              if (FAILED(hr))
                              {
                                    return false;
                              }
                        }
                  }

                  //get information about the display position and the current display mode
                  DEVMODE dm;
                  ZeroMemory(&dm, sizeof(dm));
                  dm.dmSize = sizeof(dm);

                  if(EnumDisplaySettingsEx(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm, 0) == FALSE)
                  {
                        EnumDisplaySettingsEx(dd.DeviceName, ENUM_REGISTRY_SETTINGS, &dm, 0);
                  }

                  //format information about this monitor

                  MonitorInfoEx monInfoEx;

                  HRESULT hr = StringCchPrintf(monInfoEx.refreshRate, sizeof(monInfoEx.refreshRate), TEXT("%d"), dm.dmDisplayFrequency);
            if (FAILED(hr))
                  {
                        return false;
                  }
                  //device name
                  hr = StringCchCopy(monInfoEx.name, sizeof(monInfoEx.name), ddMon.DeviceString);
                  if (FAILED(hr))
                  {
                        return false;
                  }
                  id++;

                  monitors.push_back(monInfoEx);

            }
            dev++;
      }

      return true;
}

I've tried different combinations in the EnumDisplayDevices, but it either gives me the name of the video card or "Default Monitor". What I want is the name of the monitor as stated in the Device Manager.

Help, please?

TIA,
S/.
0
Comment
Question by:stitch2802
  • 3
5 Comments
 
LVL 6

Expert Comment

by:joghurt
ID: 10848923
EnumDisplayDevices should return the name of the display card (instead of the monitor). What about EnumDisplayMonitors and GetMonitorInfo function with MONITORINFOEX?
0
 

Author Comment

by:stitch2802
ID: 10851694
Could you give me an example, please?
0
 

Author Comment

by:stitch2802
ID: 10851960
Thanks, but that isn't what I'm looking for - the combination of EnumDisplayMonitors and GetMonitorInfo, along with MONITORINFOEX  gives me the following output: \\.\Display1

I'm trying to get the name of the monitor, for example: Samsung SyncMaster 753 DF.

TIA,
s/.
0
 
LVL 1

Accepted Solution

by:
rhanneken earned 2000 total points
ID: 10852176
stitch2802,

This console application shows you how to get the information you want.  You may want to use something other than DISPLAY_DEVICE_ATTACHED_TO_DESKTOP; look up DISPLAY_DEVICE in the Platform SDK help files for more information.

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <ostream>

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD iDevNum(0);
    DISPLAY_DEVICE dd;
    dd.cb = sizeof(dd);

    while ( EnumDisplayDevices(0, iDevNum, &dd, 0) )
    {
        if (dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
        {
            DISPLAY_DEVICE dd2;
            dd2.cb = sizeof(dd2);
            EnumDisplayDevices(dd.DeviceName, 0, &dd2, 0);
            std::cout << (iDevNum + 1) << ". " << dd2.DeviceString << std::endl;
        }
        ++iDevNum;
    }

    std::cin.get();

    return 0;
}

0
 

Author Comment

by:stitch2802
ID: 10852180
rhanneken,

You're a genius! =)

Exactly what I was looking for, thanks!
s/.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Loops Section Overview

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question