News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

API functions that return BOOL

Started by sinsi, January 19, 2024, 01:05:40 AM

Previous topic - Next topic

sinsi

BOOL GetFileSizeEx(
  [in]  HANDLE         hFile,
  [out] PLARGE_INTEGER lpFileSize
);
When testing the return value (for any BOOL return) I have been using EAX (because "everything is a DWORD" :badgrin: ).
After my playing with SQL and VB.net, I am more aware of data types and sizes.
From what I can find out, a BOOL in C++ is one byte, so should I be using AL instead of EAX?

Greenhorn

There is a difference between C/C++ bool and WinAPI BOOL.

In Windows headers BOOL is defined as DWORD, in opposite the C/C++ bool is not guaranteed to be 1 byte in size.

QuoteBoolean type

    bool — type, capable of holding one of the two values: true or false. The value of sizeof(bool) is implementation defined and might differ from 1.
Fundamental types
Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

sinsi

Makes a difference when you get an idea of where to look.
I didn't think of the windows headers

Just to muddy the waters for non-C people,
 - WinDef.h has typedef int BOOL;
 - WinNT.h  has typedef BYTE BOOLEAN;

Cheers for that  :thumbsup:

TimoVJL

Also
QuoteType: _VARIANT_BOOL A 16-bit Boolean value. A value of 0xFFFF (all bits 1) indicates true; a value of 0 (all bits 0) indicates false. No other values are valid.
May the source be with you

daydreamer

Test lowest bit and conditional jumps works independent of bool size
Use bool to Mask dword result ,dword size bool is enough

my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

sinsi


jj2007

Quote from: daydreamer on January 19, 2024, 08:47:28 AMTest lowest bit and conditional jumps works independent of bool size

If the function succeeds, the return value is nonzero.

M$ does not say "returns TRUE or FALSE". So GetFileSizeEx could return 2 or 100, and your conditional jump would be screwed. In practice, they all return 0 or 1 in eax, so it's ok - but that behaviour is not documented ;-)

One interesting point is this:
BOOL GetFileSizeEx(
  [in]  HANDLE        hFile,
  [out] PLARGE_INTEGER lpFileSize
);

So it returns a BOOL. What is a BOOL, and how is it different from bool or BOOLEAN?

Windef.h:
typedef int                BOOL;
typedef BOOL near          *PBOOL;
typedef BOOL far            *LPBOOL;
typedef int                INT;
typedef unsigned int        UINT;

Crystal clear, isn't it?

P.S., just in case you are not yet thoroughly confused:

bool (C++)
QuoteA variable of this type can have values true and false. Conditional expressions have the type bool and so have values of type bool. For example, i != 0 now has true or false depending on the value of i.

Visual Studio 2017 version 15.3 and later (Available with /std:c++17 and later): The operand of a postfix or prefix increment or decrement operator may not be of type bool. In other words, given a variable b of type bool, these expressions are no longer allowed:

b++;
    ++b;
    b--;
    --b;

The values true and false have the following relationship:

!false == true
!true == false

And...
QuoteThe bool type participates in default integral promotions

C/C++ at its best - you must love it :badgrin:

P.P.S., just for the record: uppercase BOOL is a DWORD, i.e. test eax, eax is perfectly valid :thumbsup:

NoCforMe

So to sum up, the foolproof way to test a BOOL would be to test for zero ("FALSE"), which is actually easier than the alternative:
    TEST    EAX, EAX
    JNZ     isTRUE
Amiright?

(Assuming that BOOL == int, of course)
Assembly language programming should be fun. That's why I do it.