S
Schroeder, AJ
Hello,
I am trying to delete keys via WMI using VB.NET. I have used the "EnumKeys"
method before, but the "DeleteKey" method is obviously different. The error
that I get is "The specified cast is invalid" on the GetMethodParameters
line, and I have googled and there isn't much out there specifically for
what I am doing.
I am hoping to the members in these newsgroups will be able to provide me
with some advice, it would be much appreciated!
Along with my thanks, the code is below:
Enum MainRegHives
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
HKEY_CURRENT_CONFIG = &H80000005
End Enum
Dim strHKLM As MainRegHives
Dim objEnumDeleteKeys As System.Management.ManagementBaseObject
Dim objDeleteKeys As System.Management.ManagementObject
Dim WMIManagementClass As System.Management.ManagementClass
Dim WMIManagementScope As System.Management.ManagementScope
Try
strHKLM = MainRegHives.HKEY_LOCAL_MACHINE
WMIManagementScope = New System.Management.ManagementScope
With WMIManagementScope
..Path.Server = strServer
..Path.NamespacePath = "root\default"
..Options.EnablePrivileges = True
'.Options.Username = "AdminUser
'.Options.Password = "adminpass"
'.Options.Impersonation = ImpersonationLevel.Impersonate
' For XP compatibility
'.Options.Authentication = System.Management.AuthenticationLevel.Packet
' For 2000/NT compatibility
'.Options.Authentication = System.Management.AuthenticationLevel.Connect
End With
WMIManagementScope.Connect()
Catch ex As Exception
End Try
Try
WMIManagementClass = New System.Management.ManagementClass("StdRegProv")
With WMIManagementClass
..Scope = WMIManagementScope
End With
Catch ex As ManagementException
Console.WriteLine(("Failed to query WMI on " & strServer & ": " & ex.Message
& " Error code: " & ex.ErrorCode))
End Try
Try
objDeleteKeys = WMIManagementClass.GetMethodParameters("DeleteKey")
With objDeleteKeys
..SetPropertyValue("hDefKey", CType("&H" & Hex(strHKLM), Long))
..SetPropertyValue("sSubKeyName", strRegPath)
End With
objDeleteKeys.Delete()
Catch ex As ManagementException
Debug.WriteLine(ex.ErrorInformation)
End Try
I am trying to delete keys via WMI using VB.NET. I have used the "EnumKeys"
method before, but the "DeleteKey" method is obviously different. The error
that I get is "The specified cast is invalid" on the GetMethodParameters
line, and I have googled and there isn't much out there specifically for
what I am doing.
I am hoping to the members in these newsgroups will be able to provide me
with some advice, it would be much appreciated!
Along with my thanks, the code is below:
Enum MainRegHives
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
HKEY_CURRENT_CONFIG = &H80000005
End Enum
Dim strHKLM As MainRegHives
Dim objEnumDeleteKeys As System.Management.ManagementBaseObject
Dim objDeleteKeys As System.Management.ManagementObject
Dim WMIManagementClass As System.Management.ManagementClass
Dim WMIManagementScope As System.Management.ManagementScope
Try
strHKLM = MainRegHives.HKEY_LOCAL_MACHINE
WMIManagementScope = New System.Management.ManagementScope
With WMIManagementScope
..Path.Server = strServer
..Path.NamespacePath = "root\default"
..Options.EnablePrivileges = True
'.Options.Username = "AdminUser
'.Options.Password = "adminpass"
'.Options.Impersonation = ImpersonationLevel.Impersonate
' For XP compatibility
'.Options.Authentication = System.Management.AuthenticationLevel.Packet
' For 2000/NT compatibility
'.Options.Authentication = System.Management.AuthenticationLevel.Connect
End With
WMIManagementScope.Connect()
Catch ex As Exception
End Try
Try
WMIManagementClass = New System.Management.ManagementClass("StdRegProv")
With WMIManagementClass
..Scope = WMIManagementScope
End With
Catch ex As ManagementException
Console.WriteLine(("Failed to query WMI on " & strServer & ": " & ex.Message
& " Error code: " & ex.ErrorCode))
End Try
Try
objDeleteKeys = WMIManagementClass.GetMethodParameters("DeleteKey")
With objDeleteKeys
..SetPropertyValue("hDefKey", CType("&H" & Hex(strHKLM), Long))
..SetPropertyValue("sSubKeyName", strRegPath)
End With
objDeleteKeys.Delete()
Catch ex As ManagementException
Debug.WriteLine(ex.ErrorInformation)
End Try