从C ++ DLL调用C#函数 [英] Calling a C# function from a C++ DLL

查看:168
本文介绍了从C ++ DLL调用C#函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的DLL,我想将C#函数传递给C ++ DLL中的函数,并从C ++调用C#函数。我该怎么做?提前感谢,请保持您的答案简单。我不认为我需要发布一个代码示例为这一个,但如果你需要一个请问。

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.

推荐答案

通常有两种方法来实现这一点:COM Interop和Reverse PInvoke。如果你传递很多对象与行为,然后COM互操作可能是你最好的赌。这里虽然听起来你只是试图传递一个单一的函数。如果是这样,反向PInvoke是你想要的。

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.

这是一个很好的教程。

  • 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

基本过程与正常的PInvoke非常相似。

The basic process though is fairly similar to normal PInvoke.


  • 定义至少包含一个函数指针的本地入口点

  • 将本机指针定义为托管代码中的代理

  • 将委托通过入口点

作为本地代码

这篇关于从C ++ DLL调用C#函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆