locked
VBScript to read resolution of primary monitor not always working RRS feed

  • Question

  • I'm trying to write a VBScript that can determine if the screen resolution is either 4:3 or widescreen, then copy and set background image appropriately. The script works fine in my VM's (XP & Win7 32/64) but not on my host system (Dell Latitude 6420 /w Win7 64 /w a secondary monitor) . So not sure if this is a video driver issue (as stated here) or if there's another method. The following code seems to work fine, but doesn't return resolution on my machine (just the asterick)...UNLESS I change the end of line 2 from DesktopMonitor1 to DesktopMonitor2 (which stops it working in the VM's). Only other thing I thought of was that I have multiple monitors, but regardless of what number I change this to, I don't get the resolution of my secondary monitor (different resolution). Not that that would matter, as I'm only looking to get the primary monitor's resolution.

        Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2") 
        Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor where DeviceID = 'DesktopMonitor1'",,0) 
        For Each objItem in colItems 
            intHorizontal = objItem.ScreenWidth 
            intVertical = objItem.ScreenHeight 
        Next 
    
    msgbox intHorizontal & " * " & intVertical 
    
    If intHorizontal/IntVertical <= 1.334 then
    	WScript.Echo "4:3"
    	Else
    	WScript.Echo "Widescreen"
    End if

    Is this just a possible issue of video driver support (although I'm almost current, I will try updating...although if that's the issue, that's bound to happen elsewhere), or is there some other WMI object I should be using?

    Thanks in advance,
    Brian

    • Edited by Brian Grigg Saturday, April 6, 2013 11:58 PM
    Saturday, April 6, 2013 11:44 PM

Answers

  • Nevermind I guess (unless someone sees a potential hole in this version). I found the correct WMI that seems to work fine:

    Set colItems = objWMIService.ExecQuery("Select * from Win32_VideoController",,48)
        For Each objItem in colItems 
            intHorizontal = objItem.CurrentHorizontalResolution 
            intVertical = objItem.CurrentVerticalResolution 
        Next
    Thanks!
    • Edited by Brian Grigg Sunday, April 7, 2013 12:59 AM
    • Proposed as answer by Alex K. Angelopoulos Sunday, April 7, 2013 11:26 PM
    • Marked as answer by IamMred Wednesday, April 17, 2013 6:26 PM
    Sunday, April 7, 2013 12:31 AM

All replies

  • Nevermind I guess (unless someone sees a potential hole in this version). I found the correct WMI that seems to work fine:

    Set colItems = objWMIService.ExecQuery("Select * from Win32_VideoController",,48)
        For Each objItem in colItems 
            intHorizontal = objItem.CurrentHorizontalResolution 
            intVertical = objItem.CurrentVerticalResolution 
        Next
    Thanks!
    • Edited by Brian Grigg Sunday, April 7, 2013 12:59 AM
    • Proposed as answer by Alex K. Angelopoulos Sunday, April 7, 2013 11:26 PM
    • Marked as answer by IamMred Wednesday, April 17, 2013 6:26 PM
    Sunday, April 7, 2013 12:31 AM
  • That's the best WMI-based method, I believe. You can also cut to the chase with Windows Forms usage, like this:

    http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/7f707a7c-4c32-42ac-ae67-5c01c0c986c2

    Sunday, April 7, 2013 11:32 PM
  • Thanks, but that wouldn't work for XP as it requires PS, correct?
    Monday, April 8, 2013 5:42 PM
  • Thanks, but that wouldn't work for XP as it requires PS, correct?

    Oops. Yes; it would require installing PS on XP; I blanked out on the XP VM requirement. 

    The alternative generic technique used in the XP era from VBScript was to use Internet Explorer to determine the screen resolution - and note that this technique will only work for the primary display. Below is a code example; in my opinion, your use of Win32_VideoController is still a *much* better choice.

    Set ie = CreateObject("internetexplorer.application")
    ie.fullscreen = True
    width = ie.width
    height = ie.height
    $ie.Quit
    WScript.Echo "height:", height
    WScript.Echo "width:", width
    WScript.Echo "aspect ratio:", width/height
    

    Tuesday, April 9, 2013 6:31 AM