I came across the function RegOverridePredefKey, and it looked like it would do the job by allowing me to hook into a root HKEY and write to a completely different HKEY. So I started looking in the Delphi source code, and noticed that this function was not imported. The following code snippet shows the procedure that I ended up writing to have anything written to HKCR be written to HKCU instead.
function RegOverridePredefKey(hKey: HKEY; hNewKey: HKEY): Longint; stdcall; external advapi32 name 'RegOverridePredefKey'; procedure OverrideRegistryKey(Register: boolean); var HKCU: HKEY; ret: integer; begin if Register then begin RegOpenKeyEx(HKEY_CURRENT_USER, 'Software\Classes', 0, HEY_ALL_ACCESS, HKCU); try ret := RegOverridePredefKey(HKEY_CLASSES_ROOT, HKCU); if ret <> ERROR_SUCCESS then ShowMessage('Error overriding HKCU:' + #13#10 + SysErrorMessage(ret)); finally RegCloseKey(HKCU); end; end else RegOverridePredefKey(HKEY_CLASSES_ROOT, 0); end;
OverrideRegistryKey(true); try Application.Initialize; finally OverrideRegistryKey(false); end;
In my production code, I'll also put in a quick check to make sure that we're running on Windows 2000 or greater. Since we run both as a Server and a Service, I'll put this code in a conditional define, since there is no concept of HKCU in a service.
Note: I also failed to find the header for RegOpenUserClassesRoot in Delphi. If you find that you need this function, you'll need to import the function yourself.
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.