WMI Query/Join Logic

PTNL

Supreme [H]ardness
Joined
Jan 2, 2005
Messages
4,199
I am enumerating the devices returned from this WMI query:
Code:
SELECT * FROM Win32_DisplayConfiguration

What I want to do is find out what desktop adapter or video card is driving each desktop monitor returned in the above query. I can query the Win32_DesktopMonitor and get a list of desktop adapters, but I don't see a property whose value should be used (either programmatically or within a loop) to join the separate resultsets. Anyone know how to make that connection through WMI querying and/or evaluations?

(Edit: I'm using C#, v4.0)
 
Last edited:
I solved it.

Short answer is that I needed to blend data returned from a P/Invoke call to EnumDisplayDevices and mesh against the "DeviceName" property of the .NET "Screen" object.

Output looks like this:
Code:
Device: \\.\DISPLAY1, NVIDIA Quadro FX 1800M, 1280x720
Device: \\.\DISPLAY2, NVIDIA Quadro FX 1800M, Primary - 1920x1080

Code Snippet:
Code:
static void DoWork()
{
    DISPLAY_DEVICE d = new DISPLAY_DEVICE();
    d.cb = Marshal.SizeOf(d);
    try
    {
        for (uint id = 0; EnumDisplayDevices(null, id, ref d, 0); id++)
        {
            if (d.DeviceName.ToUpper().Contains("DISPLAYV"))    // skip virtual displays
                continue;

            var deviceName = d.DeviceName;
            var vendor = d.DeviceString;
            var resolution = GetDisplayDetails(deviceName);
            Console.WriteLine("Device: {0}, {1}, {2}", deviceName, vendor, resolution);
            d.cb = Marshal.SizeOf(d);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

static string GetDisplayDetails(string deviceName)
{
    var targetScreen = (from x in Screen.AllScreens where x.DeviceName == deviceName select x).FirstOrDefault();

    if (targetScreen == null)
        return "!!!";

    return (targetScreen.Primary)
            ? string.Format("Primary - {0}x{1}", targetScreen.Bounds.Width, targetScreen.Bounds.Height)
            : string.Format("{0}x{1}", targetScreen.Bounds.Width, targetScreen.Bounds.Height);
}


Edit: Note that the numbering after "\\DISPLAY" does not guarantee to match against the large number you would see from the Windows screen resolution management window -- but that wasn't a necessity for me. This blog post helped push me in the right direction.
 
Last edited:
Back
Top