0

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?

Guapo
  • 3,446
  • 9
  • 36
  • 63
  • @DavidHeffernan so you're saying DLL Exporter Viewer display all the 3 functions? – Guapo Jun 06 '15 at 10:57
  • I'm saying that unmanaged exports works fine for me. So you are doing something different. What steps do we take to reproduce? – David Heffernan Jun 06 '15 at 10:58
  • His name is Robert. I worry about the lack of attention to detail that this shows. Perhaps you did not follow the instructions carefully. – David Heffernan Jun 06 '15 at 11:15

1 Answers1

2

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I've tried both x86 and x64 and the Viewer still doesn't list the functions for me. – Guapo Jun 06 '15 at 11:05
  • Works fine here. You aren't looking at the right DLL. Or something like that. Step back and take a slower look at what you are doing. Make sure that the DLL you look at has a modified time that indicates you just built it. Perhaps you are looking in the wrong folder at the AnyCPU DLL you built to begin with. Also, not posting the real code is a disappointment. – David Heffernan Jun 06 '15 at 11:06
  • Build > Clean Solution then Build > Build Solution and certified myself I was in the right DLL on the Viewer and nothing :/ about the Interop indeed I missed it on the copy paste. – Guapo Jun 06 '15 at 11:09
  • You are doing something wrong. I bet you are looking at the wrong file. Perhaps you didn't make a class library? It really does work. I cannot tell you what you did wrong because you did not provide steps to reproduce. – David Heffernan Jun 06 '15 at 11:13
  • Using dependency walker I indeed see those but also get this message `Warning: At least one delay-load dependency module was not found. Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.` And yes its a Class Library! – Guapo Jun 06 '15 at 11:18
  • Gieseke's library has a *lot* of failure modes. A simple one that's completely undiagnosable is installing it with Nuget but forgetting to run VS elevated first. That prevents the Nuget package from installing properly and the post-build patching of the assembly simply doesn't run. No exports, no error message. I'll keep recommending doing it [this way](http://stackoverflow.com/a/17131801/17034). – Hans Passant Jun 06 '15 at 11:20
  • @Hans Nah, no need for elevation. – David Heffernan Jun 06 '15 at 11:21
  • @Guapo Those are false positives. If you see the exports in dependency viewer you are good to go. – David Heffernan Jun 06 '15 at 11:21
  • @HansPassant thanks a lot running VS in elevated mode to install it worked, if u would please put that as an answer I would gladly accept it. – Guapo Jun 06 '15 at 11:42
  • @DavidHeffernan well that was the case for me, thanks a lot for helping me debug it and in the end it was something as silly as it not being installed with elevated privileges :/ – Guapo Jun 06 '15 at 11:52
  • Your comment showing success appeared before Hans' comment! – David Heffernan Jun 06 '15 at 11:53
  • In other words, lack of elevation clearly was not the cause of your problems. Because you had produce a DLL with the exported functions before you reinstalled elevated. – David Heffernan Jun 06 '15 at 12:09
  • @DavidHeffernan yet the DLL never worked *as in the application was never able to use the exported functions*, however after I: 1) uninstalled Unmanaged Exports, 2) closed VS, 3) run it again as admin, 4) installed Unmanaged Exports with elevated privileges, 5) rebuild project. it worked. – Guapo Jun 07 '15 at 07:02
  • Like I said, you built the dll with the exports before you did that. You've been trying lots of different things and have identified the wrong one as the cause for success. – David Heffernan Jun 07 '15 at 07:06
  • Well then why the DLL I built with the 3 exports was never loaded until I rebuilt it all as described above? Well I guess you're not reading the 5 steps I described in my previous comment where it says `3) run it again as admin`. – Guapo Jun 07 '15 at 07:07
  • Because there was something else wrong that you've not yet identified. It doesn't seem as though you really want to get to the bottom of this. – David Heffernan Jun 07 '15 at 07:08
  • @DavidHeffernan bottom of what? From the begin you tough I was doing everything wrong and been retaliating me all along, now that some one found the right answer to it you just can't seem to accept it and still retaliating me. When I first checked it with dependency walker as you suggested indeed it was all exported, however the application that read the DLL still couldn't use the exported functions. I had tried uninstalling and reinstalling Unmanaged Exports at that time without privileges to make sure I was doing it right and yet it didn't work. After I tried Hans suggestion ***it worked.*** – Guapo Jun 09 '15 at 09:00
  • Your question is unrelated to any application that calls the DLL. Your question is about exporting functions. You have not got to the bottom of this at all. You have not correctly identified the cause of your problems. You don't seem to want to gain understanding. If you are happy, that's fine. – David Heffernan Jun 09 '15 at 09:05
  • Many years later I'm getting the same problem. Elevated permissions didn't solve it. Steps in link from Hans out of date :-( – AriesConnolly Jun 25 '23 at 23:06