1

I have two monitor that are connected to the computer. By using winapi EnumDisplaySettings(), i got the names and ID of the each monitor. The problem is that I can't see who is the primary monitor and who is the secondary monitor. I have tried to use GetMonitorInfo() and some more API functions, but they didn't gave me what I am looking for.

How can i get the name of the primary monitor?

1 Answers1

2

MONITORINFOEX structure returned by GetMonitorInfo provides you with dwFlags member, MONITORINFOF_PRIMARY flag of which indicates the primary monitor.

The name of the monitor is available through szDevice member. You can quickly check monitor information using MonitorInformation.exe app from this blog post (source code):

Monitor 0 at (0, 0) - (1680, 1050):
  Coordinates (rcMonitor): (0, 0) - (1680, 1050)
  Work Area (rcWork): (0, 0) - (1680, 1020)
  Flags (dwFlags): 0x1 <<-------- MONITORINFOF_PRIMARY
  Device Name (szDevice): \\.\DISPLAY1 <<-------- Name

Monitor 1 at (1680, 0) - (3360, 1050):
  Coordinates (rcMonitor): (1680, 0) - (3360, 1050)
  Work Area (rcWork): (1680, 0) - (3360, 1050)
  Flags (dwFlags): 0x0
  Device Name (szDevice): \\.\DISPLAY2

See also:

mirh
  • 514
  • 8
  • 14
Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • But the MONITORINFOF_PRIMARY flag will be set for whatever monitor is configured as primary, and that can be any external monitor. I need the name of the specific monitor that is now is the primary. How can I do it? – user3903403 Aug 13 '14 at 07:45
  • You enumerate monitors, then you find the primary one, then `szDevice` from this structure gives you the name. – Roman R. Aug 13 '14 at 07:48
  • target.rcMonitor{top=0 bottom=1080 left=0 right=1920} target.szDevice 0x007ef5b4 L"쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌... The rcMonitor he is returning is correct but as you can see the szDevice not so much! why? and how can i fix it? – user3903403 Aug 13 '14 at 08:28
  • 1
    You are doing something wrong without even showing your code. – Roman R. Aug 13 '14 at 08:34
  • "쳌" is 0xCCCC and that is mostly because you haven't initialized the variables. It only appears in [MSVC debug mode](http://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new) and is extremely useful to determine the bug when you see some "strange" values such as the string above, int3 (software interrupt), invalid access to address 0xCCCCCCCC, or 204, -858993460, etc... – phuclv Aug 13 '14 at 10:24
  • @LưuVĩnhPhúc: My guess would be the asker confused `MONITORINFOEX` with `MONITORINFO` at some point, and then eventually figured it out. – Roman R. Aug 13 '14 at 10:25
  • @RomanR. I'm explaining why he saw that. Maybe the input to the function is wrong so it didn't write anything to the output struct, leaving the output uninitialized – phuclv Aug 13 '14 at 10:30