typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA {
DWORD cbSize;
TCHAR DevicePath[ANYSIZE_ARRAY];
} SP_DEVICE_INTERFACE_DETAIL_DATA, *PSP_DEVICE_INTERFACE_DETAIL_DATA;
我该如何在 C# 中声明它才能得到Marshal.Sizeof
正常工作?
我对分配动态缓冲没有问题。
The definition at PInvoke.net is wrong.
The explanation at PInvoke.net is also wrong:
SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA(); didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // trust me :)
Don t trust him.
4 + Marshal.SystemDefaultCharSize
is only valid on x86. Same for sizeof(int) + Marshal.SystemDefaultCharSize
. On x64 it fails miserably.
这是未管理的 C++ 提供的 :
x86
Struct size A:5
Offset of device path A:4
Struct size W:6
Offset of device path W:4
x64
Struct size A:8
Offset of device path A:4
Struct size W:8
Offset of device path W:4
我尝试了所有可能的组合 StructLayout
和 MarshalAs
参数,但我无法让它返回上述值 。
正确的申报是什么?