pass unmanaged C++ method to C# dll for callback

一世执手 提交于 2019-12-10 10:43:49

问题


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!


回答1:


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);


来源:https://stackoverflow.com/questions/14325802/pass-unmanaged-c-method-to-c-sharp-dll-for-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!