How big is a C BOOLEAN

  • Thread starter Thread starter Dale
  • Start date Start date
D

Dale

Stupid question, but since I can't find it in the VS help...
I need to create a structure and pass it to a Win32 API function.

How many bytes is a C BOOLEAN? And is it the same size as a C#
bool/System.Boolean? If so is it set (values) the same, or do I need to
create an integer of the required size and set it to 0 or -1 as required for
passing to the external Win32 DLL?



Thanks.



Dale
 
There is a method you can call in .net to show the size, but I dont have it
handy, you would instantiate some variable then print and call something to
show the size;

Isnt a C boolean the same size as INT?

And I think its either zero for false or non-zero (undefined) for true?
 
How many bytes is a C BOOLEAN? And is it the same size as a C#I don't think C has a BOOLEAN type. Most likely a #define.
But if you have it defined somewhere and you want the size,
try printing sizeof(BOOLEAN).
You can also print TRUE and FALSE (also #defines, if you ask me)
 
Stupid question, but since I can't find it in the VS help...

Not exactly what you're asking for, but see
http://msdn.microsoft.com/library/en-us/cpguide/html/cpcondefaultmarshalingforbooleans.asp

How many bytes is a C BOOLEAN?

The Win32 BOOELAN type is 1 byte. It's defined as

typedef BYTE BOOLEAN;

in Winnt.h in the Platform SDK.

And is it the same size as a C# bool/System.Boolean?
Yes


If so is it set (values) the same, or do I need to
create an integer of the required size and set it to 0 or -1 as required for
passing to the external Win32 DLL?

No, the runtime can handle marshaling a managed bool to various kinds
of native ones, including BOOLEAN. Just apply the
MarshalAs(UnmanagedType.U1) attribute like the MSDN page linked to
above says.



Mattias
 
Beware the booleans!
Win32 defines different versions of booleans.
1. BOOL used by most Win32 API's, is an unsigned int (4 bytes)
2. BOOLEAN is a single byte, only used by a few win32 API's!!
3. and C/C++ has it's builtin 'bool' which is a single byte

Willy.
 
Back
Top