Background
The command regsvr32 is used to register a native, unmanaged code DLL so that it is available via COM. With a registered DLL you can use COM Interop to call that code from .NET Managed code.Regsvr32 is unmanaged code and as such makes use of some existing functions that are defined in the kernel32.dll. Fortunately .NET makes available a pretty easy to use foreign function interface (FFI) in the form of P/Invoke.
In general to call an unmanaged function you just need to use the DllImport annotation on an extern function to tell the CLR how to access the function.
e.g.:
Registering an Unmanaged DLL with C# Code
regsvr32 actually calls functions defined within the DLL itself in what is known as a self-registering DLL. So assuming your DLL is self-registering then you should be able to use this approach as well. The only thing we need to do is figure out what functions to call.
It ends up there are 2 basic functions: LoadLibrary and GetProcAddress.
LoadLibrary
LoadLibrary returns a handle the module (a pointer to a structure).After you are done with you library you can clean up by calling FreeLibrary passing it the handle that was returned from LoadLibrary.
GetProcAddress
GetProcAddress finds a function defined in a module and returns a pointer to that function. A function pointer allows you to call a method in a dynamic way. It is functionally equivalent to a delegate in managed code.In C e.g.:
Put it All Together
Now we have a basic algorithm to register a DLL:- LoadLibrary to get a handle to the library
- GetProcAddress to get a function pointer to the proper function to register the DLL
- Call the function returned from GetProcAddress
- Cleanup
Mix that in with some error checking code and I got the following:
Note: The requirement I was dealing with was a build script so I wanted to register the unmanaged DLL, use it, and then unregister it so the computer would be in its previous state. If you want to leave the DLL registered, such as for an install program, you would need to modify the above example.
To call this code you just need to pass it a path to the dll that needs to be registered.
Resources: Check out pinvoke.net for a lot of good documentation and example of how to call native methods from managed code.