[英]Load a C-Dll in C# with struct array as parameter
I try to import functions from my C-Dll. 我尝试从C-Dll导入函数。 A function has a struct array as a parameter. 函数具有结构数组作为参数。 The struct will be filled in the function. 该结构将被填充到函数中。
struct test
{
int test1;
int test2;
};
void FillStruct( struct test stTest[], int size)
{
if(size == 2){
stTest[0].test1 = 5;
stTest[0].test2 = 5;
stTest[1].test1 = 2;
stTest[1].test2 = 2;
}
}
The method FillStruct
should be used in C#. 方法FillStruct
应该在C#中使用。
I think I have to create the struct in C#. 我想我必须在C#中创建该结构。 Must I marshal the struct if I use memcpy
in the Fillstruct
? 如果在Fillstruct
使用memcpy
,是否必须Fillstruct
该结构?
struct Test
{
public int test1;
public int test2;
}
[DllImport("mydll", CallingConvention = Cdecl)]
public static extern void FillStruct( Test[] stTest, int size);
[...]
var test = new Test[n];
FillStruct(test, test.Length);
I don't see the problem here. 我在这里看不到问题。 It does not matter what you do with the memory in your c code: as long as you don't cause buffer overflows, you can read, write an copy all you want. 使用c代码中的内存做什么都没有关系:只要不引起缓冲区溢出,就可以读取,编写所有所需的副本。 c# arrays are just the type and length of the array, followed by the data. c#数组只是数组的类型和长度,其后是数据。 When you use p/invoke with simple structs, a pointer to the first element in the original array will be passed to your c code. 当您将p / invoke与简单结构一起使用时,指向原始数组中第一个元素的指针将传递给您的c代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.