question

Yuganknarula-8688 avatar image
0 Votes"
Yuganknarula-8688 asked HuiLiu-MSFT edited

EnumDisplayMonitors not returning any monitor information.

I have one main monitor and 2 PDUs attached to it. I have a main wpf application and 2 windows. I need monitor information basically its bounds and name of all monitors as I have to display other 2 windows on PDUs.
These PDUs are USB connected. On connection I am using EnumDisplayMonitors to get monitor information. I have 50 systems and it is working fine on 48 and this function returns all connected monitors. I have added delay of few seconds after connection so that all monitors finfo is returned but only on 2 system EnumDisplayMonitors doesn't return any information after connection.
Earlier *I was using System.Windows.Forms.Screen.AllScreens, as its internally call the win apis to get information. It is itself a failure as its take more time to get all connected screen.
There is no hardware issue and also I checked on device change event I can get some info for connected monitor by its name.

How can I troubleshoot the problem. Below is the code I am using to get monitor information.

EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero,
delegate (IntPtr hMonitor, IntPtr hdcMonitor, ref RectStruct lprcMonitor, IntPtr dwData)
{
MonitorInfoEx mi = new MonitorInfoEx();
mi.Size = (int)Marshal.SizeOf(mi);
bool success = GetMonitorInfo(hMonitor, ref mi);
if (success)
{
MonitorDisplayInfo di = new MonitorDisplayInfo();
di.ScreenWidth = mi.Monitor.right - mi.Monitor.left;
di.ScreenHeight = mi.Monitor.bottom - mi.Monitor.top;
di.MonitorArea = mi.Monitor;
di.WorkArea = mi.WorkArea;
di.IsPrimary = (mi.Flags == MONITORINFOF_PRIMARY);
di.DeviceName = mi.DeviceName.Split('\\').LastOrDefault();
monitorDisplayInfos.Add(di);
}
return true;
}, IntPtr.Zero);


     [DllImport("user32.dll", CharSet = CharSet.Auto)]
     static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfoEx lpmi);

     [DllImport("user32.dll")]
     static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumDelegate lpfnEnum, IntPtr dwData);

     delegate bool MonitorEnumDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RectStruct lprcMonitor, IntPtr dwData);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct MonitorInfoEx
{
////// size of a device name string
private const int CCHDEVICENAME = 32;
public int Size;
public RectStruct Monitor;
public RectStruct WorkArea;
public uint Flags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
public string DeviceName;
public void Init()
{
this.Size = 72;
this.DeviceName = string.Empty;
}
}


dotnet-csharpwindows-apidotnet-visual-basic
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Do you mean that the delegate is not called at all?


1 Vote 1 ·

What's the EnumDisplayMonitors Return value and these 2 systems version? Do these 2 systems have some something different from others?

0 Votes 0 ·

Hello @Yuganknarula-8688, Any updates? You can also test calling the WinAPI from c++ instead of using pinvoke from managed code.

0 Votes 0 ·

0 Answers