Looping in collection causes Error

Crafty

Member
Joined
Mar 19, 2008
Messages
5
Location
Johannesburg
Programming Experience
1-3
I have created a simple a simple function that collects the software list from a specific machine on the network. It is still in draft format.
The problem is that it only tells me "general error", nothing more. So how do i know what I did wrong?
to test,
Add Ref system.management.dll
add imports

VB.NET:
    Function EnumSoftwareList(ByVal strPC As String, ByVal Delimiter As Char, ByVal Department As String) As Collection
        Dim strDetail As New Collection
        Dim objWMISearch As ManagementObjectSearcher
        Dim colItems As ManagementObjectCollection
        Dim objItem As ManagementObject
        Dim objWMI As ManagementScope = New ManagementScope("\\" & strPC & "\root\cimv2")
        Dim strQuery As String = "Select * From Win32_Product"

        Try
            objWMI.Connect()
            objWMISearch = New ManagementObjectSearcher(objWMI.Path.ToString, strQuery)
            colItems = objWMISearch.Get
            For Each objItem In colItems
                Dim line As String = ""

                line = objItem.GetPropertyValue("Name") & Delimiter & _
                        objItem.GetPropertyValue("Description") & Delimiter & _
                        objItem.GetPropertyValue("IdentifyingNumber") & Delimiter & _
                        objItem.GetPropertyValue("Version") & Delimiter & _
                        Department & Delimiter


                strDetail.Add(line)


                Debug.WriteLine("Name: " & objItem.GetPropertyValue("Name") & vbNewLine & _
                            "Caption: " & objItem.GetPropertyValue("Caption") & vbNewLine & _
                            "Description: " & objItem.GetPropertyValue("Description") & vbNewLine & _
                            "IdentifyingNumber: " & objItem.GetPropertyValue("IdentifyingNumber") & vbNewLine & _
                            "Version: " & objItem.GetPropertyValue("Version") & vbNewLine)

            Next

        Catch ex As Exception
            Dim ErrorMSG As String
            ErrorMSG = "An Error has occured:" & vbNewLine & ex.Message
            MessageBox.Show(ErrorMSG, "Failed Enumurating Software", MessageBoxButtons.OK, MessageBoxIcon.Warning)

            strDetail = Nothing
        End Try
        Return strDetail
    End Function
 
Comment out your try/catch statement, and see what line it falls over on, then we've got a better chance of nailing it.


From what I can see, there is nothing glaringly obvious...
 
I did that in a try to get the real error message, bt it gives the exact same error.
Then I noticed that in the debug screen it outputs the first line. So the error happens on "Next", but still only reports General Error
 
The other approach is to set a breakpoint on each line, until it falls over.
 
Did some testing, and get some security issue if i run the exe on a different pc. That might be a different error though.
Can any one try this function and let me know if it works? Just run it agains localhost.
 
Back
Top