如何封送列表<int>?
How do i marshal list<int>?
我在 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
//...
}
Per Jared Par:
通常,任何互操作方案都不支持泛型。如果尝试封送泛型类型或值,则 PInvoke 和 COM 互操作都将失败。因此,我希望Marshal.SizeOf在这种情况下未经测试或不受支持,因为它是Marshal特定的函数。
看:封送处理 .NET 泛型类型
可能的情况之一
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;
}
C# 端
[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);
}
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 没有显式声明的int[]中的foreach
- 在c++中访问int到类对象的映射时出错
- 为什么我无法更改"set<set>"循环中的值<int>
- EASTL矢量<向量<int>>连续的
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- 'structstd::对<int,int>'没有名为'push_back'