Modifying ATL Project to Register Per User

A week ago, I blogged about how to register COM objects as LUA (https://blogs.msdn.com/jaredpar/archive/2005/05/17/418780.aspx).  This is a follow up post, to show you how to alter your standard ATL registration code so that your DLL will register for the current user instead of for the machine. 

This code uses the RegOverridePreDefKey() method to override HKCR with the registration point for the current user (HKCU\Software\Classes). 

// DllRegisterServer - Adds entries to the system registry

STDAPI DllRegisterServer(void)
{
// registers object, typelib and all interfaces in typelib for the current user
HKEY key;
if ( ERROR_SUCCESS != ::RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Classes", &key) )
{
return E_FAIL;
}
if ( ERROR_SUCCESS != ::RegOverridePredefKey(HKEY_CLASSES_ROOT, key) )
{
::RegCloseKey(key);
return E_FAIL;
}
HRESULT hr = _AtlModule.RegisterServer(true);
::RegCloseKey(key);
return hr;
}

"This posting is provided "AS IS" with no warranties, and confers no rights"