Win32: How to use RegisterTypeLib API from standar

2019-03-20 13:15发布

问题:

The Win32 API call RegisterTypeLib() is used to create the registry keys necessary to register a type library.

Unfortunatly, on Windows XP, it tries to write those registry key entries to

HKEY_CLASSES_ROOT\TypeLib

rather than

HKEY_CURRENT_USER\Software\Classes\TypeLib

Meaning that a standard user will not be able to run an ActiveX.

In May 2008 Microsoft released a hotfix for Vista to correct this issue - but the problem remains on Windows XP.

What's a standard-user friendly developer to do?


Answer 1

Use the API call that is designed for it:

RegisterTypeLibraryForUser()

Answer 2

If you can't fix it, hack it:

//begin hack
HKEY key;
RegOpenKeyW(HKEY_CURRENT_USER, @"Software\Classes", out key);
RegOverridePredefKey(HKEY_CLASSES_ROOT, key);

//do original work
RegisterTypeLibrary(...)

//stop hacking
RegOverridePredefKey(HKEY_CLASSES_ROOT, null);
RegCloseKey(key); 

回答1:

You can use the RegOverridePredefKey() API to map the HKEY_CLASSES_ROOT regtree to HKEY_CURRENT_USER\Software\Classes:

  • http://msdn.microsoft.com/en-us/library/ms724901.aspx


回答2:

Take a look at this blog entry I wrote. It will registry ATL COM objects into HKCU instead of HKCR using RegOverridePredefKey. You can use the same technique to call RegisterTypeLib and have it properly register under HKCU

Register ATL as Normal User