Case in point, I want to use DEV_BROADCAST_DEVICEINTERFACE_A from C#. However, I'm not sure how to declare the struct since the size of dbcc_name
is dependent on dbcc_size
(It's officially declared as char dbcc_name[1]
).
According to this question it seems like I need to add
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)]
over dbcc_name
.
But why use SizeConst=255
? We don't know the size. (And it seems from other answers I've seen, that there is no simple way to declare it such that it will know the correct size or a way to specify the size case by case.)
So what happens if I set a static length as in the linked answer. What would happen if the string is shorter or longer?
Testing has shown that if it's longer I get the correct string, and if shorter - I get a truncated string (e.g. if I set SizeConst to 2 and the real string is "abc", I get "ab".) But can I be sure that that's how it works, or is it dependent on something that just happens to be fine in this specific case?