# RegOpenKeyEx and Windows 7



## AncientDragon (Nov 29, 2009)

I am running Windows 7 as Admin account but RegOpenKeyEx returns error 5, Access Deined. The problem does not occur on Vista and earlier operating systems. The MSDN description of that function indicates I may need to set additional permissions, or take ownership of the key. Using regedit32.exe I navigated to the key and looked at the permissions. It appears that I already have full access permissions. 

Anyone have any idea how to resolve this problem? Compiler is VC++ 2008 Express and I'm compiling for UNICODE.


```
int SetRegistry()
{
          HKEY hKey;
          char filename[] = "C:\\Windows\\filenamehere.exe";

          int n = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
              0,KEY_ALL_ACCESS,&hKey);
          if( n == ERROR_SUCCESS)
          {
            n = RegSetValueEx(hKey, TEXT("filenamehere"),0,REG_SZ,(LPBYTE) filename,sizeof(filename));
            RegCloseKey(hKey);
          }
          else
          {
                TCHAR buf[255];
                FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,n,0,buf,sizeof(buf)/sizeof(TCHAR),0);
                wcout << buf << L"\n";

          }

          return n;
}
```


----------

