Solved

Monitor information using EnumDisplayDevices

Posted on 2004-04-16
5
2,724 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…

740 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