Redirecting Registry Keys Using RegOverridePredefKey

The other day, I had somebody send me an email from the contact form here which was asking about a Windows API I almost never see, but nonetheless could come in handy in some scenarios.

Let's talk about these scenarios.

With Windows Vista, more people than ever are running as a standard user. For the IT Pro, we have a number of shims available to take applications that have dependencies on administrator rights and remove some of these dependencies. For developers, we have guidance for updating an application to run correctly as a standard user.

But ... what if you are a developer writing your own code, but you are also using third party code as part of your product - and it's the third party code that has the problem? And what if you don't have the source code? While we don't prevent developers from shipping shims with their application, we do really discourage it. But, in this scenario, what do we encourage as the alternative?

And, for one specific scenario, it turns out that there is an API that can really come in handy if your problem is in the registry. That API? RegOverridePredefKey (in advapi32).

Say, for example, that the third party code you are leveraging in your application is doing runtime registration of a COM object into HKEY_CLASSES_ROOT. As a non-admin, that doesn't work so well. An IT Pro could shim that with VirtualizeHKCRLite, but developers don't generally like to take that approach. Instead, you could use this API to remap this predefined key.

Here's an example:

HKEY hKey;
DWORD dDisposition;
// Comment this section out to turn off registry redirection and hit HKCR
HKEY hNewKey;
RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\RegOverridePredefKey", NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hNewKey, &dDisposition);
RegOverridePredefKey(HKEY_CLASSES_ROOT, hNewKey);
// End override section
LONG fCreateKey = RegCreateKeyEx(HKEY_CLASSES_ROOT, L"LUA Bug Registry Key", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dDisposition);
if (fCreateKey == ERROR_SUCCESS) {
    MessageBox(0, L"Registry key created.", L"LUA - Registry", MB_OK | MB_ICONINFORMATION);
} else {
    MessageBox(0, L"Could not create registry key.", L"LUA - Registry", MB_OK | MB_ICONERROR);

If you call this API a second time, passing NULL as the second parameter, you can reset the key mapping back to the default. This can be a handy trick if you are trying to fix up third party code that is the final LUA bug on your way to making an application work as a standard user and your third party control seems to be struggling to get a version that doesn't depend on admin rights.

Comments (1)

  1. Aaron Margosis says:

    Indeed!  Check out this tool, that does exactly that to register ActiveX/COM as per-user:

Skip to main content