3

我在 c++ 中有一个 dll,它返回列表,我想在我的 c# 应用程序中使用它作为列表

[DllImport("TaskLib.dll", SetLastError = true, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern List<int> GetProcessesID();

public static List<int> GetID()
{
    List<int> processes = GetProcessesID();//It is impossible to pack a "return value": The basic types can not be packed
    //...
}
4

2 回答 2

3

根据贾里德·帕:

在任何互操作方案中通常不支持泛型。如果您尝试编组泛型类型或值,PInvoke 和 COM 互操作都将失败。因此,我希望 Marshal.SizeOf 在这种情况下未经测试或不受支持,因为它是 Marshal 特定的功能。

请参阅: 编组 .NET 泛型类型

于 2012-04-23T22:50:02.543 回答
3

一种可能的情况

c++端

    struct ArrayStruct
    {
        int myarray[2048];
        int length;
    };

    extern "C" __declspec(dllexport) void GetArray(ArrayStruct* a)
    {
        a->length = 10;
        for(int i=0; i<a->length; i++)
            a->myarray[i] = i;
    }

时间:2019-04-10 标签:c#side

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct ArrayStruct
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
        public int[] myarray;
        public int length;
    }

    [DllImport("TaskLib.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void GetArray(ref ArrayStruct a);

    public void foo()
    {
        ArrayStruct a = new ArrayStruct();
        GetArray(ref a);
    }
于 2012-04-27T22:29:22.113 回答