Marshal class doesn't contain ReadBool method. If my c++ structure contains bool field then how should I read it? I've tried to do this: (bool) Marshal.ReadInt32(intPointer, offset) but it is not allowed to cast int32 to bool.
Asked
Active
Viewed 756 times
0
marc_s
- 704,970
- 168
- 1,303
- 1,425
Oleg Vazhnev
- 22,231
- 49
- 161
- 290
1 Answers
2
sizeof(bool) in C++ is implementation-defined, so it might be better to define the field in the struct as an integer of a known size (e.g., int32_t or BOOL). Then it's customary use 0 to indicate false and not-0 to indicate true:
// C++
intPointer->int32_t_field = bool_value ? 1 : 0;
// C#
bool result = Marshal.ReadInt32(intPointer, offset) != 0;