troubleshooting Question

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

Avatar of stephenlecomptejr
stephenlecomptejrFlag for United States of America asked on
.NET ProgrammingVisual Basic.NETMicrosoft Development
2 Comments1 Solution673 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Mike Tomlinson
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros