0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305

1 Answers1

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;
Community
  • 1
  • 1
dtb
  • 213,145
  • 36
  • 401
  • 431