导入结构参数
本文关键字:参数 结构 导入 | 更新日期: 2023-09-27 18:13:22
我有3个c++结构体和两个方法,我想通过c#使用它们。以下是我的c#代码以及对c++代码的一些注释。
[StructLayout(LayoutKind.Sequential)]
public unsafe struct smat
{
public long rows;
public long cols;
public long vals;
public long* pointr;
public long* rowind;
public double* value;
};
[StructLayout(LayoutKind.Sequential)]
public unsafe struct dmat {
long rows;
long cols;
double **value;
};
[StructLayout(LayoutKind.Sequential)]
public unsafe struct svdrec {
int d;
dmat Ut; // it was dmat* in C++
double *S;
dmat Vt; // it was dmat* in C++
};
[DllImport(@"file.dll", EntryPoint = "svdNewSMat", CallingConvention = CallingConvention.Cdecl)]
public static extern smat svdNewSMat(int rows, int cols, int vals); // it was smat* in C++
[DllImport(@"file.dll", EntryPoint = "svdLAS2", CallingConvention = CallingConvention.Cdecl)]
public static extern svdrec svdLAS2(smat a, long dimensions, long iterations, double[] las2end, double kappa); // it was smat* and svdrec* in C++
编辑:下面是c++头文件
typedef struct smat *SMat;
typedef struct dmat *DMat;
typedef struct svdrec *SVDRec;
/* Harwell-Boeing sparse matrix. */
struct smat {
long rows;
long cols;
long vals; /* Total non-zero entries. */
long *pointr; /* For each col (plus 1), index of first non-zero entry. */
long *rowind; /* For each nz entry, the row index. */
double *value; /* For each nz entry, the value. */
};
/* Row-major dense matrix. Rows are consecutive vectors. */
struct dmat {
long rows;
long cols;
double **value; /* Accessed by [row][col]. Free value[0] and value to free.*/
};
struct svdrec {
int d; /* Dimensionality (rank) */
DMat Ut; /* Transpose of left singular vectors. (d by m)
The vectors are the rows of Ut. */
double *S; /* Array of singular values. (length d) */
DMat Vt; /* Transpose of right singular vectors. (d by n)
The vectors are the rows of Vt. */
};
extern DMat svdNewDMat(int rows, int cols);
extern SVDRec svdLAS2(SMat A, long dimensions, long iterations, double end[2],
double kappa);
我没有给出完整的代码,因为它包含很多库。下面是我执行的测试:
var a = svdNewSMat(3, 6, 3);
var m = new double[] {1, 2};
var r = svdLAS2(a, 1, 0, m, 1e-6); // I get the Exception here
第二行给我试图读取或写入受保护的内存错误。什么好主意吗?
您不能简单地将c#端的dmat*替换为dmat。指针不会自动封送为引用
请参阅http://msdn.microsoft.com/en-us/library/0szztey7(v=vs.80).aspx,了解如何正确编组嵌入指针。
如果是smart *和dmat*,则使用IntPtr代替smad/dmat
如果您正在使用DLL中的指针,则可以使用IntPtr。请记住,在此工作期间将发生大量的装箱/拆箱操作。
我不相信c++模块可以在c#结构体上执行操作。
在函数声明中,等价于指针到指针的pinvoke通常是ref IntPtr
。在结构声明中(正如其他人指出的),它应该只是IntPtr
。请记住,在函数调用中,事情往往比让你的结构声明正确更复杂(例如,调用约定可能需要修改)。