如何将函数指针从 C# 传递到C++ Dll
How to pass function pointer from C# to a C++ Dll?
C++dll中定义的函数是:
static double (*Func1)(double);
EXTERN_C __declspec(dllexport) __stdcall double TestDelegate(double (*fun)(double))
{
Func1 = fun;
return Func1(25.0);
}
void My_Real_purpose()
{
SomeClass a;
a.SetFunction(Func1);//Define behaviour of a by C# in runtime
a.DoSomething();//Even I want it runs in another thread!
}
我尝试像这样用 C# 调用它:
class A
{
[DllImport("DllName.dll")]
public extern static double TestDelegate(IntPtr f);
public delegate double MyFuncDelegate(double x);
public static double MyFunc(double x)
{
return Math.Sqrt(x);
}
static MyFuncDelegate ff;
static GCHandle gch;
public static double Invoke()
{
ff = new MyFuncDelegate(MyFunc);
gch = GCHandle.Alloc(ff);
double c = TestDelegate(Marshal.GetFunctionPointerForDelegate(ff));//Error occurs this line
gch.Free();
return c;
}
}
编译时没有错误。但是当它运行时,VS2012显示"访问冲突异常"错误。
我搜索并尝试了很多方法,例如通过委托而不是 IntPtr,但结果都失败了。
那么,在包含函数指针的 dll 中使用 API 函数的正确方法是什么?或者如何实现"My_Real_purpose"功能?
委托使用 cdecl
调用约定。因此,在 C# 中,您将像这样声明委托:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate double CallbackDelegate(double x);
作为替代方案,您可以决定将C++中的函数指针声明为 __stdcall
,在这种情况下,您将删除 UnmanagedFunctionPointer
属性并依赖于CallingConvention.StdCall
的默认调用约定。
像这样实现它:
public static double MyFunc(double x)
{
return Math.Sqrt(x);
}
为了使非托管函数指针保持活动状态(防范 GC(,您需要在变量中保存委托的实例。
private static CallbackDelegate delegateInstance;
....
delegateInstance = MyFunc;
在这里的简单示例中,C++代码不使用 TestDelegate
之外的非托管函数指针,但在更复杂的示例中,您可以这样做,在这种情况下,您必须使非托管函数指针保持活动状态。
您导入的函数声明如下:
[DllImport("DllName.dll")]
public extern static double TestDelegate(CallbackDelegate f);
然后你可以像这样称呼它:
double retval = TestDelegate(delegateInstance);
在C++端,我会明确指定回调的调用约定,例如 __stdcall
(您尚未在代码中执行此操作,我认为默认值为 __cdecl
(:
// Include the calling convention (__stdcall) for the Callback
typedef double (__stdcall * Callback)(double);
// Just use "Callback" here, instead of repeating
// the above function prototype
extern "C" __declspec(dllexport) __stdcall double TestDelegate(Callback func)
{
return func(25.0);
}
// BTW: Can export also using .DEF file to avoid __stdcall name mangling
在 C# 端,您可以尝试如下操作:
public delegate double CallbackDelegate(double x);
// PInvoke declaration for the native DLL exported function
[DllImport("YourDLL.dll", CallingConvention = CallingConvention.StdCall)]
public static extern double TestDelegate(CallbackDelegate func);
private double MyFunctionCallback(double x)
{
// ... Implement your C# callback code ...
}
CallbackDelegate managedDelegate = new CallbackDelegate(MyFunctionCallback);
// Call into the native DLL, passing the managed callback
TestDelegate(managedDelegate);
相关文章:
- 挂起和取消挂起一个文件DLL
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 导入库可以跨dll版本工作吗
- 从C++dll访问C#中的一行主要参数
- 链接到自行创建的dll失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- C++:将外部库链接到dll库
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 不同的Visual Studio版本中缺少.dll
- 从DLL中删除类的实例
- 如何包装第三方DLL在R中使用
- 使用c#访问c++dll中带有char*参数的函数时发生AccessViolationException
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Python ctypes:不会按预期加载 dll
- 在 C++/CLI 中将 .NET 事件从一个 DLL 引发到另一个 DLL