Checking EnumDisplayDevices dwFlags

I currently have a function, and I want it to return the number of active monitors (using EnumDisplayDevices).

... DISPLAY_DEVICE dd; dd.cb = sizeof(DISPLAY_DEVICE); while (EnumDisplayDevices(NULL, numberofDeviceAdapters, &dd, EDD_GET_DEVICE_INTERFACE_NAME)) { if (dd.StateFlags == DISPLAY_DEVICE_ACTIVE) { numberOfActiveMonitors++; } numberofDeviceAdapters++; } return numberOfActiveMonitors; 

numberOfActiveMonitors never increases, how can I properly check StateFlags DISPLAY_DEVICE?

+5
source share
1 answer

If you read the MSDN documentation of the DISPLAY_DEVICE structure, you will notice that:

Stateflags
Device status flags. This can be any reasonable combination of the following. [...]

So, you should use the binary operator & (bitwise AND) to check if the desidered flag (in your case DISPLAY_DEVICE_ACTIVE ) is set in the StateFlags data StateFlags above data structure.

eg:.

 // Your code: // if (dd.StateFlags == DISPLAY_DEVICE_ACTIVE) // // Change to (use binary AND & operator): if (dd.StateFlags & DISPLAY_DEVICE_ACTIVE) { ... } 

This is a very common pattern in Win32 / C ++ programming to check if a given binary flag is set in a DWORD containing several flags.

+2
source

Source: https://habr.com/ru/post/1206976/


All Articles