12

I have two monitors connected to my Windows PC -- one is a normal monitor and the other is a projector. Because Windows doesn't consistently assign one or the other as the primary monitor (in part because they aren't always both on when Windows boots), I need to programmatically detect which monitor is which.

The Control Panel shows the names of the monitors as "HP 2159" (the normal monitor) and "PROJECTOR" in the screen where you choose which is the primary monitor. That's the information I want to get in my program.

I can't find the right Win32 API function for this info. I've tried both EnumDisplayDevices and EnumDisplayMontiors. Both just give "DISPLAY1" and "DISPLAY2" as devices names. What should I be using to get the "HP 2159" and "PROJECTOR" info or something analogous?

UPDATE: Here's the Python code I'm using:

>>> import win32api
>>> monitors = win32api.EnumDisplayMonitors()
>>> win32api.GetMonitorInfo(monitors[0][0])
{'Device': '\\\\.\\DISPLAY1', 'Work': (0, 0, 1920, 1080), 'Flags': 1, 'Monitor': (0, 0, 1920, 1080)}
>>> win32api.GetMonitorInfo(monitors[1][0])
{'Device': '\\\\.\\DISPLAY2', 'Work': (1920, 0, 3360, 1080), 'Flags': 0, 'Monitor': (1920, 0, 3360, 1080)}
Ghopper21
  • 10,287
  • 10
  • 63
  • 92
  • Post your code. The method I outline in my answer has always worked for me. – Jim Mischel Mar 29 '14 at 02:43
  • @Jim Mischel - see the Python code (accessing the Win32 API via the `win32api` PyWin library). Maybe it's something wrong with the PyWin implementation of the API? – Ghopper21 Apr 01 '14 at 02:09
  • Perhaps somebody else can help you with it. I'm not a Python programmer, and I know nothing about PyWin. – Jim Mischel Apr 01 '14 at 02:35
  • 1
    It occurs to me that the problem could be that when PyWin calls `GetMonitorInfo`, it's passing a `MonitorInfo` structure rather than a `MonitorInfoEx`. I don't know if that's the *actual* behavior, but that behavior would explain the results you're seeing. – Jim Mischel Apr 01 '14 at 13:38
  • 1
    Although a quick look at the source, https://bitbucket.org/amauryfa/pywin32-pypy/src/0d23a353509b/win32/src/win32api_display.cpp, leads me to believe that PyWin is doing the right thing. – Jim Mischel Apr 01 '14 at 13:46
  • @JimMischel - thanks for checking. Given it looks like PyWin is doing the right thing, any other possible reasons for this odd behavior? – Ghopper21 Apr 01 '14 at 13:46
  • Maybe https://stackoverflow.com/a/65013500/32453 windows 7+ – rogerdpack Dec 20 '22 at 05:23
  • I posted a solution to your specific problem [here](https://stackoverflow.com/a/76934256/22413869). – Stuntman11 Aug 19 '23 at 20:10

1 Answers1

6

The EnumDisplayMonitors passes a monitor handle to the MonitorEnumProc callback function. You can pass that handle to GetMonitorInfo, being sure to pass a pointer to a MonitorInfoEx structure and setting the cbSize member accordingly.

Upon return, the szDevice field in the MonitorInfoEx structure will contain the name of the monitor.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • 6
    Afraid this isn't working for me -- that's what I've already tried, and I get the same "DISPLAY1" and "DISPLAY2" as names. – Ghopper21 Mar 29 '14 at 00:02
  • 1
    This is not a PyWin issue, as you get the same behavior directly from the C API. You can use EnumDisplayDevices to get the monitor names, but the projector for me is just named "Generic PnP Monitor" and yet the control panel applet shows it as PROJECTOR as the OP explains. – Adrian McCarthy Apr 01 '18 at 21:44