Question

I have a C# apllication having add-ons capability. An add-on/plug-in that implements a specific interface can be linked/loaded at runtime to the main C# application.

Now to my question: how can I develop an unmanaged cpp dll that implements the C# interface and be loaded runtime to the C# app?

Thanks, Cabbi

Was it helpful?

Solution

You can implement interface using p/inkove, for example:

public interface IDirectory
{
    bool IsDirectoryEmplty(string path);
}

public class Directory : IDirectory
{
    [DllImport("Shlwapi.dll", EntryPoint = "PathIsDirectoryEmpty")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsDirectoryEmplty([MarshalAs(UnmanagedType.LPStr)]string directory);

    bool IInterface.IsDirectoryEmplty(string path)
    {
        return IsDirectoryEmplty(path);
    }
}

OTHER TIPS

With common language support on, you can write managed dll with full native c++ capability mixed in.

And you can load unmanaged dll via PInvoke in .Net, but I'd rather not recommend that way.

Strictly speaking, you cannot have an unmanaged DLL that implements a C# (managed) interface.

However, you can create a mixed mode assembly, that might be what you want. This is a very common way to provide a native C++ (unmanaged) implementation with a .NET wrapper making it an assembly that looks managed to a C# (or other .NET language) consumer.

In addition, you can use PInvoke to consume a pure native DLL from a NET assembly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top