有没有可能在没有for循环的情况下将本机结构数组封送到托管数组?
Is it possible to marshal native struct array to managed array with out a for loop
我使用的是本地DLL。我不确定,但我想我不能使用PInvoke decl's,因为它不导出任何函数,也没有清单。DLL提供了一个头文件,解释了如何使用它。头文件定义了无数的结构体,枚举和一个类,使用工厂方法来构造,通过Windows函数::GetProcAddress
(通过晦涩实现安全)来访问。这个类包含我想在托管代码中使用的函数。
我已经成功地将类包装在CLI ref类中,并且可以在其上调用简单的方法,也可以包装这些方法。
我正在经历将一些结构从头文件转换为托管结构的过程。例如,本机结构:
struct FooACL{
int action;
unsigned long from,to;
char comment[64];
int reserved[17];
};
变为托管结构体:
[StructLayout(LayoutKind::Sequential, CharSet = CharSet::Ansi)]
public value struct ManagedFooACL{
int action;
int from,to;
[MarshalAs(UnmanagedType::ByValTStr, SizeConst = 64)]
String^ comment;
[MarshalAs(UnmanagedType::ByValArray, SizeConst = 17)]
array<int>^ reserved;
};
据我所知,这应该使托管结构可损坏?以及遵循类似模式或嵌套结构级别的任何其他结构。只要指定了布局,并且没有使用MarshalAs装饰的blittable,那么整个结构是否可以blittable?
因此,我试图看看是否有方法使用Marshal::Copy
或Marshal::PtrToStructure
将FooACL*
数组转换为array<ManagedFooACL>^
。
我得到的FooACL*数组从一个函数调用;我自己不分配。
int total;
FooACL* foos = unamagedClass->GetFooACLS(&total);
total
是一个获取返回数组大小的in/out操作。
到目前为止我做了什么,工作是什么:
ManagedFooACL first = static_cast<ManagedFooACL>(Marshal::PtrToStructure(IntPtr(&foos [0]), ManagedFooACL::typeid));
我不明白的是为什么这不是:
array<ManagedFooACL>^ mfoos = gcnew array<ManagedFooACL>(total);
Marshal::PtrToStructure(IntPtr(&foos), mfoos);
抛出:
System.ArgumentException was unhandled
Message=The specified structure must be blittable or have layout information.
Parameter name: structure
Source=mscorlib
ParamName=structure
是否有一种方法来复制数组数据在一次调用或我真的需要做一个for循环?这些封送功能看起来有点傻。
在做了更多的研究之后,答案似乎是否定的。在不循环的情况下自动封送array
/struct
是不可能的。
我认为struct
封送处理与PtrToStructure
一起工作的主要原因是因为结构是静态的/预定义的。编译器知道如何布局内存。由于您获得的是动态大小array
,因此无法提前指定内存布局。所以你必须循环struct
s的动态数。
或者,如果你知道你将总是得到一个长度为X的数组,你可以定义你自己的托管struct
来保存一个元素,即ManagedFooACL
的数组,ByValArray
和SizeConst
的值为X,并将本地array
转换为struct
。
- 从函数中全局删除并重新实例化数组结构,而无需在编译时知道数组的大小
- 如何使用函数的输出初始化 const 数组结构字段?
- 传递数组结构、ofstream 和 interger 以运行
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 将文本文件读取到数组结构中
- C/C++中数组结构和数组结构的通用接口
- C++ MDC final-在字符类型的数组结构中按字母顺序对记录中的名称进行排序
- C++ 使用数组结构创建平衡的二叉搜索树
- C++:释放动态数组(结构成员)和指向此结构的指针的方法
- 使用 vector 在 c++ 中声明 3D 数组结构
- 数组结构无法正确打印
- 如何在C++中访问数组结构内部的数组结构
- CIN进入数组结构似乎什么也没输入
- ifstream将数组结构到txt文件中,然后尽可能将其提取为数组
- wlanapi-将WlanFreeMemory释放其WLAN_INTERFACE_INFO数组结构
- 显示数组结构 c++
- C++ 使用函数访问数组结构的方法是什么?
- 将庞大的数组结构复制到 GPU
- 如何封送包含字符矩阵的数组结构
- C++多维数组结构的对齐