从c#中调用c++函数指针可以像这样调用ac(++)静态函数指针(不是delegate)typedefint(*MyCppFunc)(void*SomeObject);来自C#?voidCallFromCSharp(MyCppFuncfuncptr,IntPtrparam){funcptr(param);我需要能够从c#回调到一些旧的c++类。C++是托管的,但类不是ref类(目前)。到目前为止,我不知道如何从C#调用C++函数指针,这可能吗?dtb是对的。下面是一个更详细的Marshal.GetDelegateForFunctionPointer示例。它应该适合你。在C++中:staticint__stdcallSomeFunction(void*someObject,void*someParam){CSomeClass*o=(CSomeClass*)someObject;返回o->MemberFunction(someParam);}intmain(){CSomeClasso;无效*p=0;CSharp::Function(System::IntPtr(SomeFunction),System::IntPtr(&o),System::IntPtr(p));}在C#中:publicclassCSharp{delegateintCFuncDelegate(IntPtrObj,IntPtrArg);publicstaticvoidFunction(IntPtrCFunc,IntPtrObj,IntPtrArg){CFuncDelegatefunc=(CFuncDelegate)Marshal.GetDelegateForFunctionPointer(CFunc,typeof(CFuncDelegate));intrc=func(Obj,Arg);}}看看Marshal.GetDelegateForFunctionPointer方法。委托voidMyCppFunc(IntPtrsomeObject);MyCppFunccsharpfuncptr=(MyCppFunc)Marshal.GetDelegateForFunctionPointer(funcptr,typeof(MyCppFunc));csharpfuncptr(参数);但是,我不知道这是否真的适用于您的C++方法,因为MSDN文档指出:YoucannotusethismethodwithfunctionpointersobtainedthroughC++更多关于C#的学习教程,希望大家多多关注—这个文章收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
