tags:

views:

36

answers:

1

I am trying to use GetMultiStringValue to read a REG_MULTI_SEZ from remote Registry. This function always returns the errorcode 2. And the array it supposed to fill with the REG_MULTI_SEZ is null. I am fighting with this since morning without any success.

Here is the sample code I am using to read REG_MULTI_SEZ in C#.

    ConnectionOptions connectionOptions = new ConnectionOptions();
            //connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
            //connectionOptions.EnablePrivileges = true;
            connectionOptions.Username = domain +"\\" +userName;
            connectionOptions.Password = password ;

            ManagementScope managementScope = new ManagementScope("\\\\" + server  + "\\root\\default", connectionOptions);
            ManagementPath managementPath = new ManagementPath("StdRegProv");
            ManagementClass managementClass = new ManagementClass(managementScope, managementPath, null); // null should be connectionOptions ?
            ManagementBaseObject inParams = managementClass.GetMethodParameters("GetMultiStringValue");
            inParams["hDefKey"] = HKEY_LOCAL_MACHINE;
            inParams["sSubKeyName"] = key;
            inParams["sValueName"] = valueName;                
            ManagementBaseObject outParams = managementClass.InvokeMethod("GetMultiStringValue", inParams, null);
                if (Convert.ToUInt32(outParams["ReturnValue"]) == 0)
                {
                    if (outParams["sValue"] != null)
                    {
                        Object objReturn = outParams["sValue"];
                        if (objReturn is Array)
                        {
                            string[] multStr = (string[])objReturn;
                            return string.Join("|", multStr);
                        }
                    }
                }

I have tried to do the same with VBS and end up with the same result. Irony is i have taken this piece of VBS from the MSDN. Even this script fails with the same errorcode (2).

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Services\Eventlog\System"
strValueName = "Sources"
Returnval = objReg.GetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath,strValueName,arrValues)
If (Returnval = 0) And (Err.Number = 0) Then   
    For Each strValue In arrValues
    WScript.Echo  strValue
 Next
Else
    Wscript.Echo "GetMultiStringValue failed. Error = " & Err.Number
 Wscript.Echo "Return value " & Returnval
End If

If anybody ever faced this issue, Could u point me what I am missing here.

Thanks Moorthi

A: 

You are correct it has end up very obvious. The registry key I was looking for was not present on the Remote machine. Found that later that day.. Anyhow thanks for the update.

Actually Error code 2 mean Object not found.

Moorthi