1

I use WMDisplayChange to get notified when a monitor gets connected/disconnected because I need to load from a INI file information specific to that monitor.

What I don't know is how to know that an "known" monitor was reconnected to the PC. How to get monitor's unique ID? Windows obviously does it because it remembers the last resolution used on that monitor.

I tried this library but the ID returned looks like this (the number in brackets seems not to be unique).

enter image description here (one of the monitors is Dell, the others are Asus )

Update:
The number at the end of the ID (ex: 0009) seems to be some kind of unique ID. If I disconnect and reconnect a monitor, that number is remembered for that reconnected monitor. I even swapped the connector to a new port. The 0009 changed to 0008 but then when I put it back to the original HDMI port, it turned back to 0009.

This number seems to be equal to the chronological order in which the monitors were added to the system. Of course, connecting the same monitor to different port, makes the monitor to appear as "new monitor".

Kromster
  • 7,181
  • 7
  • 63
  • 111
Gabriel
  • 20,797
  • 27
  • 159
  • 293
  • related : [Detect external display being connected or removed under Windows 7](https://stackoverflow.com/q/5981520/327083) Is the problem knowing which WinAPI call to make, or do you also need help writing the import in Delphi? – J... Jul 15 '20 at 15:21
  • maybe also helpful : [How to catch WM_DEVICECHANGE in a control other than TForm?](https://stackoverflow.com/q/30711942/327083) – J... Jul 15 '20 at 15:23
  • also : [What is the notification when the number of monitors changes?](https://stackoverflow.com/q/33762140/327083) – J... Jul 15 '20 at 15:24
  • @J... - My problem is that I don't know which of those strings is a unique ID. I will look right now at your links. – Gabriel Jul 15 '20 at 15:25
  • 1
    Also keep in mind that the same display plugged into a different port is seen by Windows as a "different" monitor. (Same with USB devices, etc). If you have two identical monitors and switch their port, Windows will not generally distinguish them. Also, if you unplug a monitor and then reconnect it to a different port it will be managed by Windows as a new, different monitor (the "old" one being 'gone' now). You can get the serial numbers from WMI if you care about tracking a specific physical monitor, but I think this gets messy, and doesn't play well with RDP, etc. – J... Jul 15 '20 at 15:29
  • "if you have two identical monitors and switch their port" - Well I can accept that. I expect that (in most cases) the user will reconnect the monitor to the same port like last time. – Gabriel Jul 15 '20 at 15:31
  • 1
    The GUID in the screenshot above is the class ID for the display - all displays of the same type will share the same class id. There will also be a [device instance ID](https://learn.microsoft.com/en-us/windows-hardware/drivers/install/device-instance-ids). This is the one that identifies a specific monitor of the class connected to the system. – J... Jul 15 '20 at 15:35
  • @J... - Man... Thanks! That is what I needed! I will see how it can be obtained under Delphi. If you post this as an answer I will accept it. – Gabriel Jul 15 '20 at 15:37
  • I have found this as a possible was to extract the monitor ID. I will look into it. It is quite dense. https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-querydisplayconfig – Gabriel Jul 15 '20 at 16:00
  • 1
    That could work, but it's a query, which isn't ideal. You don't want to always be checking - with RegisterDeviceNotification you can be notified when the hardware changes. Maybe if that doesn't give you enough information you can use QueryDisplayConfig in response to a device change notification, though. – J... Jul 15 '20 at 16:05

0 Answers0