Attaching a disabled display device using WinAPI

My problem is to turn off the monitor using ChangeDisplaySettingsEx . I think this is not rocket science, but after some digging it still seems impossible. I found a way to disable all secondary displays based on the sample Microsoft code found here . Although only basic configuration was required to work, re-installation never worked. I tried to do this:

1. Initialize DisplayDevice

 BOOL FoundSecondaryDisp = FALSE; DWORD DispNum = 0; DISPLAY_DEVICE DisplayDevice; LONG Result; TCHAR szTemp[200]; int i = 0; DEVMODE defaultMode; ZeroMemory(&DisplayDevice, sizeof(DisplayDevice)); DisplayDevice.cb = sizeof(DisplayDevice); 

2. Find all devices

 while (EnumDisplayDevices(NULL, DispNum, &DisplayDevice, 0)) { ZeroMemory(&defaultMode, sizeof(DEVMODE)); defaultMode.dmSize = sizeof(DEVMODE); //point 3 goes here } 

3. Detecting a disconnected device

 if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) 

4. Turn on the device

 DEVMODE DevMode; ZeroMemory(&DevMode, sizeof(DevMode)); DevMode.dmSize = sizeof(DevMode); DevMode.dmFields = DM_POSITION | DM_PELSWIDTH | DM_PELSHEIGHT; DevMode.dmPelsWidth = 1920; DevMode.dmPelsHeight = 1080; Result = ChangeDisplaySettingsEx(DisplayDevice.DeviceName, &DevMode, NULL, CDS_UPDATEREGISTRY, NULL); ChangeDisplaySettingsEx(NULL, NULL, NULL, NULL, NULL); 

The last point returns the code DISP_CHANGE_FAILED and does not turn on the display. Has anyone had a similar experience?

+5
source share
1 answer

Try adding CDS_NORESET to the first ChangeDisplaySettingsEx call.

It works:

 ChangeDisplaySettingsEx((LPCWSTR)DisplayDevice.DeviceName, &DevMode, NULL, CDS_UPDATEREGISTRY | CDS_NORESET, NULL); ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL); 

This does not work:

 ChangeDisplaySettingsEx((LPCWSTR)DisplayDevice.DeviceName, &DevMode, NULL, CDS_UPDATEREGISTRY | CDS_RESET, NULL); ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL); 

This one does NOT work either:

 ChangeDisplaySettingsEx((LPCWSTR)DisplayDevice.DeviceName, &DevMode, NULL, CDS_UPDATEREGISTRY, NULL); ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL); 
+1
source

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


All Articles