Link to home
Start Free TrialLog in
Avatar of stephenlecomptejr
stephenlecomptejrFlag 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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stephenlecomptejr

ASKER

Thank you always for your replies!