I'm fiddling with calling DLLs from C#, and came across the need to define my own structs. Lots of articles force a sequential layout for the struct with
我正在摆弄从C#调用DLL,并且需要定义我自己的结构。很多文章强制结构的顺序布局
[StructLayout(LayoutKind.Sequential)]
struct Foo ...
So, I followed suite, and my programme worked. Now, when I took the line out, it still works. Why do I need it?
所以,我按照套件,我的程序工作。现在,当我把线路拿出来时,它仍然可以工作。我为什么需要它?
3 个解决方案
#1
The internal layout of a managed struct is undocumented and undiscoverable. Implementation details like member order and packing are intentionally hidden. With the [StructLayout] attribute, you force the P/Invoke marshaller to impose a specific layout and packing.
托管结构的内部布局未记录且无法发现。故意隐藏成员订单和打包等实施细节。使用[StructLayout]属性,可以强制P / Invoke编组器强制执行特定的布局和打包。
That the default just happens to match what you need to get your code to work is merely an accident. Although not an uncommon one. Note the Type.StructLayoutAttribute property.
默认恰好匹配您的代码工作所需的只是一个意外。虽然不是一个不寻常的。请注意Type.StructLayoutAttribute属性。
#2
Interesting point. I'm sure I had code that failed until I put in an explicit LayoutKind.Sequential, however I have confirmed Sequential is the default for structures even in 1.1.
有趣的一点。我确定我的代码失败了,直到我输入一个显式的LayoutKind.Sequential,但我已经确认Sequential是结构的默认值,即使在1.1中也是如此。
Note the VB Reference for Structure implies at Remarks > Behaviour > Memory Consumption that you do need to specify StructLayout to confirm the memory layout, but the documentation for StructLayoutAttribute states Sequential is the default for structures in Microsoft compilers.
注意VB Reference for Structure意味着在备注>行为>内存消耗中,您需要指定StructLayout来确认内存布局,但StructLayoutAttribute的文档说明Sequential是Microsoft编译器中结构的默认值。
#3
I am not entirely sure, but it may affect binary serialization - it might spit out the fields in order with not naming or ordering information (resulting in a smaller file), but that is a complete whim.
我不完全确定,但它可能会影响二进制序列化 - 它可能会在没有命名或排序信息的情况下按顺序吐出字段(导致文件较小),但这是一个完整的心血来潮。
#1
The internal layout of a managed struct is undocumented and undiscoverable. Implementation details like member order and packing are intentionally hidden. With the [StructLayout] attribute, you force the P/Invoke marshaller to impose a specific layout and packing.
托管结构的内部布局未记录且无法发现。故意隐藏成员订单和打包等实施细节。使用[StructLayout]属性,可以强制P / Invoke编组器强制执行特定的布局和打包。
That the default just happens to match what you need to get your code to work is merely an accident. Although not an uncommon one. Note the Type.StructLayoutAttribute property.
默认恰好匹配您的代码工作所需的只是一个意外。虽然不是一个不寻常的。请注意Type.StructLayoutAttribute属性。
#2
Interesting point. I'm sure I had code that failed until I put in an explicit LayoutKind.Sequential, however I have confirmed Sequential is the default for structures even in 1.1.
有趣的一点。我确定我的代码失败了,直到我输入一个显式的LayoutKind.Sequential,但我已经确认Sequential是结构的默认值,即使在1.1中也是如此。
Note the VB Reference for Structure implies at Remarks > Behaviour > Memory Consumption that you do need to specify StructLayout to confirm the memory layout, but the documentation for StructLayoutAttribute states Sequential is the default for structures in Microsoft compilers.
注意VB Reference for Structure意味着在备注>行为>内存消耗中,您需要指定StructLayout来确认内存布局,但StructLayoutAttribute的文档说明Sequential是Microsoft编译器中结构的默认值。
#3
I am not entirely sure, but it may affect binary serialization - it might spit out the fields in order with not naming or ordering information (resulting in a smaller file), but that is a complete whim.
我不完全确定,但它可能会影响二进制序列化 - 它可能会在没有命名或排序信息的情况下按顺序吐出字段(导致文件较小),但这是一个完整的心血来潮。