3

I am trying to create a c# dll with a few exportable functions. Then I want a C++/unmanaged program to load that .dll and call a particular exported function inside the dll.

I'am using Robert Giesecke's Unmanaged Exports. But it doesn't seem to work.

I ran the unmanaged program in a debugger and it successfully does "LoadLibrary()", but when it tries to "GetProcAddress(test_start)" the call fails and returns zero.

This is my c# code:

    using System.Runtime.InteropServices;
    using RGiesecke.DllExport;
    using etc...; 

    namespace test_dll
    {

        public class Class1
         {  

              [DllImport("kernel32.dll")]
              public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

              [DllExport("test_start", CallingConvention = CallingConvention.Cdecl)]
              public static void test_start()
              {
                    MessageBox.Show("It works","YES");
              }

         }
    }

The .dll builds fine and the CPU match (x86), but the unmanaged program can't get the export function address once the c# dll has been loaded.

Its pretty basic right now, but I'm just trying to get it to work. I'll need the imports later on.

Any help please, the documentation for the nuget package is quite thin. Thanks

5
  • And what does GetLastError return after the call to GetProcAddress fails? I assume that you're passing the correct module handle to GetProcAddress. Have you looked at the EXPORTS section of your generated dll to make sure that the spelling (including case) of test_start is what you expect? Jul 25, 2016 at 21:30
  • 3
    DllExport has always been a very finicky solution from what I heard. The better solution is use C++/cli to act as a bridge between the managed and unmanged code. You then expose unmanged functions that then forwards the calls in to the managed functions in C#. See this MSDN Blog post for a example of how it is done. Jul 25, 2016 at 21:30
  • In addition to @Scott's recommendation, another alternative is to build your library as a COM server, exporting your class as a COM object. It's a bit more heavy-weight, but provides a more universal solution (i.e. your COM server will work with a wide variety of client code scenarios). For debugging your current issue, you can use dumpbin.exe with the /exports option to see if and what the third-party DllExport and build action has exported for you. Jul 25, 2016 at 21:38
  • Use Dumpbin.exe /exports on your DLL to see exported names. It should be "_test_start" (leading underscore) to match Cdecl. Jul 25, 2016 at 21:43
  • What c++ development tool you use?
    – M.Hassan
    Jul 25, 2016 at 21:47

1 Answer 1

2

Hey so actually I was doing it perfectly fine.

Weirdly Robert Giesecke's Unmanaged Exports ignores the first export??

So any exports I created below that one worked and was recognised.

I checked with CFF explorer to see the valid exports, and all but the first one was there. So I just left a blank export on top.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.