c# - 导入 C++ dll 时出现问题

标签 c# c++ dllimport

我正在尝试从我程序中的 C# 代码访问此 C++ 函数

Tridiagonal3 (float** mat, float* diag, float* subd)
{
   float a = mat[0][0], b = mat[0][1], c = mat[0][2],
                        d = mat[1][1], e = mat[1][2],
                                       f = mat[2][2];

}

调用如下图

tred2(tensor, eigenValues, eigenVectors);

其中张量是float[,],特征值和特征向量是float[]数组。

当我尝试这样做时出现异常

Access violation reading location 0x3f5dce99

当我尝试访问

float a = mat[0][0]

会发生什么?

最佳答案

Tridiagonal3 (float** mat, float* diag, float* subd)

mat是双指针类型(pointer to pointer)。 在 C# 中,float[,] 不是双指针。它只是访问多维数组的语法糖,就像您使用 mat[x + y * width] 而不是 mat[y][x] 一样;

换句话说,您正在将 float* 传递给您的 C++ 应用程序,而不是 float**

您应该更改使用mat 的方式,以使用手动 偏移访问元素,例如mat[y + 2 * x]

关于c# - 导入 C++ dll 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6033783/

相关文章:

c# - 如何从谷歌翻译下载文本到语音数据?

c++ - 一个处理 char* 和 wchar_t* 的函数

c++ - easypng替代/ super 简单的数据可视化库(c++)

c# - 根据 x64/x86 更改 C# DllImport 目标代码

c++ - 使用 dllimport 代替 dllexport

c# - 如何建立Nest SearchRequest对象并在查询中查看原始JSON?

c# - MySQL数据读取器不读取

c++ - 为什么/何时不需要 __declspec( dllimport )?

c# - 使用任务的意外线程中止异常。为什么?

c++ - 如何在 C++ 代码上使用 Tcl-C API