问题描述
我在C ++中创建了使用C#应用程序的COM服务器。现在我必须添加一个方法到我的COM对象,其中一个参数必须是函数指针回调函数。 C ++原函数看起来像这样:
I have created COM server in C++ that is used from C# application. Now I have to add one more method to my COM object and one of its parameter must be function pointer to callback function. C++ original function looks like this:
typedef int (__stdcall *tCallBackFunc)(int Param);
void MyFunc(void* CallBackFunc)
{
int Param;
//some code
((tCallBackFunc)(CallBackFunc))(Param); //call callback function
}
我需要添加MyFunc作为COM对象方法。 VS VC ++添加方法对话框提供一些预定义类型的COM方法参数像int *或bstr,我试图定义CallBackFunc参数作为int *类型,但我不能使其工作C#:
I need to add MyFunc as COM object method. VS VC++ "Add Method" dialog offers some predefined types for COM method parameter like "int*" or "bstr", I tried to define "CallBackFunc" parameter as "int*" type, but I was not able to make it work in C#:
public delegate int tCallBackFunc(int Param);
...
tCallBackFunc cb; //I will initialize it when it is compiled
MyCOMObject.MyFunc(cb); //cannot find any way to cast cb to "ref int"
看来我使用错误的方法,有没有一种快速的方法来声明函数指针作为C ++中COM对象方法的参数,然后在C#中使用它?
It seems I use wrong approach, is there a fast way to declare function pointer as parameter of COM object method in C++ and then use it in C#?
推荐答案
COM方式是使参数成为一个接口指针,并让接口声明回调函数。
The COM way is to make the parameter an interface pointer, and have the interface declare the callback function.
这篇关于函数指针作为COM方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!