|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
(newbie warning) vb.net, stdregprov, deletekey - Invalid castI 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 Hi,
GetMethodParameters returns a ManagementBaseObject not a ManagementObject. You can not convert a manamentbaseobject to an managementobject. Why not use the win32.registry classes. You can use the registrykeys openremotebasekey to access the registry on another machine. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfmicrosoftwin32registrykeyclassopenremotebasekeytopic.asp Ken ------------------ "Schroeder, AJ" <a**@qg.com> wrote in message I am trying to delete keys via WMI using VB.NET. I have used the "EnumKeys"news:da4acs$1o35$1@sxnews1.qg.com... Hello, 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 Doh! I didn't even read that part in MSDN... <sigh>
I have coded quite a bit of this in WMI already, but there are just some things that are horribly complex in WMI, so I might just go ahead and try out impersonation in VB and then do the win32 registry classes. WMI is cool, but just way too complex for what I am trying to do. Thanks Ken, Cheers, AJ Hi,
I don't understand why anyone would use VB.NET for this type of programming? I've never seen such convoluted code for such a simple task. VB is the worst language available in .NET and is not standards based. Forgetting the cast, can you clarify strRegPath in the code below. I can't see where it's defined. Schroeder, AJ wrote: Show quoteHide quote > 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 > > -- Gerry Hickman (London UK) Well, everyone around here uses VB, so it was a natural choice because
of the learning curve. I agree, this code is quite convoluted (it is even more apparent now that I am looking at C# examples), maybe I'll cut my losses and switchover to C#. It does look WAY easier! Anyway, strRegPath is just a parameter that I am passing from Main. Tis just a path to the registry keys... BTW - I have figured it out and I changed my ManagementObject to a ManagementBaseObject and now the keys are being removed... Gerry Hickman wrote: Show quoteHide quote > Hi, > > I don't understand why anyone would use VB.NET for this type of > programming? I've never seen such convoluted code for such a simple > task. VB is the worst language available in .NET and is not standards based. > > Forgetting the cast, can you clarify strRegPath in the code below. I > can't see where it's defined. > > Schroeder, AJ wrote: > > 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 > > > > > > > -- > Gerry Hickman (London UK) Hi AJ,
> Well, everyone around here uses VB, Why? > so it was a natural choice because You are right, C# is easier, and 100 times more elegant and > of the learning curve. I agree, this code is quite convoluted (it is > even more apparent now that I am looking at C# examples), maybe I'll > cut my losses and switchover to C#. It does look WAY easier! professional. It's also standards based, so you have options to use it on Linux too. > Anyway, strRegPath is just a parameter that I am passing from Main. Tis OK, I only asked so I could test a much cleaner and meaner JScript > just a path to the registry keys... version which cuts out all the .NET bloat. You only need a few Kb of DLLs to do this kind of thing, not 65Mb of badly designed bloatware. > BTW - I have figured it out and I changed my ManagementObject to a Great! Glad it's working now.> ManagementBaseObject and now the keys are being removed... -- Gerry Hickman (London UK) Hello Gerry,
>> Well, everyone around here uses VB, Not sure really, it seems to be the platform of choice for my company.>Why? Probably because we are primarily a MS shop and the older developers are used to VB from version 2 on up... > You are right, C# is easier, and 100 times more elegant and I have dabbled a little bit with C on unix a bit, and I like it and> professional. It's also standards based, so you have options to use it > on Linux too. understand it better than the vb.net stuff... that does it, time to switch! On top of the convoluted code I have run into stumbling block after stumbling block trying to get this application I am writing to work in VB. I appears I can do the same thing in C# in half the code. Thank you for your input, AJ Schroeder AJ,
> I am writing to work in VB. I appears I can do the same thing in C# in This is mostly because somebody uses his basic programming language and > half the code. > change only the keywords. Example DataSet ds = new Dataset(); dim ds as New Dataset which can as well written as dim ds as Dataset = New DataSet int i = Convert.ToInt32("1"); dim i as integer = CInt("1") which can as well be written as dim i as integer = Convert.ToInt32("1") I hope this gives an idea what I want to tell Cor
Loop thru all subfolders and list all files under each
imagelist and bitmaps saved bitmap size How to convert error code to text?? Two quick Questions Need a Strategy to store the Single Quotes in the Database My.Settings.Add Loading two separate instance of the same assembly Error: Cast From String to type Integer not valid Use another login |
|||||||||||||||||||||||