Search code examples
c#c++dllexport

Exported functions from C# DLL not working


I have to export 3 basic methods from my DLL in C#, so it becomes accessible in C++:

  • OnPluginStart
  • OnPluginStop
  • PluginUpdate

So I found Unmanaged Exports a nice C# library that makes that easier.

So I went ahead with a sample code to test:

using System;
using System.IO;
using RGiesecke.DllExport;

namespace Plugins
{
    public class Plugins
    {
        [DllExport("OnPluginStart", CallingConvention = CallingConvention.StdCall)]
        public static void OnPluginStart()
        {
            using (var file = new StreamWriter(@"pluginLog.txt", true))
            {
                file.WriteLine("OnPluginStart");
            }
        }

        [DllExport("OnPluginStop", CallingConvention = CallingConvention.StdCall)]
        public static void OnPluginStop()
        {
            using (var file = new StreamWriter(@"pluginLog.txt", true))
            {
                file.WriteLine("OnPluginStop");
            }
        }

        [DllExport("PluginUpdate", CallingConvention = CallingConvention.StdCall)]
        public static void PluginUpdate(float dt)
        {
            using (var file = new StreamWriter(@"pluginLog.txt", true))
            {
                file.WriteLine("PluginUpdate");
            }
        }
    }
}

However, when I compile my DLL and use DLL Exporter Viewer it doesn't list any of the exported functions and the application the DLL is loaded into also never runs my plugin.

What am I doing wrong here which makes my functions not being exported at all?


Solution

  • Your code works fine, apart from the fact that the code you posted does not compile. You omitted the using System.Runtime.InteropServices line. Dependency Walker for an x86 class library build of your (fixed) code says this:

    enter image description here

    The most obvious cause for the problem could be the following from the NuGet page for the library:

    You have to set your platform target to either x86, ia64 or x64. AnyCPU assemblies cannot export functions.