Solved

Trapping a specific HResult from a COMexception in VB.net

Posted on 2008-10-27
1
2,330 Views
Last Modified: 2012-05-05
I have a client that is getting the following error...

System.Runtime.InteropServices.COMException (0x80010002): Call was canceled by the message filter. (Exception from HRESULT: 0x80010002 (RPC_E_CALL_CANCELED))
   at System.Management.ManagementScope.Initialize()
   at System.Management.ManagementObject.Initialize(Boolean getObject)
   at System.Management.ManagementClass.GetInstances(EnumerationOptions options)
   at System.Management.ManagementClass.GetInstances()
(etc., etc.)

The error is caused by the WMI not starting up before the program that is crashing.  I plan to add a 10 sleep and try again if the error is HRESULT: 0x80010002 (RPC_E_CALL_CANCELED)...

BUT, I can't reproduce the error, so I don't know if the code If my error handling will work (or if I coded it correctly).  My since the errorCode is a 32-bit Integer, I'm not sure if I can just convert 80010002 to an int (= 2147549186) and not have to pad it.  Please let me know if the attached code snippet is correctly trapping 0x80010002

Thanks!!!!!
Private Function Identifier(ByVal wmiClass As String, ByVal wmiProperty As String) As String
        'Return a hardware identifier
        Try
            Dim Result As String = ""
            Dim mc As New System.Management.ManagementClass(wmiClass)
            Dim moc As System.Management.ManagementObjectCollection = mc.GetInstances
            Dim mo As System.Management.ManagementObject
            For Each mo In moc
                'Only get the first one
                If Result = "" Then
                    Try
                        Result = mo(wmiProperty).ToString
                        Exit For
                    Catch ex As Exception
                        'Ignore error
                    End Try
                End If
            Next mo
 
            Return Result
        Catch ex As Runtime.InteropServices.COMException
            If ex.ErrorCode = 2147549186 Then '0x80010002
                System.Threading.Thread.Sleep(10000)
                Return Identifier(wmiClass, wmiProperty)
            End If
        End Try
        
    End Function

Open in new window

0
Comment
Question by:straubtm
1 Comment
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 22842601
Yeah, that works...   You can also use the "WHEN" statement.   Here is an example:
 

            Try
                ' do something here
            Catch com_ex As System.Runtime.InteropServices.COMException When com_ex.ErrorCode = &H800706BA
                MsgBox("The Remote PC is not responding.  It may be off line or" & vbCr & "firewall settings are preventing the connection", MsgBoxStyle.Exclamation, "Error")
                Exit Sub
            Catch ua_ex As System.UnauthorizedAccessException
                MsgBox("The Remote PC refused the connection.  This is most likely" & vbCr & "due to a permissions issue." & vbCr & "Note: Windows XP Home Edition cannot be configured to support remote connections", MsgBoxStyle.Exclamation, "Error")
                Exit Sub
            Catch wmi_ex As System.Management.ManagementException When wmi_ex.ErrorCode = Management.ManagementStatus.AccessDenied
                MsgBox("You do not have sufficient privileges on the remote PC" & vbCr & "This application requires Administrator rights on the Remote PC", MsgBoxStyle.Exclamation, "Error")
                Exit Sub
            Catch ex As Exception
                If IsNothing(ex.InnerException) Then
                    MsgBox("Could not get initial data" & vbCr & ex.Message, MsgBoxStyle.Critical, "Error")
                Else
                    MsgBox("Could not get initial data" & vbCr & ex.Message & vbCr & ex.InnerException.Message, MsgBoxStyle.Critical, "Error")
                End If
                Exit Sub
            End Try

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help with AsEnumerable(), LINQ 4 40
Assigning handler to UserControls in flowlayoutpanel 4 23
Adding Existing Item to vb.net Project causes Errors 4 32
vb.net class 3 16
A short article about problems I had with the new location API and permissions in Marshmallow
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question