Monday, 29 February 2016

Force WMI to read the 32-bit Registry Hive on a 64-bit Computer

In some cases, the registry settings for installed Applications will be available only in WoW6432Node even in 64-bit machine. So, we need to force WMI to access the 32-bit Registry Hive in 64 bit computer.

Use the following C# code to access 32 bit registry hive information in 64 machine. We can force WMI to load the 32-bit provider by adding the ConnectionOptions __ProviderArchitecture and __RequiredArchitecture.
private static void Read32RegistryIn64Machine()
{
    uint LOCAL_MACHINE = 0x80000002;
    ConnectionOptions options = new ConnectionOptions();
    options.Impersonation = ImpersonationLevel.Impersonate;
    options.EnablePrivileges = true;
    options.Username = "MyUserName";
    options.Password = "MyPassword";

    ManagementScope mgmtScope = new ManagementScope("\\\\" + "ComputerName" + "\\root\\default", options);

    mgmtScope.Options.Context.Add("__ProviderArchitecture", 32);
    mgmtScope.Options.Context.Add("__RequiredArchitecture", true);

    ManagementClass mc = new ManagementClass(mgmtScope, new ManagementPath("StdRegProv"), null);
    ManagementBaseObject inParams = mc.GetMethodParameters("EnumKey");
    inParams["hDefKey"] = LOCAL_MACHINE;
    inParams["sSubKeyName"] = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

    ManagementBaseObject outParams = mc.InvokeMethod("EnumKey", inParams, null);
    inParams = mc.GetMethodParameters("GetStringValue");
    inParams["hDefKey"] = LOCAL_MACHINE;

    foreach (string name in (string[])outParams["sNames"])
    {
        inParams["sSubKeyName"] = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" + "\\" + name;
        inParams["sValueName"] = "DisplayName";
        outParams = mc.InvokeMethod("GetStringValue", inParams, null);
        Console.WriteLine(outParams["sValue"]);
    }
}

Advertisements
Advertisements

No comments:

Post a Comment