c# - 如何在 C# 包装器中调用 C++ 非托管代码?
问题描述
我有 C# 包装器项目,它使用 C++ 非托管 dll 代码并借助[DllImport]
属性和静态外部方法。这是 MSDN 描述的首选方法。
问题是我们只能有一个 c++ 运行器,这意味着我们不能并行运行任务。
如何在不使用dllimport + static
extern 方法的情况下调用非托管 C++,以便当我们实例化其中一个 c# 对象时,它会实例化一个新的 c++ dll 对象
这就是我们现在调用 C++ 代码的方式。
public class FMU : IDisposable
{
[DllImport("FMU.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
private static extern IntPtr Load(string fmuFileName, bool isCoSimulation, AddMessageDelegate messageDelegate, int arrayLocation);
[DllImport("FMU.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr Instantiate(IntPtr fmu, bool isCoSimulation, bool loggingOn, int nCategories, string[] categories);
[DllImport("kernel32", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr hModule);
解决方案
推荐阅读
- spring-boot - 在春季启动时无法转到特定端点,它重定向到登录,
- java - 如何根据当前屏幕或状态启用和禁用代码行?
- c++ - 检查初始化列表中的参数是否正确?
- .net-core - 从 BackgroundService 启动服务总线客户端
- python - 在 Go 中读取图像文件与在 Python 中读取图像文件
- python - 根据熊猫数据框中的列值获取更改日期
- laravel - 阿拉伯字体在 barryvdh/laravel-snappy 中以方形符号显示
- python - 具有抽象类的并行处理
- google-cloud-platform - Google Cloud Console - SQL - 无法加载错误
- javascript - mini-css-extract-plugin 生成两个额外的 JS 文件