Solved

Monitor information using EnumDisplayDevices

Posted on 2004-04-16
5
2,615 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
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 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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now