Jan 282010

MFC tries to write application program informatin into the HKLM (Local Machine) which isn’t allowed for non admin applicaitons on Vista and Win7.  The following functions will cause your program to redirect that information to the HKCU (Current User) area in the registry instead.  The OS will happily use the information from there.

In your application code InitInstance add the lines around:

OverrideHKCR();
RegisterShellFileTypes(true);
RestoreHKCR();

bool OverrideHKCR()
{
    // If we can't write to HKEY_CLASSES_ROOT, we should try
    // HKEY_CURRENT_USER\Software\Classes instead.

    HKEY hkcr;
    long ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\Classes"), 0,
                                     KEY_ALL_ACCESS, &hkcr);
    if(ret != ERROR_SUCCESS) { // Need to override
        HKEY hkcu;
        ret = RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Classes"), &hkcu);
        if(ret == ERROR_SUCCESS) {
            ret = RegOverridePredefKey(HKEY_CLASSES_ROOT, hkcu);
            RegCloseKey(hkcu);
            return ret == ERROR_SUCCESS;
        }
    }
    else {
        RegCloseKey(hkcr);
    }
    return false; // Didn't need to do this
}

void RestoreHKCU()
{
    RegOverridePredefKey(HKEY_CLASSES_ROOT, NULL);
}

Leave a Reply

(required)

(required)