编组未知的数组大小
您有一个采用字节数组的结构
byte[]
,但是,该数组的大小取决于您提交的图像(宽度x高度)
所以...您该怎么办?
[MarshalAs(UnmanagedType.ByValArray, SizeConst = ???)]
public Byte[] ImageData;
在处理从 C# 传递到的字节数组时,sizeconst 是必须有的吗? C dll?
You have a structure that takes a byte array
byte[]
however, the size of that array depends on the image you are submitting (widthxheight)
So... how do you do
[MarshalAs(UnmanagedType.ByValArray, SizeConst = ???)]
public Byte[] ImageData;
Is the sizeconst a MUST HAVE when working with byte arrays being passed from C# to C dlls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要更改编组类型。 如果您要编组为 ByValArray,但不编组为其他类型,则需要 SizeConst。 有关详细信息,请查看 UnmanagedType 枚举 。
我怀疑您希望将其编组为指向数组的 C 指针:
这将导致其编组为标准 C 数组 (BYTE*),因此仅传递一个指针。 这样做可以让您传递任何大小的数组。 通常,您还需要将数组大小作为另一个参数(或图像宽度/高度/bpp,它提供相同的信息)传递,因为在 C/C++ 中无法轻松告知这一点。
You need to change the marshalling type. SizeConst is required if you're marshalling as ByValArray, but not with other types. For details, look at the UnmanagedType enum.
My suspicion is that you want to marshall as a C pointer to the array:
This will cause it to marshall through to a standard C array (BYTE*), so only a pointer is passed through. Doing this allows you to pass any sized array. Typically, you'll also want to pass through the array size as another parameter (or image width/height/bpp, which provides the same info), since there's no way in C/C++ to tell that easily.