将非托管C ++方法传递给C#dll以进行回调 [英] pass unmanaged C++ method to C# dll for callback

查看:181
本文介绍了将非托管C ++方法传递给C#dll以进行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.Net DLL,我已经注册,并能够从我的C#代码调用方法。
我基本上遵循了本教程: http://support.microsoft.com/kb/828736



现在我需要在c#中异步地做一些事情,所以我需要一些告诉c ++代码的方法。



我已经创建了一个这样的方法:

  public void Init (字符串服务器,IntPtr回调); 

我可以在c ++中看到:

  Init(BSTR server,long callback); 

当我调用它时,我还需要将一个变量传递回c ++代码。 >

基本上,我有一个事件在C#代码中被触发,我需要c ++代码来处理它,包括事件args。



我很高兴自己做阅读,但我根本找不到任何东西。我看到一些关于Windows事件的事情...这里
http://msdn.microsoft.com/en-us/library/windows/desktop/aa385771(v = vs.85).aspx



但我们没有使用任何窗口标题,我不认为,我不想添加所有这些东西,如果有一个更简单的方法这样做。



谢谢您阅读!

解决方案

好吧,比我想象的容易,但看起来像巫毒我...



基本上在c ++中添加

  void CALLBACK CppCallbackc()
{
std :: cout<< 测试;
}

然后在c#add

  public delegate void CppCallback(); 

,当您要启动它时,

  Marshal.GetDelegateForFunctionPointer(cppCallback,typeof(CppCallback))。DynamicInvoke(null); 


I have a .Net dll which I have registered and am able to call methods on from my C# code. I essentially followed this tutorial: http://support.microsoft.com/kb/828736

now I need to do something in c# asynchronously, so I need some way of telling the c++ code that I am done.

I have created a method like so:

public void Init(string server, IntPtr callback); 

which I can see in the c++ as:

Init(BSTR server, long callback);

I also need to pass a variable back to the c++ code when I make invoke it.

basically, I have an event that's getting fired in the C# code and I need the c++ code to handle it, including the event args.

I am glad to do the reading myself, but I am not able to find anything at all. I did see some stuff about windows events... here http://msdn.microsoft.com/en-us/library/windows/desktop/aa385771(v=vs.85).aspx

but we aren't using any windows headers I don't think and I didn't want to add all this stuff if there is a simpler way of doing this.

Thank you for reading!

解决方案

okay it's easier than I thought, but it seems like voodoo to me...

basically in the c++ add

void CALLBACK CppCallbackc()
{
    std::cout << "test";
}

then in the c# add

public delegate void CppCallback();

and when you want to fire it,

Marshal.GetDelegateForFunctionPointer(cppCallback, typeof(CppCallback)).DynamicInvoke(null);

这篇关于将非托管C ++方法传递给C#dll以进行回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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