Avatar of stephenlecomptejr
stephenlecomptejr
Flag for United States of America asked on

Visual Basic .NET function that gives Screen Resolution throws an error.

Per the following code, I get an error on line:  colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DesktopMonitor", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

I get error warning:

User: [slecompte] - Date: [7/11/2012 3:41:25 PM] - Source: [frmMain] - Procedure: [Verify_Info_Exists] - Error Message: [System.Runtime.InteropServices.COMException (0x80020009): Unspecified error
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at System.Runtime.InteropServices.CustomMarshalers.EnumerableViewOfDispatch.GetEnumerator()
   at System.Collections.IEnumerable.GetEnumerator()
   at FKPTrack.frmMain.Screen_X_Resolution(String strComputer) in C:\Users\slecompte\Documents\Visual Studio 2010\Projects\FKPTracker\frmMain.vb:line 390
   at FKPTrack.frmMain.Verify_Info_Exists() in C:\Users\slecompte\Documents\Visual Studio 2010\Projects\FKPTracker\frmMain.vb:line 257 - Unspecified error ].

How may I rewrite it so the syntax works?

This was originally taken from a .vbs script that worked just fine.

    Public Function Screen_X_Resolution(ByVal strComputer As String) As Long


        Dim objWMIService As Object
        Dim colItems As Object
        Dim objItem As Object

        Dim x As Long

        x = 0
        Const wbemFlagReturnImmediately = &H10
        Const wbemFlagForwardOnly = &H20
        strComputer = strComputer
        objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
        colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DesktopMonitor", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
        For Each objItem In colItems
            If (objItem.ScreenWidth Is Nothing) Then  
            Else
                x = objItem.ScreenWidth
                'y = objItem.ScreenHeight
            End If
        Next
        objItem = Nothing
        colItems = Nothing
        objWMIService = Nothing


        Screen_X_Resolution = x

    End Function

    Public Function Screen_Y_Resolution(ByVal strComputer As String)

        Dim objWMIService As Object
        Dim colItems As Object
        Dim objItem As Object
        Dim Y As Long

        Y = 0
        Const wbemFlagReturnImmediately = &H10
        Const wbemFlagForwardOnly = &H20

        strComputer = strComputer

        objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
        colItems = objWMIService.ExecQuery("SELECT * FROM Win32_DesktopMonitor", "WQL", _
        wbemFlagReturnImmediately + wbemFlagForwardOnly)

        For Each objItem In colItems

            If (objItem.ScreenWidth Is Nothing) Then

            Else

                Y = objItem.ScreenHeight
            End If

        Next
        objItem = Nothing
        colItems = Nothing
        objWMIService = Nothing

        Screen_Y_Resolution = Y

    End Function

Open in new window

Visual Basic.NET.NET ProgrammingMicrosoft Development

Avatar of undefined
Last Comment
stephenlecomptejr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Mike Tomlinson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
stephenlecomptejr

ASKER
Thank you always for your replies!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes