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 798 times
0
marc_s
- 732,580
- 175
- 1,330
- 1,459
Oleg Vazhnev
- 23,239
- 54
- 171
- 305
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;