The following is a summary from a social.msdn thread . Does it sound familiar ? I have just wasted an hour and could have wasted much more trying to understand why RegOpenKeyEx fails in Windows 7 in C++ code that works on Vista and XP…
————————————————————————————-
Kagsw
I have an application that stores the application database location in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\EasySoft. The VB6 application uses the registry API (RegOpenKey and RegQueryValue) to locate the database and works correctly on both XP and Vista. When run on Windows 7, the call to RegOpenKey fails returning an error code of 5 (access denied). The application install creates the registry entry with read access for all users. I have verified this happened on the windows 7 install using RegEdit. The entry is being created and the permission is being set to READ (same as in Vista and XP). To see if I could better understand the problem, I tried many unacceptable solutions including manually setting full control for all users, running the application as administrator and even turning off UAC. In every case, RegOpenKey returned error code 5 (getting access denied with user access control turned off seems impossible but this also failed). Any help, even suggestions about other things to check/test, would be greatly appreciated.
————————————————————————————-
Kagsw
Well, I finally figured out the problem. In the code example I downloaded (as with many others like http://www.codetoad.com/vb_modify_registry.asp) there was the constant ERROR_ACCESS_DENIED = 5 defined. While looking at potential issues with ADVAPI32.DLL, where the function I am call resides, I found another example with the constant ERROR_ACCESS_DENIED = 8 and another constant ERROR_CANTWRITE = 5 defined. When I changed my application to ignore ERROR_CANTWRITE, it now works correctly.
I can understand adding a new return code since Windows 7 changed the access levels of this registry tree, but why change the meaning of an existing return value instead of adding a new one? And why not tell the development community what you did? One very simple note anywhere on the web and this would have been a 5 minute fix. Instead I spent many hours and wasted 2 weeks. Thanks for the great New Years present.
————————————————————————————-
Irina AM
Hi!
I familiar to this problem, just replace RegOpenKeyEx function with RegCreateKeyEx (declaring 2 additional params). It opens existing key or creates new.
It works fine!
————————————————————————————-
My note – Irena’s reply works. Just remember to use RegCreateKeyEx with KEY_QUERY_VALUE as the 6th parameter