c# - 从 C# 包装 C++ DLL 的问题

标签 c# c++ dll interop word-wrap

我正在使用 System.Runtime.InteropServices 从我的 C# 应用程序调用几个用 C++ 编写的函数。我只是遇到了返回数组的特定函数的问题。

我已经看到我的函数不应该返回任何东西,并且指向“返回变量”的指针应该是入口。但我没有设法正确地做到这一点。

例如,如果我在 C++ 中有一个函数

void func(double *y, double *x){...}

操作数组 x 并返回数组 y。

我在做:

-在我的 .h 中:

extern "C" __declspec(dllexport) void func(double *y,double *x);

-在我的 .cpp 中:

__declspec(dllexport) void func(double *y,double *x){...}

-在我的 C# 代码中:

static class AnyClass
  {
    [DllImport(dllPath)]
    public extern static void func(out double[] y, double[] x);

    int otherfunc
    {
      double[] x = new double[5];
      double[] y = new double[5];

      ...

      func(out y, x); 
    }
  }

但它给了我一个 System.EntryPointNotFoundException。

有什么线索吗?

最佳答案

EntryPointNotFoundException 意味着在您的 DLL 中没有找到所谓的“函数”。

在您的 .h 文件中,您将其称为“func”。但是在您的 .cpp 文件中,您称它为“函数”。因为你的 .h 文件是唯一声明 extern "C" 的地方,实际上发生的是你的 DLL c++-style-name-mangled 导出的函数,而不是普通的-c-风格。因此,当 C# 寻找纯 C 风格的“函数”时,它找不到。

关于c# - 从 C# 包装 C++ DLL 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8660403/

相关文章:

c# - 如何使用 EWS 获取电子邮件正文、收据、发件人和抄送信息?

c# - 使用 DocX dll 将行添加到 word 文件(doc 文件)中的现有表

c# - Newtonsoft 11.0.0.0 无法在 Azure Function App 2.0 上加载

c# - 使用 Enumerable.Empty<T>() 而不是 new List<T>() 来初始化 IEnumerable<T> 是否更好?

c++ - 如何声明通过引用传递多维数组的函数

c++ - 将文本文件附加到 char 数组,接收垃圾输出

c++ - C++11和C++14如何实现动态函数调用?

c# - 如何安装 Visual Studio dll 文件?

c++ - 无法运行 DLL 链接的 C++ exe。 "This program cannot be run in DOS mode."错误

c++ - 一个定义规则是否适用于在运行时动态加载共享库?