Search code examples
.netdeploymentcomregistrationtypelib

How Can I register a type library just for the current user?


I have a type library called MyCOMAddin.tlb, that I have extracted using tlbexp.exe from a MyCOMAdd.dll. This is a .NET COM exposed class written in C#. This dll is register at per user level.

The question is

  • Is there any way I can also register MyCOMAddin.tlb just the current user?

I tried regtlibv12.exe, but it registers for all-users. This in inconvenient, because if any user un-install the Add-in on the machine, the other users, which will still have access to the dll in late binding, will not have access anymore to the typelibrary.

I am using Inno setup to deploy the add-in, so I could write the keys directly in the registry. But I don't have any idea of which keys and where to write them.

I wish regtlibv12.exe had a /peruser option.


Solution

  • This isn't impossible, but of course you'll be heading for some complications. First off, it isn't sufficient to just register the type library locally, you also need to register the CLSIDs locally or it still won't work correctly. The job done by Regasm.exe. Do favor its /tlb command line option (without a file) over running regtlib. And beware that whatever programming tool uses the type library may need some extra registry keys to recognize the library as a component, the "Control" key is common for example.

    Professional installers do not rely on tools like Regasm or Regtlib to write the keys at install time, they write the keys themselves. Which of course requires knowing what those keys are. One way is to observe the install tool writing the keys with SysInternals' ProcMon utility. Albeit that it tends to generate a lot of info. You'll find a dedicated tool in the WiX project, Heat.exe is the "harvesting" tool. Modify everything you found that way from using HKCR or HKLM to using HKCU instead. HKCR maps to HKCU\Software\Classes. Possibly with Wow6432Node added for 64-bit operating systems.

    And be sure you actually need the type library registered. It is only necessary if you are using the component in another programming environment. And the corner case of using the object created on an STA thread and calling it from another thread, the type library is used by the standard marshaller to marshal the call across apartments. And don't forget about reg-free COM with a manifest so no registry modifications are required at all. It isn't clear whether that would apply here.