is bool a 32 bit integer?

  • Thread starter Thread starter ORC
  • Start date Start date
O

ORC

Is the bool type actually an Int32 with 0 as false and non zero as true? The
reason for my question is that I've seen a lot of API calls that return a
Int32 implemented in C# as an bool like:
[DllImport("kernel32.dll", EntryPoint="PurgeComm", SetLastError=true)]
private static extern bool PurgeComm( Int32 hFile, UInt32 dwFlags);

Thanks
Ole
 
Is the bool type actually an Int32 with 0 as false and non zero as true?

No, bool is one byte.

The
reason for my question is that I've seen a lot of API calls that return a
Int32 implemented in C# as an bool like:
[DllImport("kernel32.dll", EntryPoint="PurgeComm", SetLastError=true)]
private static extern bool PurgeComm( Int32 hFile, UInt32 dwFlags);

That's correct, the runtime can marshal a Win32 BOOL to a C# bool.



Mattias
 
Thanks.
As the windows BOOL is defined as:
typedef int BOOL; in WINDEF.H
I assume that an API that returns an int can be marshalled to a C# bool - or
am I wrong?
What about a BOOL (int) parameter in an API function - is that also
marshalled to C# bool ?

Thanks for your help!
Ole


Mattias Sjögren said:
Is the bool type actually an Int32 with 0 as false and non zero as true?

No, bool is one byte.

The
reason for my question is that I've seen a lot of API calls that return a
Int32 implemented in C# as an bool like:
[DllImport("kernel32.dll", EntryPoint="PurgeComm", SetLastError=true)]
private static extern bool PurgeComm( Int32 hFile, UInt32 dwFlags);

That's correct, the runtime can marshal a Win32 BOOL to a C# bool.



Mattias
 
ORC,

You could try, but it wouldn't be accurate. The reasoning behind BOOL
in Win32 is to provide a true/false value. Zero is false, true is not
false. There are really only two states that can be returned, they just
choose to use 32 bits to represent it. Since a bool represents the same
thing in .NET, they can be mapped accurately.

When the return type is an integer, then the state can be any value that
an int can represent. If you map this to bool (which in reality, has only
two values, true and false), you will not get an accurate representation of
the return value. You ^could^ do it, but you won't get the right
information back.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ORC said:
Thanks.
As the windows BOOL is defined as:
typedef int BOOL; in WINDEF.H
I assume that an API that returns an int can be marshalled to a C# bool -
or
am I wrong?
What about a BOOL (int) parameter in an API function - is that also
marshalled to C# bool ?

Thanks for your help!
Ole


Mattias Sjögren said:
Is the bool type actually an Int32 with 0 as false and non zero as true?

No, bool is one byte.

The
reason for my question is that I've seen a lot of API calls that return
a
Int32 implemented in C# as an bool like:
[DllImport("kernel32.dll", EntryPoint="PurgeComm", SetLastError=true)]
private static extern bool PurgeComm( Int32 hFile, UInt32 dwFlags);

That's correct, the runtime can marshal a Win32 BOOL to a C# bool.



Mattias
 
OK. I only need to map BOOL from the API to bool in C# so from your
explanation I guees it will work - I'll give it a try :-)

Thank you both for your promt help!

Best regards
Ole

Nicholas Paldino said:
ORC,

You could try, but it wouldn't be accurate. The reasoning behind BOOL
in Win32 is to provide a true/false value. Zero is false, true is not
false. There are really only two states that can be returned, they just
choose to use 32 bits to represent it. Since a bool represents the same
thing in .NET, they can be mapped accurately.

When the return type is an integer, then the state can be any value that
an int can represent. If you map this to bool (which in reality, has only
two values, true and false), you will not get an accurate representation of
the return value. You ^could^ do it, but you won't get the right
information back.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ORC said:
Thanks.
As the windows BOOL is defined as:
typedef int BOOL; in WINDEF.H
I assume that an API that returns an int can be marshalled to a C# bool -
or
am I wrong?
What about a BOOL (int) parameter in an API function - is that also
marshalled to C# bool ?

Thanks for your help!
Ole


Mattias Sjögren said:
Is the bool type actually an Int32 with 0 as false and non zero as true?

No, bool is one byte.


The
reason for my question is that I've seen a lot of API calls that return
a
Int32 implemented in C# as an bool like:
[DllImport("kernel32.dll", EntryPoint="PurgeComm", SetLastError=true)]
private static extern bool PurgeComm( Int32 hFile, UInt32 dwFlags);

That's correct, the runtime can marshal a Win32 BOOL to a C# bool.



Mattias
 
Back
Top