views:

395

answers:

2

In attempting to upgrade some C++ software to run in Windows 7 I have experienced some problems in being able to create registry keys from scratch.

This is my code:

//
// Create a brand new registry key
//
LONG Registry::CreateRegister( std::string path )
{
    HKEY hKey;  
    DWORD dwDisposition;

    LONG openRes = RegCreateKeyEx( HKEY_CLASSES_ROOT,
                                   path.c_str(),
                                   0, 
                                   NULL,
                                   REG_OPTION_NON_VOLATILE,
                                   KEY_ALL_ACCESS,
                                   NULL,
                                   &hKey,
                                   &dwDisposition );    

    RegCloseKey( hKey );

    return openRes;
}

In Windows XP, the RegCreateKeyEx function successfully creates the registry key, returning a success (0) value. In Windows 7 I am getting a return value of 5 (access denied) from the same function.

I used the regedit tool to ensure that my account has the necessary full permissions, but without success. Can anyone see where I might be going wrong, or if there are other gotchas and known issues I need to be aware of when using Visual Studio within Windows 7?

The software is currently written in Visual Studio 2003.

Thanks in anticipation.

+2  A: 

Since Vista, access to certain areas of the registry has been locked down. The user must have "elevated" permissions. Try running your program with "Run as administrator" (right click it in Explorer).

Daniel Earwicker
Yup. Normal users use `HKCU\Software\Classes`.
MSalters
This is not new for Vista, "normal" users with a non-admin account have never been able to write the HKCR.
Hans Passant
@nobugz - what is new for Vista/7 is that user accounts with admin rights are also blocked by default, until they explicitly choose to elevate a process.
Daniel Earwicker
A: 

See Registry Key Security and Access Rights and RegCreateKeyEx
For parameter "hKey [in] A handle to an open registry key. The calling process must have KEY_CREATE_SUB_KEY access to the key."
This tuto help you Teach Your Apps To Play Nicely With Windows Vista User Account Control
and The Windows Vista and Windows Server 2008 Developer Story: Windows Vista Application Development Requirements for User Account Control (UAC)

lsalamon