4

I have a C# app.exe and one C# my.dll. The my.dll .NET project links to a native C++ DLL (mynat.dll) (extern C DLL interface) and calling from C# into the C++ DLL works without problems. ( By using the [DllImport("mynat.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] attribute. )

What I need to add now is for the C# dll to provide some callback functions that the C++ code can call into. Ideally the mynat.dll C++ code would use LoadLibrary("my.dll") to load the C# dll and then use GetProcAddress to resolve a callback function it can then call. (Note that at the point the C++ code calls LoadLibrary the my.dll C# dll is already loaded into the process - this call would just be to get a handle to the dll.)

However, I don't know what the correct way is to export an "extern C DLL interface" from a .NET DLL

What do I need to do to achieve this?

Martin Ba
  • 37,187
  • 33
  • 183
  • 337
  • possible duplicate of [Using a .net compiled dll inside native c++](http://stackoverflow.com/questions/852048/using-a-net-compiled-dll-inside-native-c) – Filip Ekberg Jan 19 '11 at 15:25
  • possible duplicate of [How to export c# methods?](http://stackoverflow.com/questions/2082159/how-to-export-c-methods) – SLaks Jan 19 '11 at 15:26
  • At first I was going to say it's a duplicate, but because it's C++ and not Python, I think it can serve as a flag post, so consider not closing this. – user541686 Jan 19 '11 at 15:27
  • @Filip: That's a different question. – SLaks Jan 19 '11 at 15:28

2 Answers2

2

Contrary to popular belief, this is possible.
See here.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Didn't you just say this should be a close vote? It almost makes me want to -1, just because you contradicted yourself and discouraged my answer, then posted the exact same thing (your own answer) here. – user541686 Jan 19 '11 at 15:28
  • 1
    It's funny how we've been members for the exact same amount of time and you have 100k more rep. It's not fair ;) @Lambert, You answer should have been a "close - Possible duplicate". – Filip Ekberg Jan 19 '11 at 15:28
  • Note that this site you link to doesn't really explain anything - it just provides an executable+dll (I couldn't even find the sourcecode for it) that "does the magic". Not really a satisfying answer. – Martin Ba Jan 20 '11 at 08:24
0

While the link provided by SLaks to the Unmanaged Exportsutility might or might not work, we used a similar tool (from a different source) here and due to problems with signed executables have abandoned this approach.

We have concluded the following and will do this in the future:

The correct way to make .NET callbacks available to pure native modules is to write a C++/CLR project that does the upcalls to to .NET assembly and exports a native interface.

[ .NET ] -> ----------- -> [ C(++) ]  ... via DllImport Attribute

[ .NET ] <- [ C++/CLR ] <- [ C(++) ]  ... "default" .NET interface + "default" native interface
Martin Ba
  • 37,187
  • 33
  • 183
  • 337