如何将带有2维整数和双数组的结构从C#传递给C ++ DLL [英] How to pass struct with 2 dimension integer and double array from C# to C++ DLL

查看:75
本文介绍了如何将带有2维整数和双数组的结构从C#传递给C ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的vc ++ dll低于struct



struct Value

{

int RunInchMm;

int transform_order;

int rotation_ang;

int rotation_org [2] [2];

int mirror_xchg_code;

int absincmode;

double WCS [6] [4];

};



我使用这个结构作为我的vc ++ dll函数的参数,我在这里读取结构中的所有值。



我从c#application& amp中调用这个Dll及其函数。从C#应用程序传递值到结构,但我没有得到dll中数组的正确值(但对于整数我得到正确的值)。



我不知道知道为什么会这样吗?



我的C#结构如下 -



[StructLayout(LayoutKind) 。顺序)]

公共结构价值

{

[MarshalAs(UnmanagedType.I4)]

public int RunInchMm;

[MarshalAs(UnmanagedType.I4)]

public int transform_order;

[MarshalAs(UnmanagedType.I4)]

public int rotation_ang;

[MarshalAs(UnmanagedType.ByValArray)]

public int [,] rotation_org;

[MarshalAs(UnmanagedType。 I4)]

public int mirror_xchg_code;

[MarshalAs(UnmanagedType.I4)]

public int absincmode;

[MarshalAs(UnmanagedType.ByValArray)]

公共双[,] WCS;

}



我我在C#中使用d11 as-



[DllImport(Convert.dll)]

public static extern int ConvertFunc(int prognum, ref Value WincncVal,string ProjectName,int fromrunormdi);



//创建struct的对象



Value WincncVal = new Value();

WincncVal.rotation_org = new int [2,2];

WincncVal.WCS = new double [6,4];



WincncVal.RunInchMm = 5;



WincncVal.transform_order = 10;

WincncVal。 rotation_ang = 12;



WincncVal.rotation_org [0,0] = 2;

WincncVal.rotation_org [0,1] = 3;

WincncVal.mirror_xchg_code = 6;

WincncVal.absincmode = 4;



for(int i = 0 ;我< 6; i ++)

for(int j = 0; j< 4; j ++)

WincncVal.WCS [i,j] = 1.1;



ConvertFunc(9999,参考WincncVal,eWinCNC,0);



我google了很多但没有得到正确的解决方案。



请帮帮我。



谢谢....



我尝试了什么:



我尝试使用指针但没有成功。

Hi,

I am having vc++ dll having below struct

struct Value
{
int RunInchMm;
int transform_order;
int rotation_ang;
int rotation_org[2][2];
int mirror_xchg_code;
int absincmode;
double WCS [6][4];
};

I am using this structure as parameter to my function of vc++ dll where I am reading all values from structure.

I am calling this Dll and its function from c# application & passing values to structure from C# application, but i am not getting correct values for arrays in dll (but for integer i am getting correct values) .

I don't know why it is so?

My structure from C# is as below-

[StructLayout(LayoutKind.Sequential)]
public struct Value
{
[MarshalAs(UnmanagedType.I4)]
public int RunInchMm;
[MarshalAs(UnmanagedType.I4)]
public int transform_order;
[MarshalAs(UnmanagedType.I4)]
public int rotation_ang;
[MarshalAs(UnmanagedType.ByValArray)]
public int[,] rotation_org;
[MarshalAs(UnmanagedType.I4)]
public int mirror_xchg_code;
[MarshalAs(UnmanagedType.I4)]
public int absincmode;
[MarshalAs(UnmanagedType.ByValArray)]
public double[,] WCS;
}

I am using dll in C# as-

[DllImport("Convert.dll")]
public static extern int ConvertFunc(int prognum, ref Value WincncVal, string ProjectName, int fromrunormdi);

//Create object of struct

Value WincncVal = new Value();
WincncVal.rotation_org = new int[2, 2];
WincncVal.WCS =new double[6,4];

WincncVal.RunInchMm = 5;

WincncVal.transform_order = 10;
WincncVal.rotation_ang = 12;

WincncVal.rotation_org[0, 0] = 2;
WincncVal.rotation_org[0, 1] = 3;
WincncVal.mirror_xchg_code = 6;
WincncVal.absincmode = 4;

for (int i = 0; i < 6; i++)
for (int j = 0; j < 4; j++)
WincncVal.WCS[i, j] = 1.1;

ConvertFunc(9999, ref WincncVal, "eWinCNC", 0);

I googled a lot but not getting proper solution.

Please help me.

Thanks....

What I have tried:

I tried using pointers but no success.


推荐答案

多维数组很难在托管和非托管世界之间传递。



你的
Multi-dimensional arrays are hard to pass between managed and unmanaged worlds.

Your
int rotation_org[2][2]; 

实际上是三个对象:一个用于矩阵,一个用于每个列。虽然您的非托管C ++代码只能看到一个对象,但是相邻内存位置中的所有矩阵单元格都是如此,就像指定了一维数组一样。



最简单的方法传输矩阵是通过使用一维数组!!

in C# world is actually three objects: one for the matrix, and one for each of its columns. Whereas your unmanaged C++ code only sees one object, will all matrix cells in adjacent memory locations, as if a one-dimensional array was specified.

Far the easiest way to transport matrices is by using one-dimensional arrays!!


这篇关于如何将带有2维整数和双数组的结构从C#传递给C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆