Home All Groups Group Topic Archive Search About

WMI - Executing a method to read registry - Going Crazy!

Author
27 Apr 2006 7:23 PM
Bmack500
Hello, and thanks in advance. I have the below code which keeps
returning "1" as the result. It's never successful, and I don't know
what I'm doing wrong. I know that when I get to objManagementClass &
objManagmeentBaseObject, the VB debugger says "can't read value" on all
lines. Bu tI can read the objPath, and the scope just fine.


        Dim strStringValue As String = ""

        'Dim strValue As String
        Dim objManagementScope As ManagementScope
        Dim objManagementClass As ManagementClass
        Dim objManagementBaseObject As ManagementBaseObject
        Dim objPath As ManagementPath

        sSubKey = sSubKey.Trim
        strValue = strValue.Trim

        If (sSubKey.Length > 0) Then
            'Connect to the specified computer registry
            objPath = New ManagementPath
            objPath.Server = "SERVERNAME(WIN2003)"
            objPath.NamespacePath = "root\default"
            objPath.ClassName = "stdRegProv"

            objManagementScope = New ManagementScope(objPath)
            objManagementScope.Options.EnablePrivileges = True
            objManagementScope.Options.Impersonation =
ImpersonationLevel.Impersonate
            objManagementScope.Connect()


            'Retrieve the required string value from the registry

            If objManagementScope.IsConnected Then
                objManagementClass = New ManagementClass(objPath)
                objManagementClass.Scope = objManagementScope
                objManagementBaseObject =
objManagementClass.GetMethodParameters("GetStringValue")
                objManagementBaseObject.SetPropertyValue("sSubKeyName",
sSubKey)
                objManagementBaseObject.SetPropertyValue("sValueName",
strValue)
                objManagementBaseObject =
objManagementClass.InvokeMethod("GetStringValue",
objManagementBaseObject, Nothing)
                strReturn =
objManagementBaseObject.Properties.Item("ReturnValue").Value.ToString()
                If strReturn = "0" Then
                    strResult =
CType(objManagementBaseObject.Properties.Item("sValue").Value, String)
                Else
                    strReturn =
objManagementBaseObject.Properties.Item("ReturnValue").Value.ToString
                End If
                'objManagementclass.
            End If
        End If

Author
28 Apr 2006 5:05 AM
Cor Ligthert [MVP]
BMack,

Andy reason that you don't use the registry class for this?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfmicrosoftwin32registryclasstopic.asp

I hope this helps,

Cor

Show quoteHide quote
"Bmack500" <brett.m***@gmail.com> schreef in bericht
news:1146165802.615235.238020@y43g2000cwc.googlegroups.com...
> Hello, and thanks in advance. I have the below code which keeps
> returning "1" as the result. It's never successful, and I don't know
> what I'm doing wrong. I know that when I get to objManagementClass &
> objManagmeentBaseObject, the VB debugger says "can't read value" on all
> lines. Bu tI can read the objPath, and the scope just fine.
>
>
>        Dim strStringValue As String = ""
>
>        'Dim strValue As String
>        Dim objManagementScope As ManagementScope
>        Dim objManagementClass As ManagementClass
>        Dim objManagementBaseObject As ManagementBaseObject
>        Dim objPath As ManagementPath
>
>        sSubKey = sSubKey.Trim
>        strValue = strValue.Trim
>
>        If (sSubKey.Length > 0) Then
>            'Connect to the specified computer registry
>            objPath = New ManagementPath
>            objPath.Server = "SERVERNAME(WIN2003)"
>            objPath.NamespacePath = "root\default"
>            objPath.ClassName = "stdRegProv"
>
>            objManagementScope = New ManagementScope(objPath)
>            objManagementScope.Options.EnablePrivileges = True
>            objManagementScope.Options.Impersonation =
> ImpersonationLevel.Impersonate
>            objManagementScope.Connect()
>
>
>            'Retrieve the required string value from the registry
>
>            If objManagementScope.IsConnected Then
>                objManagementClass = New ManagementClass(objPath)
>                objManagementClass.Scope = objManagementScope
>                objManagementBaseObject =
> objManagementClass.GetMethodParameters("GetStringValue")
>                objManagementBaseObject.SetPropertyValue("sSubKeyName",
> sSubKey)
>                objManagementBaseObject.SetPropertyValue("sValueName",
> strValue)
>                objManagementBaseObject =
> objManagementClass.InvokeMethod("GetStringValue",
> objManagementBaseObject, Nothing)
>                strReturn =
> objManagementBaseObject.Properties.Item("ReturnValue").Value.ToString()
>                If strReturn = "0" Then
>                    strResult =
> CType(objManagementBaseObject.Properties.Item("sValue").Value, String)
>                Else
>                    strReturn =
> objManagementBaseObject.Properties.Item("ReturnValue").Value.ToString
>                End If
>                'objManagementclass.
>            End If
>        End If
>
Author
19 May 2006 1:26 PM
Bmack500
Thanks! Got it working.