0

I have a DLL written in C++, and I want to pass a C# function to a function in the C++ DLL and call the C# function from C++. How would I do this? Thanks in advance, and please keep your answers simple. I don't think I'll need to post a code sample for this one, but if you need one please ask.

1
  • What does this have to do with Lua? You're just asking about calling C# from C++ code. Commented Mar 4, 2012 at 5:11

1 Answer 1

4

There are generally two ways to accomplish this: COM Interop and Reverse PInvoke. If you are passing around a lot of objects with behaviors then COM interop is likely you're best bet. Here though it sounds like you're just trying to pass a single function around. If so then Reverse PInvoke is what you want.

Here is a nice tutorial on the subject

The basic process though is fairly similar to normal PInvoke.

  • Define a native entry point which takes at least one function pointer
  • Define the native pointer as a delegate in managed code
  • Pass the delegate down through the entry point

It can now be invoked as a normal function pointer in native code

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

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.