473,387 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

is bool a 32 bit integer?

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
Nov 16 '05 #1
4 10720
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
ORC
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" <ma********************@mvps.org> wrote in message
news:OD****************@TK2MSFTNGP10.phx.gbl...
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"ORC" <or*@sol.dk> wrote in message
news:ea**************@TK2MSFTNGP14.phx.gbl...
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" <ma********************@mvps.org> wrote in message
news:OD****************@TK2MSFTNGP10.phx.gbl...
>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 16 '05 #4
ORC
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:OX****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"ORC" <or*@sol.dk> wrote in message
news:ea**************@TK2MSFTNGP14.phx.gbl...
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" <ma********************@mvps.org> wrote in message
news:OD****************@TK2MSFTNGP10.phx.gbl...

>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.



Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have...
14
by: JKop | last post by:
Will the following print "NOT equal" on all implementations: #include <iostream> signed main() { bool k; k = 5;
19
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or...
12
by: Fred | last post by:
Why was the bool type introduced into c++? What does it provide that int does not and are the two entirely interchangable in conditional expressions? Thanks Fred
11
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper...
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
2
by: TheRomance | last post by:
i have a problem about insert integer to link list. sorry it's too long but i try many times, many ways , it's still have an error function is fix . can't change anything about function. i...
4
by: Frederick Gotham | last post by:
Does "bool" behave _exactly_ like any other integer type, except for when it comes to integer promotion? Consider the following C function whose purpose it is to calculate the quantity of equal...
9
by: jacob navia | last post by:
Hi I am incorporating 128 Bit integer code into lcc-win and it would be nice to have some code to test this feature. Has anyone here code that uses 128 bit integers? Thanks in advance ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.