WMI SystemRestore InteropServices.COMException Crash. What's wrong?

trparky

Member
Joined
Jun 18, 2011
Messages
7
Location
Cleveland, Ohio, United States
Programming Experience
Beginner
VB.NET:
Imports System.Management
Imports System.Runtime.InteropServices

Module systemRestore
   Function createRestorePoint(strDescription As String, rt As RestoreType, ByRef lSeqNum As Long) As Integer
      Try
         Dim managementScopeObject As New ManagementScope("\\localhost\root\default")
         Dim managementPathObject As New ManagementPath("SystemRestore")
         Dim managementObjectOptions As New ObjectGetOptions()
         Dim managementClassObject As New ManagementClass(managementScopeObject, managementPathObject, managementObjectOptions)
         
         Dim managementBaseObjectParameters As ManagementBaseObject = managementClassObject.GetMethodParameters("CreateRestorePoint")
         managementBaseObjectParameters("Description") = strDescription
         managementBaseObjectParameters("RestorePointType") = rt
         managementBaseObjectParameters("EventType") = 100
         
         Dim oOutParams As ManagementBaseObject = managementClassObject.InvokeMethod("CreateRestorePoint", managementBaseObjectParameters, Nothing)
         
         Return oOutParams("ReturnValue")
      Catch ex As Exception
         MsgBox(ex.Message)
         Return 0
      End Try
   End Function
End Module

I have a number of people saying that they are receiving this particular crash.

Exception Type: System.Runtime.InteropServices.COMException The exception occurred at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options) at My_Program.createRestorePoint(String strDescription, RestoreType rt, Int64& lSeqNum)

I have been unable to narrow down why this crash is occurring. The code works exactly as intended with absolutely no issues on my desktop, notebook, and several instances of Windows (XP, Vista, 7, 8, and 8.1 Update 1) that I have installed in VirtualBox VMs. I'm tearing my hair out here because I have no earthly idea why this is happening. If it were happening on my own machines I'd be able to track down where the issue is but I can't reproduce this crash on my machines or in my VMs.

What am I doing wrong here?
 
I make sure that the user has admin privileges by making a call to this function. If it returns a false response I exit the program during the MyApplication_Startup routine and reload the program with a ProcessStartInfo and Process.Start() function call making sure that I set "runas" as the Verb.

VB.NET:
Public Function areWeAnAdministrator() As Boolean
    Try
        Dim identity As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
        Dim principal As System.Security.Principal.WindowsPrincipal = New System.Security.Principal.WindowsPrincipal(identity)

        If principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator) = True Then
            identity.Dispose()
            Return (True)
        Else
            identity.Dispose()
            Return False
        End If
    Catch ex As Exception
        Return False
    End Try
End Function
 
It turns out that if for whatever reason, System Restore was disabled or turned off for any of the drives in the system, that function call will fail and crash. So I wrote the following code to fix one such possible cause of this crash. I just hope that it's the only cause of this crash.

VB.NET:
' Call like this... enableSystemRestoreOnDrive("C:").  It accepts a drive letter.
Private Function enableSystemRestoreOnDrive(driveLetter As String) As Short
    Try
        Dim managementScopeObject As New ManagementScope("\\localhost\root\default")
Dim managementPathObject As New ManagementPath("SystemRestore")
        Dim managementObjectOptions As New ObjectGetOptions()
        Dim managementClassObject As New ManagementClass(managementScopeObject, managementPathObject, managementObjectOptions)

        Dim managementBaseObjectParameters As ManagementBaseObject = managementClassObject.GetMethodParameters("Enable")
        managementBaseObjectParameters("Drive") = driveLetter
        managementBaseObjectParameters("WaitTillEnabled") = True

        Dim oOutParams As ManagementBaseObject = managementClassObject.InvokeMethod("Enable", managementBaseObjectParameters, Nothing)

        Return oOutParams("ReturnValue")
    Catch ex As Exception
        Return 1
    End Try
End Function

Public Sub enableSystemRestoreOnAllSystemDrives()
    Dim driveLetter As String

    For Each currentDrive As IO.DriveInfo In My.Computer.FileSystem.Drives
        If currentDrive.DriveType = IO.DriveType.Fixed Then
            driveLetter = currentDrive.RootDirectory.ToString.Replace("\", "")
            enableSystemRestoreOnDrive(driveLetter)
        End If
    Next
End Sub
 
OK, something else is wrong. I'm still getting reports of this happening. What could possibly be broken on these people's machines that could be causing this error to occur and how do I fix it?

Like I said, I don't have this problem on my own machines. I'd love to say that these people should just go ahead and "nuke and repave" (reinstall their OS) their machines but I'd like to try and figure out how and why this is happening so that I can code in some procedures to fix it without going jumping to the "nuke and repave" method.
 
Back
Top