Why is RegOpenKeyEx() returning error code 2 on Vista 64bit?
Asked Answered
C

5

32

I was making the following call:

result = RegOpenKeyEx(key, s, 0, KEY_READ, &key);

(C++, Visual Studio 5, Vista 64bit).

It is failing with error code 2 ("File not found") even though "regedit" shows that the key exists. This code has always worked on 32bit XP. Why is it "file not found" when it clearly is there?

Crellen answered 31/10, 2008 at 0:58 Comment(0)
C
63

I discovered that I could solve my problem using the flag: KEY_WOW64_64KEY , as in:

result = RegOpenKeyEx(key, s, 0, KEY_READ|KEY_WOW64_64KEY, &key);

For a full explanation: 32-bit and 64-bit Application Data in the Registry

Crellen answered 31/10, 2008 at 0:58 Comment(0)
A
26

On a Windows 64-bit system the Registry is actually divided into two parts. One section is used by 64-bit processes, and one part by 32-bit processes.

For example, if a 32-bit application programatically writes to what it believes is HKLM\SOFTWARE\Company\Application, it's actually redirected by the WoW64-layer to HKLM\SOFTWARE\Wow6432Node\Company\Application.

So when you run your 32-bit application and call RegOpenKeyEx it's actually working against the Wow6432Node\ folder, and not the regular \SOFTWARE node.

Acroterion answered 14/11, 2008 at 19:15 Comment(1)
Note that you should not rely on the key being called "Wow6432Node". Access the other registry view using the flags to RegOpenKeyEx instead.Gabriellagabrielle
L
1

You have to compile with "Use Multi-Byte Character Set" or cast string in code to (LPWSTR)

Leotaleotard answered 4/6, 2017 at 14:22 Comment(0)
L
0

I had a similar problem. I was using:

dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                   (LPWSTR)"SOFTWARE\\0test",
                                   0,
                                   WRITE_DAC ,
                                   &hKey);

That didn't work. I tried it like this and it worked:

dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                                   _T("SOFTWARE\\0test"),
                                   0,
                                   WRITE_DAC ,
                                   &hKey);
Leucomaine answered 23/6, 2010 at 18:52 Comment(1)
Never just insert casts to shut up the compiler. The compiler correctly refused to compile the first one without a cast.Gabriellagabrielle
A
0

yes,win7 64B,add further flag KEY_WOW64_64KEY ,it will work. if not work, refer to http://msdn.microsoft.com/en-us/library/ms724897(v=VS.85).aspx

Amplexicaul answered 21/11, 2011 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.