none
How to use "EnumDisplayDevices" ? RRS feed

  • 问题

  • When I want to enumerate each monitor of the display adapter, I choose to use EnumDisplayDevices and EnumDisplaySettings. However, I got return 0 from EnumDisplaySettings, here is my code:

    	BOOL CMultiScreen::UpdateAllPhysicalScreens()
    	{
    		DEVMODE dev_mode;
    		DISPLAY_DEVICE dd;
    		int iDevNum = 0;
    		BOOL b_EnumDisplayDevicesflag = TRUE;
    		BOOL b_EnumDisplaySettingsflag = TRUE;
    
    		ZeroMemory(&dd, sizeof(dd));
    		dd.cb = sizeof(DISPLAY_DEVICE);
    		ZeroMemory(&dev_mode, sizeof(dev_mode));
    		dev_mode.dmSize = sizeof(DEVMODE);
    
    		// Get the display adapter name
    		b_EnumDisplayDevicesflag = EnumDisplayDevices(NULL, iDevNum, &dd, NULL);
    		NCString strAdapterName = _T("");
    		strAdapterName = b_EnumDisplayDevicesflag ? dd.DeviceName : strAdapterName;
    
    		while (b_EnumDisplayDevicesflag)
    		{
    			b_EnumDisplayDevicesflag = EnumDisplayDevices(strAdapterName.ToString(), iDevNum, &dd, NULL);
    			b_EnumDisplaySettingsflag = EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dev_mode);
    
    			if (!(b_EnumDisplayDevicesflag && b_EnumDisplaySettingsflag))
    			{
    				break;
    			}
    
    			ScreenInfo sc_info;
    			sc_info.nPhysicalPosX = dev_mode.dmPosition.x;
    			sc_info.nPhysicalPosY = dev_mode.dmPosition.y;
    			sc_info.uPhysicalWidth = dev_mode.dmPelsWidth;
    			sc_info.uPhysicalHeight = dev_mode.dmPelsHeight;
    			m_vAllScreensInfo.push_back(sc_info);
    
    			++iDevNum;
    		}
    		return TRUE;
    	}

    As the code show, when I first call EnumDisplayDevices, I can get the adapter name "Intel Graphics 400", and in the while loop, I can get the device name ".\\Display1\\Monitor0" when iDevNum equal to 0, then I got 0 from EnumDisplaySettings, I have read the MSDN many times and still cannot figure it out.

    The referred document: https://msdn.microsoft.com/en-us/library/dd162609

    Does anyone know how to get correct display settings from the SPECIFIC display adapter?

    Thank you guys :-)



    • 已编辑 JinQin 2018年1月29日 10:15
    2018年1月29日 10:07

全部回复

  • Hi JinQin,

    感谢在MSDN论坛发帖。

    >>As the code show, when I first call EnumDisplayDevices, I can get the adapter name "Intel Graphics 400", and in the while loop, I can get the device name ".\\Display1\\Monitor0" when iDevNum equal to 0, then I got 0 from EnumDisplaySettings, I have read the MSDN many times and still cannot figure it out.

    你可以尝试调用GetLastError函数来获取准确的错误信息。方便查找错误原因。

    另外,在调用EnumDisplaySettings之前,你需要设置dmDriverExtra成员来指示可用于接收私有驱动程序数据的额外空间的大小(以字节为单位)。如果iModeNum大于显示设备的最后一个图形模式的索引,也将导致函数失败。

    更多关于EnumDisplaySettings的使用方法,你可以参考下面的文档。

    https://msdn.microsoft.com/en-us/library/dd162611(v=vs.85).aspx

    希望对你有所帮助。

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月30日 1:35
  • "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."

    上面的语句是摘自:https://msdn.microsoft.com/en-us/library/dd162609

    我按照这个步骤来调用EnumDisplayDeivces,但将其中的DeviceName传到DnumDisplaySettings的时候,就return 0了

    2018年1月30日 4:10
  • >>我按照这个步骤来调用EnumDisplayDeivces,但将其中的DeviceName传到DnumDisplaySettings的时候,就return 0了

    错误不在你如何调用EnumDisplayDeivces,而是调用DnumDisplaySettings错误了。DnumDisplaySettings并不只有DeviceName一个参数,你不仅需要按照EnumDisplayDeivces的文档,同时也需要参照DnumDisplaySettings的调用规范。

    请参考我给你的步骤,调用GetLastError函数找出具体原因并查看DnumDisplaySettings参数的正确性。

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月30日 6:11