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?

4
  • possible duplicate of Using a .net compiled dll inside native c++ Commented Jan 19, 2011 at 15:25
  • possible duplicate of How to export c# methods? Commented Jan 19, 2011 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. Commented Jan 19, 2011 at 15:27
  • @Filip: That's a different question. Commented Jan 19, 2011 at 15:28

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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".
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.
1

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.