Mathias Olsson
Mathias Olsson

Reputation: 13

C# not reading registry values from Wow6432Node on Windows Server 2012 64-bit

I am moving three Windows Services (.NET 3.5) from Windows Server 2003R2, to Windows Server 2012 R2 (.NET 4.5).

The first two went well. Reading registry settings from [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MyCompany\MyApplication].

Now here´s the funny stuff - the third one only works when I store settings in [HKEY_LOCAL_MACHINE\SOFTWARE\MyCompany\MyApplication] (whitout Wow6432Node)?

And, if I try to run the service with settings in 32-bit registry it reads the settings ok but I get this assembly binding error instead: System.BadImageFormatException: Could not load file or assembly 'Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format. The other two, using the same dll, works fine.

Any ideas? What is different in the third Windows Service?

Since all applications using the same code to read the registry I don´t think that´s the problem. Her´s the code (simplyfied) anyway.

    private string getRegistrySetting(string keyName)
    {
        string softwareSubkeyName = "SOFTWARE";
        using (RegistryKey softwareSubkey = Registry.LocalMachine.OpenSubKey(softwareSubkeyName, false))
        {
            string lmSubkeyName = "MyCompany\\MyApplication;
            using (RegistryKey lmSubkey = softwareSubkey.OpenSubKey(lmSubkeyName))
            {
                return lmSubkey.GetValue(keyName).ToString();
            }
        }
    }

Upvotes: 1

Views: 817

Answers (1)

Dirk Vollmar
Dirk Vollmar

Reputation: 176159

First, you need to make sure that all your executables have the target platform set to x86 (and not to AnyCPU) on the build tab of the project's property pages (caveat: this is a per-build-configuration setting, you need to set the target platform for both Debug and Release build).

Then you need to make sure that you also deploy the 32-bit version of any third-party components such as Oracle.DataAccess. The reason is that a 32-bit process cannot load a 64-bit dll and vice versa.

The target platform is relevant because it determines if your process will be started as a 32-bit or 64-bit process. If your executable runs as a 64-bit process Registry and file system redirection won't be in place - as a result your process will read and write directly to HKEY_LOCAL_MACHINE\SOFTWARE\ and not to the Wow6432Node subnode.

Upvotes: 1

Related Questions