Recently, I wrote a class library in C# for use in Office applications, including a critical Access application used by ~70 people. For users with admin rights, registering the DLL is trivial, but getting the DLL working on other machines was problematic.
Registering the DLL for use with admin privileges
- Build the DLL in Visual Studio. Ensure these options are selected on the project's Application tab:
- Output Type: Class Library
- Assembly Information: Make assembly COM-visible: Checked
- Use an elevated command prompt to register the assembly:
RegAsm YourDll.dll /tlb /codebase
- Add a reference to
YourDll.tlb
in VBA: Tools-->References - Verify that you can create an instance of your object
What is happening?
Regasm is doing several things here. First, it is creating a type library (YourDLL.tlb) which provides information about the classes that are in your DLL. Second, it is storing the information about the library in the registry so that the system "knows" what you mean when you ask for a class to be instantiated as an object.
To see what registry entries Regasm is adding, you can run it with the /regfile
parameter:
Regasm YourDLL.dll /codebase /regfile
(The /regfile
option is invalid with the /tlb
parameter.)
If the /codebase
option tells Regasm to include information about where YourDLL.dll
is located on disk, which is important for creating the objects.
If you open YourDLL.reg
in a text editor, you'll see the entries that Regasm is adding to the registry: entries to HKEY_Classes_Root
(which is really just a redirect to HKLM\Software\Classes
). Unfortunately, you need admin privileges to modify HKLM
, so this isn't going to work for our other users.
There are a few other threads (e.g. Registering a COM without Admin rights, Register COM DLL for use by VBA, Register DLL (ActiveX) for non-admin user, Unable to register .NET COM DLL, COM Interop without regasm) that discuss the problem, but their solutions are complex (e.g. require registry redirection) or incomplete (assume you already know at least half of the answer) or do not work in mixed 64/32 bit environments (e.g. Win64, Office32).
So how do you register a COM DLL, created in Visual Studio, for use in VBA 32 and 64 bit environments for the current user, without administrative privileges?