Calling a C# function from a C++ DLL

后端 未结 1 1976

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

相关标签:
1条回答
  • 2021-01-26 11:46

    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

    • http://tigerang.blogspot.com/2008/09/reverse-pinvoke.html
    • http://blogs.msdn.com/b/thottams/archive/2007/06/02/pinvoke-reverse-pinvoke-and-stdcall-cdecl.aspx

    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

    0 讨论(0)
提交回复
热议问题