# EnumDisplayDevices function #
code: https://github.com/yubaochina/programming_API_Guide/
Syntax
```C++
BOOL EnumDisplayDevices(
_In_ LPCTSTR lpDevice,
_In_ DWORD iDevNum,
_Out_ PDISPLAY_DEVICE lpDisplayDevice,
_In_ DWORD dwFlags
);
```
Parameters
- lpDevice [in]
A pointer to the device name. If NULL, function returns information for the display adapter(s) on the machine, based on iDevNum.
For more information, see Remarks.
- iDevNum [in]
An index value that specifies the display device of interest.
The operating system identifies each display device in the current session with an index value. The index values are consecutive integers, starting at 0. If the current session has three display devices, for example, they are specified by the index values 0, 1, and 2.
- lpDisplayDevice [out]
A pointer to a DISPLAY_DEVICE structure that receives information about the display device specified by iDevNum.
Before calling EnumDisplayDevices, you must initialize the cb member of DISPLAY_DEVICE to the size, in bytes, of DISPLAY_DEVICE.
- dwFlags [in]
Set this flag to EDD_GET_DEVICE_INTERFACE_NAME (0x00000001) to retrieve the device interface name for GUID_DEVINTERFACE_MONITOR, which is registered by the operating system on a per monitor basis. The value is placed in the DeviceID member of the DISPLAY_DEVICE structure returned in lpDisplayDevice. The resulting device interface name can be used with SetupAPI functions and serves as a link between GDI monitor devices and SetupAPI monitor devices.
- Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. The function fails if iDevNum is greater than the largest device index.
- Remarks
To query all display devices in the current session, call this function in a loop, starting with iDevNum set to 0, and incrementing iDevNum until the function fails. To select all display devices in the desktop, use only the display devices that have the DISPLAY_DEVICE_ATTACHED_TO_DESKTOP flag in the DISPLAY_DEVICE structure.
To get information on the display adapter, call EnumDisplayDevices with lpDevice set to NULL. For example, DISPLAY_DEVICE.DeviceString contains the adapter name.
To obtain information on a display monitor, first call EnumDisplayDevices with lpDevice set to NULL. Then call EnumDisplayDevices with lpDevice set to DISPLAY_DEVICE.DeviceName from the first call to EnumDisplayDevices and with iDevNum set to zero. Then DISPLAY_DEVICE.DeviceString is the monitor name.
To query all monitor devices associated with an adapter, call EnumDisplayDevices in a loop with lpDevice set to the adapter name, iDevNum set to start at 0, and iDevNum set to increment until the function fails. Note that DISPLAY_DEVICE.DeviceName changes with each call for monitor information, so you must save the adapter name. The function fails when there are no more monitors for the adapter.
```cpp
bool CVirtualDisplayDlg::EnumurateMonitor()
{
ofstream mylog;
mylog.open("./mylog.txt");
DISPLAY_DEVICE lpDisplayDevice;
ZeroMemory(&lpDisplayDevice, sizeof(DISPLAY_DEVICE));
lpDisplayDevice.cb = sizeof(DISPLAY_DEVICE);
//lpDisplayDevice.StateFlags = DISPLAY_DEVICE_ATTACHED_TO_DESKTOP;
DWORD iDevNum = 0;
if (mylog.is_open())
{
mylog << "To query all display devices in the current session" << endl;
}
else { return false; }
TCHAR adapterDevice[32];
for (iDevNum = 0; EnumDisplayDevices(NULL, iDevNum, &lpDisplayDevice, 0); iDevNum++)
{
mylog << "--------iDevNum-------" << iDevNum << endl;
//mylog << "cb\t:" << lpDisplayDevice.cb << endl;
//mylog << "DeviceID\t:" << lpDisplayDevice.DeviceID << endl; //not used
//mylog << "DeviceKey\t:" << lpDisplayDevice.DeviceKey << endl; //reserved
mylog << "DeviceName\t:" << CT2A(lpDisplayDevice.DeviceName) << endl;
mylog << "DeviceString\t:" << CT2A(lpDisplayDevice.DeviceString) << endl;
mylog << "StateFlags\t:" << int(lpDisplayDevice.StateFlags) << endl;
if ((DISPLAY_DEVICE_ATTACHED_TO_DESKTOP | DISPLAY_DEVICE_PRIMARY_DEVICE) == lpDisplayDevice.StateFlags)
{
mylog << "DISPLAY_DEVICE_ATTACHED_TO_DESKTOP & DISPLAY_DEVICE_PRIMARY_DEVICE" << endl;
}
if (DISPLAY_DEVICE_PRIMARY_DEVICE == lpDisplayDevice.StateFlags)
{
mylog << "DISPLAY_DEVICE_PRIMARY_DEVICE" << endl;
}
if (DISPLAY_DEVICE_ATTACHED_TO_DESKTOP == lpDisplayDevice.StateFlags)
{
mylog << "DISPLAY_DEVICE_ATTACHED_TO_DESKTOP" << endl;
}
/* Get monitor name via adaptername; please refer to: https://msdn.microsoft.com/en-us/library/dd162609(v=vs.85).aspx */
memcpy(adapterDevice, lpDisplayDevice.DeviceName, sizeof(lpDisplayDevice.DeviceName));
ZeroMemory(&lpDisplayDevice, sizeof(DISPLAY_DEVICE));
lpDisplayDevice.cb = sizeof(DISPLAY_DEVICE);
EnumDisplayDevices(adapterDevice, 0, &lpDisplayDevice, 0);
mylog << "monitor name \t:" << CT2A(lpDisplayDevice.DeviceName) << endl;
mylog << "monitor type\t:" << CT2A(lpDisplayDevice.DeviceString) << endl;
//clear DISPLAY_DEVICE and goto the next loop
ZeroMemory(&lpDisplayDevice, sizeof(DISPLAY_DEVICE));
lpDisplayDevice.cb = sizeof(DISPLAY_DEVICE);
}
mylog.close();
return true;
}
```
bool CVirtualDisplayDlg::EnumurateMonitor()
{
ofstream mylog;
mylog.open("./mylog.txt");
DISPLAY_DEVICE lpDisplayDevice;
ZeroMemory(&lpDisplayDevice, sizeof(DISPLAY_DEVICE));
lpDisplayDevice.cb = sizeof(DISPLAY_DEVICE);
//lpDisplayDevice.StateFlags = DISPLAY_DEVICE_ATTACHED_TO_DESKTOP;
DWORD iDevNum = 0;
if (mylog.is_open())
{
mylog << "To query all display devices in the current session" << endl;
}
else { return false; }
TCHAR adapterDevice[32];
for (iDevNum = 0; EnumDisplayDevices(NULL, iDevNum, &lpDisplayDevice, 0); iDevNum++)
{
mylog << "--------iDevNum-------" << iDevNum << endl;
//mylog << "cb\t:" << lpDisplayDevice.cb << endl;
//mylog << "DeviceID\t:" << lpDisplayDevice.DeviceID << endl; //not used
//mylog << "DeviceKey\t:" << lpDisplayDevice.DeviceKey << endl; //reserved
mylog << "DeviceName\t:" << CT2A(lpDisplayDevice.DeviceName) << endl;
mylog << "DeviceString\t:" << CT2A(lpDisplayDevice.DeviceString) << endl;
mylog << "StateFlags\t:" << int(lpDisplayDevice.StateFlags) << endl;
if ((DISPLAY_DEVICE_ATTACHED_TO_DESKTOP | DISPLAY_DEVICE_PRIMARY_DEVICE) == lpDisplayDevice.StateFlags)
{
mylog << "DISPLAY_DEVICE_ATTACHED_TO_DESKTOP & DISPLAY_DEVICE_PRIMARY_DEVICE" << endl;
}
if (DISPLAY_DEVICE_PRIMARY_DEVICE == lpDisplayDevice.StateFlags)
{
mylog << "DISPLAY_DEVICE_PRIMARY_DEVICE" << endl;
}
if (DISPLAY_DEVICE_ATTACHED_TO_DESKTOP == lpDisplayDevice.StateFlags)
{
mylog << "DISPLAY_DEVICE_ATTACHED_TO_DESKTOP" << endl;
}
/* Get monitor name via adaptername; please refer to: https://msdn.microsoft.com/en-us/library/dd162609(v=vs.85).aspx */
memcpy(adapterDevice, lpDisplayDevice.DeviceName, sizeof(lpDisplayDevice.DeviceName));
ZeroMemory(&lpDisplayDevice, sizeof(DISPLAY_DEVICE));
lpDisplayDevice.cb = sizeof(DISPLAY_DEVICE);
EnumDisplayDevices(adapterDevice, 0, &lpDisplayDevice, 0);
mylog << "monitor name \t:" << CT2A(lpDisplayDevice.DeviceName) << endl;
mylog << "monitor type\t:" << CT2A(lpDisplayDevice.DeviceString) << endl;
//clear DISPLAY_DEVICE and goto the next loop
ZeroMemory(&lpDisplayDevice, sizeof(DISPLAY_DEVICE));
lpDisplayDevice.cb = sizeof(DISPLAY_DEVICE);
}
mylog.close();
return true;
}
# Reference #
- EnumDisplayDevices function in MSDN: [https://msdn.microsoft.com/zh-cn/library/windows/desktop/dd162609(v=vs.85).aspx](https://msdn.microsoft.com/zh-cn/library/windows/desktop/dd162609(v=vs.85).aspx "msdn")
-