Thoughts from Dan Miser RSS 2.0
# Thursday, September 23, 2004
By default, COM Servers register themselves in HKEY_CLASSES_ROOT (HKCR). The actual registry entries will get written to HKEY_LOCAL_MACHINE (HKLM) in most cases. HKCR is an alias to HKEY_LOCAL_MACHINE in older versions of Windows, but starting in Windows 2000 and later, HKCR is a merged view of HKLM and HKEY_CURRENT_USER (HKCU). I needed a way to register my COM server into HKCU so that locked down machines that don't have access to HKLM could still run properly. For some background information on HKCR, check out MSDN.

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;
So that left me with the question "Where do I call this?". My first thought was to hook into the UpdateRegistry method and call my OverrideRegistry procedure there. Unfortunately, by the time UpdateRegistry is called in my Remote DataModule (RDM), several other registration calls have already finished. This left me with some things in HKLM and some in HKCU. Then I remembered that the COM registration occurs when Application.Initialize is called in the dpr file. The following is the code I use in my dpr to write to the right place in the registry.

  OverrideRegistryKey(true);
  try
    Application.Initialize;
  finally
    OverrideRegistryKey(false);
  end;
After doing this, I checked in regedit and verified that everything was registered in HKCU, and it was there. Since HKCR shows the merge of HKLM and HKCU, it was also present in HKCR. This means that COM calls will find the app server. Running a quick test showed me that everything worked as I expected.

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.

Thursday, September 23, 2004 10:48:00 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
Delphi
Tracked by:
http://9nn-information.info/90318987/index.html [Pingback]
http://9no-information.info/99521946/index.html [Pingback]
http://9nf-information.info/72555784/index.html [Pingback]
http://9no-information.info/07873834/hemophilia-disease-management-outcomes.html [Pingback]
http://9no-information.info/06668347/photo-of-carey-loftin.html [Pingback]
http://9no-information.info/03912079/index.html [Pingback]
http://9nc-information.info/69059362/book-clubs-world-ebooks.html [Pingback]
http://9nk-information.info/81961538/index.html [Pingback]
http://9np-information.info/73192962/how-do-i-liquefy-melted-chocolate.html [Pingback]
http://9ng-information.info/91878960/index.html [Pingback]
http://9no-information.info/72178269/index.html [Pingback]
http://9od-information.info/31849916/index.html [Pingback]
http://9qi-information.info/92989693/index.html [Pingback]
http://9ol-information.info/30327121/index.html [Pingback]
http://9oi-information.info/27723334/index.html [Pingback]
http://9ot-information.info/95014592/index.html [Pingback]
http://9ow-information.info/73047881/park-lake-internet-community.html [Pingback]
http://9oa-information.info/75809405/index.html [Pingback]
http://9rl-information.info/57084620/index.html [Pingback]
http://9ri-information.info/43539446/index.html [Pingback]
http://9ry-information.info/52214184/index.html [Pingback]
http://9rq-information.info/77206199/index.html [Pingback]
http://9rl-information.info/33318348/coupons-enterprise-car-rental.html [Pingback]
http://9sc-information.info/05893373/index.html [Pingback]
http://9uafd-le-informazioni.info/11919787/index.html [Pingback]
http://9uaeh-le-informazioni.info/50688654/mappa-linea-tram-roma.html [Pingback]
http://9uafd-le-informazioni.info/74363490/index.html [Pingback]
http://9uaef-le-informazioni.info/24128209/index.html [Pingback]
http://9uael-le-informazioni.info/22358903/case-in-affitto-bergamo.html [Pingback]
http://9uafn-le-informazioni.info/94159734/attiva-dominio-primo-anno-gratis.html [Pingback]
http://9uafg-le-informazioni.info/60806121/index.html [Pingback]
http://9uaft-le-informazioni.info/95024900/san-julian.html [Pingback]
http://9uagr-le-informazioni.info/09748130/index.html [Pingback]
http://9uagq-le-informazioni.info/91249570/index.html [Pingback]
http://9uagp-le-informazioni.info/46381182/index.html [Pingback]
http://9uagb-le-informazioni.info/87882445/index.html [Pingback]
http://9uagd-le-informazioni.info/21615124/legge-39-1989.html [Pingback]
http://9uahs-le-informazioni.info/22165845/index.html [Pingback]
http://9uahq-le-informazioni.info/64036000/programma-modificare-film.html [Pingback]
http://9uahc-le-informazioni.info/92714514/rendering-ville.html [Pingback]
http://9uahl-le-informazioni.info/36875596/index.html [Pingback]
Navigation
Archive
<December 2014>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Dan Miser
Sign In
Statistics
Total Posts: 388
This Year: 0
This Month: 0
This Week: 0
Comments: 630
Themes
Pick a theme:
All Content © 2014, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)