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
  • 704,970
  • 168
  • 1,303
  • 1,425
Oleg Vazhnev
  • 22,231
  • 49
  • 161
  • 290

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
  • 206,299
  • 34
  • 391
  • 426