Search code examples
c#marshallingcom-interopunmanaged

Marshaling unmanaged dll in C# through ComInterop without registering DLL


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.


Solution

  • I have wanted to do the same thing myself. As Jim mentions it is possible to get the address of DllRegisterServer and 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 DllGetClassObject function. 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:

    1. Load the library (DLL) that houses the desired COM object
    2. Locate the DllGetClassObject function
    3. Call DllGetClassObject, passing it the CLSID of the desired COM object, this will return an IClassFactory instance.
    4. Call the CreateInstance method on the class factory to get an instance of the COM object.
    5. Cast the returned object to the interface you wish to use.

    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:

    // Load the library. You dispose this after you are finished with
    // all of your COM objects. 
    var library = new LibraryModule();
    library.Load("mylibrary.dll"); or whatever your dll is called
    
    var clsid = new Guid("75E81043-CAD5-11D3-800D-00105A5E2FA0");
    
    var myObject = (MyInterface)ComHelper.CreateInstance(library, clsid);
    

    Just note that if you dispose of the LibraryModule object, 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.