The SP_DEVICE_INTERFACE_DETAIL_DATA
structure:
typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA {
DWORD cbSize;
TCHAR DevicePath[ANYSIZE_ARRAY];
} SP_DEVICE_INTERFACE_DETAIL_DATA, *PSP_DEVICE_INTERFACE_DETAIL_DATA;
How do I declare it in C# to get Marshal.SizeOf
work properly?
I don't have a problem with allocating dynamic buffer. I only want to calculate cbSize
in a proper, non-hardcoded manner.
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.
This is what unmanaged C++ gives:
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
I tried every possible combination of StructLayout
and MarshalAs
parameters, but I couldn't get it to return the above values.
What is the correct declaration?
4
is not a valid size. – Osage