I have an unmanaged DLL which I’m currently calling from C# using a COM Class Wrapper.
[ComImport(), Guid("75E81043-CAD5-11D3-800D-00105A5E2FA0")]
public class MyObject { }
[ComImport(), Guid("75E81042-CAD5-11D3-800D-00105A5E2FA0"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface MyInterface
{
string EncryptString([In, MarshalAs(UnmanagedType.BStr)] string bstrOrginal);
}
Then to call:
MyInterface obj = (MyInterface)new MyObject();
string crypt = obj.EncryptString("something");
This works, the return value is as I expect. However, it requires that the dll is registered with regsvr32.
I’m looking for a way to do this without the requirement of needing to regsvr32. Preferably, by just having a copy of the dll available. It is worth noting, I have the source for the unmanaged dll, and the ability to modify it if necessary.
A shove in the right direction would be greatly appreciated.
I have wanted to do the same thing myself. As Jim mentions it is possible to get the address of
DllRegisterServerand call it, but that still modifies your registry with the various entries. If you don’t want to do this (for example, you might not have the necessary privileges to write to the Registry), then there is another way.Any DLL that houses one or more in-process COM objects must expose the
DllGetClassObjectfunction. This function is used to acquire an instance of the COM class factory that is used to create a COM object. What you need to do is:DllGetClassObjectfunctionDllGetClassObject, passing it the CLSID of the desired COM object, this will return anIClassFactoryinstance.CreateInstancemethod on the class factory to get an instance of the COM object.Note that there be dragons with this approach — it is fairly low level. If you get anything wrong you will experience access violation exceptions or worse. (For instance, your interface declaration has to exactly match the COM interface).
I have included some sample code at gist which you might like to use if you want to go this way.
Using this code would look something like this:
Just note that if you dispose of the
LibraryModuleobject, then this will unload the DLL. Depending on your needs, you might just assign the value to a static field so that it exists for the lifetime of the program.