如何从C ++向C#获取unsigned int数组?

时间:2019-11-29 14:00:27

标签: c# c++ marshalling

我是C#的新手,并尝试找到等效的C#代码以从dll文件填充int数组。

    // in C++
unsigned int MyFunc(unsigned int* ids, unsigned int* size);


//usage of the function in C++


{

    //...
    unsigned int status = 0;
    unsigned int myIds[2000];
    unsigned int size = sizeof(myIds) / sizeof(myIds[0]);
    //invoke the function to fill myIds
    status = MyFunc(myIds, &size);

    //...
}
//in C#

[DllImport("MyFunc")]
private static extern uint MyFunc(ref uint ids, ref uint size);


//usage of the function in C#

{

    //...
    uint[] myIds = new uint[2000];
    uint size = (uint)myIds.Length;
    uint status = MyFunc(ref myIds, ref size);//error compilation .. cannot convert from ref uint[] to ref uint 
//...
}

如何使myIds填充在C#中?

1 个答案:

答案 0 :(得分:0)

您将ref传递给[DllImport("MyFunc")] private static extern uint MyFunc(ref uint ids, ref uint size);

中的单个无符号整数

您应该将ref传递给无符号整数数组,而不仅仅是将一个无符号整数作为参数接受。该错误非常简单。

执行完此操作后,您的代码将如下所示: [DllImport("MyFunc")] private static extern uint MyFunc(ref uint[] ids, ref uint size);