问题描述
我开发了一个小型C ++应用程序,它使用本文中的指导方针调用托管的.NET C#COM对象:
[]
现在我想在我的.NET应用程序中调用的C ++应用程序中建立一个回调函数。我想在.NET端创建一个函数 - 从C ++应用程序调用的RegisterCppCallback。并将某种函数指针作为参数。
我的想法是在.NET端应该看起来像这样:
public delegate void CppCallbackFn();
CppCallbackFn myCallbackFunc;
public void RegisterCppCallback( IntPtr pfn)
{
myCallbackFunc =(CppCallbackFn)(Marshal.GetDelegateForFunctionPointer(pfn, typeof (CppCallbackFn))) as CppCallbackFn;
myCallbackFunc(); // 测试
}
和CPP方面:
typedef int(__ stdcall * CallbackFunc)();
CallbackFunc * mInstance;
int CallbackHandler()
{
// some代码
返回 0 ;
}
// 在.NET中调用RegisterCppCallback函数
mInstance = new CallbackFunc((CallbackFunc)CallbackHandler);
pDotNetCOMPtr-> RegisterCppCallback(( long )mInstance);
我的问题是没有调用CPP中的回调函数。
我做错了什么?任何帮助都非常感谢
I've developed a small C++ application that calls a managed .NET C# COM object using the guide lines in this article:
Calling Managed .NET C# COM Objects from Unmanaged C++ Code[^]
Now I would like to establish a callback function in my C++ application that is called from my .NET application. I want to create a function on the .NET side - RegisterCppCallback that is called from the C++ app. and takes some kind of function pointer as argument.
My idea is that is should look something like this on the .NET side:
public delegate void CppCallbackFn(); CppCallbackFn myCallbackFunc; public void RegisterCppCallback(IntPtr pfn) { myCallbackFunc = (CppCallbackFn) (Marshal.GetDelegateForFunctionPointer(pfn, typeof(CppCallbackFn))) as CppCallbackFn; myCallbackFunc(); // test it }
and on the CPP side:
typedef int (__stdcall* CallbackFunc)();
CallbackFunc * mInstance;
int CallbackHandler() { // some code return 0; } // call the RegisterCppCallback function in .NET mInstance = new CallbackFunc((CallbackFunc) CallbackHandler); pDotNetCOMPtr->RegisterCppCallback((long) mInstance);
My problem is that the callback function in CPP isn't called.
What am I doing wrong ? Any help is highly appreciated
这篇关于从C#回调到C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!