Is it possible to call a c(++) static function pointer (not a delegate) like this
是否可以像这样调用c(++)静态函数指针(不是委托)
typedef int (*MyCppFunc)(void* SomeObject);
from c#?
void CallFromCSharp(MyCppFunc funcptr, IntPtr param)
{
funcptr(param);
}
I need to be able to callback from c# into some old c++ classes. C++ is managed, but the classes are not ref classes (yet).
我需要能够从c#回调到一些旧的c ++类。 C ++是受管理的,但类不是ref类(尚未)。
So far I got no idea how to call a c++ function pointer from c#, is it possible?
到目前为止,我不知道如何从c#调用c ++函数指针,这可能吗?
2 个解决方案
#1
17
dtb is right. Here a more detailed example for Marshal.GetDelegateForFunctionPointer. It should work for you.
dtb是对的。这里是Marshal.GetDelegateForFunctionPointer的更详细示例。它应该适合你。
In C++:
static int __stdcall SomeFunction(void* someObject, void* someParam)
{
CSomeClass* o = (CSomeClass*)someObject;
return o->MemberFunction(someParam);
}
int main()
{
CSomeClass o;
void* p = 0;
CSharp::Function(System::IntPtr(SomeFunction), System::IntPtr(&o), System::IntPtr(p));
}
In C#:
public class CSharp
{
delegate int CFuncDelegate(IntPtr Obj, IntPtr Arg);
public static void Function(IntPtr CFunc, IntPtr Obj, IntPtr Arg)
{
CFuncDelegate func = (CFuncDelegate)Marshal.GetDelegateForFunctionPointer(CFunc, typeof(CFuncDelegate));
int rc = func(Obj, Arg);
}
}
#2
2
Have a look at the Marshal.GetDelegateForFunctionPointer method.
看看Marshal.GetDelegateForFunctionPointer方法。
delegate void MyCppFunc(IntPtr someObject);
MyCppFunc csharpfuncptr =
(MyCppFunc)Marshal.GetDelegateForFunctionPointer(funcptr, typeof(MyCppFunc));
csharpfuncptr(param);
I don't know if this really works with your C++ method, though, as the MSDN documentation states:
但是,我不知道这是否真的适用于您的C ++方法,因为MSDN文档指出:
You cannot use this method with function pointers obtained through C++
您不能将此方法与通过C ++获得的函数指针一起使用
#1
17
dtb is right. Here a more detailed example for Marshal.GetDelegateForFunctionPointer. It should work for you.
dtb是对的。这里是Marshal.GetDelegateForFunctionPointer的更详细示例。它应该适合你。
In C++:
static int __stdcall SomeFunction(void* someObject, void* someParam)
{
CSomeClass* o = (CSomeClass*)someObject;
return o->MemberFunction(someParam);
}
int main()
{
CSomeClass o;
void* p = 0;
CSharp::Function(System::IntPtr(SomeFunction), System::IntPtr(&o), System::IntPtr(p));
}
In C#:
public class CSharp
{
delegate int CFuncDelegate(IntPtr Obj, IntPtr Arg);
public static void Function(IntPtr CFunc, IntPtr Obj, IntPtr Arg)
{
CFuncDelegate func = (CFuncDelegate)Marshal.GetDelegateForFunctionPointer(CFunc, typeof(CFuncDelegate));
int rc = func(Obj, Arg);
}
}
#2
2
Have a look at the Marshal.GetDelegateForFunctionPointer method.
看看Marshal.GetDelegateForFunctionPointer方法。
delegate void MyCppFunc(IntPtr someObject);
MyCppFunc csharpfuncptr =
(MyCppFunc)Marshal.GetDelegateForFunctionPointer(funcptr, typeof(MyCppFunc));
csharpfuncptr(param);
I don't know if this really works with your C++ method, though, as the MSDN documentation states:
但是,我不知道这是否真的适用于您的C ++方法,因为MSDN文档指出:
You cannot use this method with function pointers obtained through C++
您不能将此方法与通过C ++获得的函数指针一起使用