uLib  User mode C/C++ extended API library for Win32 programmers.
Generic Types and Functions

Module Description

Type definitions, globals, macros, constants, and common functions.
#include <uLib/Common.h>

Notes
  • If you're targeting ReactOS with your program,
    You should normally build uLib with #define WINVER 0x0502.
  • If you need <intrin.h>, You should include it before <uLib/Common.h>

Groups

 Parameter Annotation
 
 Global Variables
 
 Fundamental Datatypes
 
 S.E.H Supplements
 
 Byte Order Swappers
 
 Integer Bit Rotation
 
 Windows Version Checking
 
 Arithmetic Supplements
 
 Call Types
 
 Defined Constants and Composites
 
 Undocumented Messages
 
 Common Characters
 
 Control Characters
 

Macros

#define NT_SUCCESS(Status)   ((NTSTATUS)(Status) >= 0)
 
#define NT_ERROR(Status)   (((ULONG)(Status) >> 30) == 3)
 
#define BEGIN_EXTERN_C   extern "C" {
 
#define END_EXTERN_C   }
 
#define BEGIN_EXTERN_CPP   extern "C++" {
 
#define END_EXTERN_CPP   }
 
#define BEGIN_NAMESPACE(name)   namespace name {
 
#define END_NAMESPACE(name)   }
 
#define BEGIN_ANONYMOUS   namespace {
 
#define END_ANONYMOUS   }
 
#define DEF_(x)   = x
 
#define __assembly   __declspec(naked)
 
#define DISABLE_BOOLCAST_WARNING   __pragma( warning( disable: 4800 ))
 
#define REENABLE_BOOLCAST_WARNING   __pragma( warning( default: 4800 ))
 
#define __bool_cast_defined
 
#define dimof(x)   (sizeof(x)/sizeof(x[0]))
 
#define offsetof(stype, field)   ((size_t)&reinterpret_cast<char const volatile&>((((stype*)0)->field)))
 
#define structof(faddr, stype, field)   ((stype*)( (ULONG_PTR)(faddr) - (ULONG_PTR)offsetof(stype,field)))
 
#define fieldsize(stype, field)   sizeof(((stype*)0)->field)
 
#define struct_subsize(stype, field)   (offsetof( stype,field ) + fieldsize( stype,field ))
 
#define LITERAL(type, x)   (*((type*)&(x)))
 
#define UNUSED(x)   ((void)&(x))
 
#define ZEROMEM(var)   memset( &(var),0,sizeof(var) )
 
#define YIELD()   SleepEx( 0, true )
 
#define SWAP_INPLACE(a, b)   a ^= b ^= a ^= b
 
#define ROUND_UP(x, chunk)   (((UINT_PTR(x) + (chunk) - 1) / (chunk)) * (chunk))
 
#define ROUND_DOWN(x, chunk)   ((UINT_PTR(x) / (chunk)) * (chunk))
 
#define ROUND_UP2(x, chunk)   ((UINT_PTR(x)+((chunk)-1)) & -(chunk))
 
#define ROUND_DOWN2(x, chunk)   (UINT_PTR(x) & -(chunk))
 
#define ALIGN_VALUE(x, aln)   (INT_PTR(x) + ((~INT_PTR(x)+1) & (aln-1)))
 
#define DELETE_ARRAY(A)   if (A) { delete[] (A); (A) = NULL; }
 
#define DELETE_OBJ(Obj)   if (Obj) { delete (Obj); (Obj) = NULL; }
 
#define RELEASE_OBJ(Ifc)   if (Ifc) { (Ifc)->Release(); (Ifc) = NULL; }
 
#define COMTASK_FREE(Obj)   if (Obj) { CoTaskMemFree( PVOID(Obj) ); (Obj) = NULL; }
 
#define _BAD_HANDLE   INVALID_HANDLE_VALUE
 
#define BAD_HANDLE(hnd)   ((hnd) == _BAD_HANDLE || !(hnd))
 
#define GOOD_HANDLE(hnd)   ((hnd) != _BAD_HANDLE && (hnd) != NULL)
 
#define CHK_HANDLE(hnd)   ((hnd) == _BAD_HANDLE ? NULL : (hnd))
 
#define _BAD_ATTRIBUTE   INVALID_FILE_ATTRIBUTES
 
#define BITS_SET(bits, x)   (((x) & (bits)) == (bits))
 
#define BITS_RST(bits, x)   (((x) & (bits)) == 0)
 
#define HAVE_BITS(var, mask)   BITS_SET( (mask),(var) )
 
#define ALL_BITS   0xFFFFFFFF
 
#define ALL_BITS64   0xFFFFFFFFFFFFFFFF
 
#define _BV(bitNr)   (UINT_PTR(1) << (bitNr))
 
#define PP_NSTR(x)   PP_NSTR_(x)
 
#define PP_NSTR_(x)   #x
 
#define PP_STR(x)   _T(#x)
 
#define CCSTR_LENGTH(s)   (dimof(s) - sizeof(*s))
 
#define CCH_AVAIL(buf, pz)   (dimof(buf) - (CSTR(pz) - CSTR(buf)))
 
#define _ODS(s)   OutputDebugString( s )
 
#define _ODSA(s)   OutputDebugStringA( s )
 
#define _ODSW(s)   OutputDebugStringW( s )
 
#define _CASSERT(id, expr)   typedef char __cassert_##id[ (expr) ? +1 : -1 ]
 
#define OFFSETOFCLASS(base, derived)   ((DWORD)(DWORD_PTR) (STATIC_CAST(base*)( (derived*)8 )) - 8)
 

Functions

bool __forceinline bool_cast (BOOL B52)
 

Macro Definition Documentation

◆ NT_SUCCESS

#define NT_SUCCESS (   Status)    ((NTSTATUS)(Status) >= 0)

Native call successful ?

Definition at line 154 of file Common.h.

◆ NT_ERROR

#define NT_ERROR (   Status)    (((ULONG)(Status) >> 30) == 3)

Native call resulted in error ?

Definition at line 158 of file Common.h.

◆ BEGIN_EXTERN_C

#define BEGIN_EXTERN_C   extern "C" {

Definition at line 220 of file Common.h.

◆ END_EXTERN_C

#define END_EXTERN_C   }

Definition at line 221 of file Common.h.

◆ BEGIN_EXTERN_CPP

#define BEGIN_EXTERN_CPP   extern "C++" {

Definition at line 222 of file Common.h.

◆ END_EXTERN_CPP

#define END_EXTERN_CPP   }

Definition at line 223 of file Common.h.

◆ BEGIN_NAMESPACE

#define BEGIN_NAMESPACE (   name)    namespace name {

Definition at line 224 of file Common.h.

◆ END_NAMESPACE

#define END_NAMESPACE (   name)    }

Definition at line 225 of file Common.h.

◆ BEGIN_ANONYMOUS

#define BEGIN_ANONYMOUS   namespace {

Definition at line 226 of file Common.h.

◆ END_ANONYMOUS

#define END_ANONYMOUS   }

Definition at line 227 of file Common.h.

◆ DEF_

#define DEF_ (   x)    = x

Default parameter declaration

Definition at line 240 of file Common.h.

◆ __assembly

#define __assembly   __declspec(naked)

Preferred prefix for MSVC inline assembly routines (x86).

Definition at line 677 of file Common.h.

◆ DISABLE_BOOLCAST_WARNING

#define DISABLE_BOOLCAST_WARNING   __pragma( warning( disable: 4800 ))

Definition at line 752 of file Common.h.

◆ REENABLE_BOOLCAST_WARNING

#define REENABLE_BOOLCAST_WARNING   __pragma( warning( default: 4800 ))

Definition at line 753 of file Common.h.

◆ __bool_cast_defined

#define __bool_cast_defined

Definition at line 770 of file Common.h.

◆ dimof

#define dimof (   x)    (sizeof(x)/sizeof(x[0]))

'Dimension of' an array.
dimof is by far the best identifier I've encountered for array dimensions.
I got it a long time ago from a Russian programmer. Equvalent to _countof.

Definition at line 949 of file Common.h.

◆ offsetof

#define offsetof (   stype,
  field 
)    ((size_t)&reinterpret_cast<char const volatile&>((((stype*)0)->field)))

Get the offset of 'field' in the struct type 'stype'.
Note that 'field' can even be a subscripted array member, e.g. szSomeStr[32] !

Definition at line 956 of file Common.h.

◆ structof

#define structof (   faddr,
  stype,
  field 
)    ((stype*)( (ULONG_PTR)(faddr) - (ULONG_PTR)offsetof(stype,field)))

See MSDN doc for CONTAINING_RECORD( address, type, field )

Definition at line 962 of file Common.h.

◆ fieldsize

#define fieldsize (   stype,
  field 
)    sizeof(((stype*)0)->field)

get the size of a struct member.

Definition at line 963 of file Common.h.

◆ struct_subsize

#define struct_subsize (   stype,
  field 
)    (offsetof( stype,field ) + fieldsize( stype,field ))

Get the subsize of struct 'stype' up to and including 'field'.

Definition at line 966 of file Common.h.

◆ LITERAL

#define LITERAL (   type,
 
)    (*((type*)&(x)))

Omnipotent cast (more or less).

Definition at line 969 of file Common.h.

◆ UNUSED

#define UNUSED (   x)    ((void)&(x))

I prefer it not too verbose.

Definition at line 970 of file Common.h.

◆ ZEROMEM

#define ZEROMEM (   var)    memset( &(var),0,sizeof(var) )

Note: 'var' must be a variable, not a pointer to a variable.

Definition at line 971 of file Common.h.

◆ YIELD

#define YIELD ( )    SleepEx( 0, true )

Yield to another scheduled thread that's ready to run.

Definition at line 972 of file Common.h.

◆ SWAP_INPLACE

#define SWAP_INPLACE (   a,
 
)    a ^= b ^= a ^= b

Integer swap w/o intermediate var.

Definition at line 974 of file Common.h.

◆ ROUND_UP

#define ROUND_UP (   x,
  chunk 
)    (((UINT_PTR(x) + (chunk) - 1) / (chunk)) * (chunk))

Note: ROUND_UP( 0,N ), ROUND_UP2( 0,N ), and ALIGN_VALUE( 0,N ) return 0, not N!

Unsigned integers, can be odd as well.

Definition at line 979 of file Common.h.

◆ ROUND_DOWN

#define ROUND_DOWN (   x,
  chunk 
)    ((UINT_PTR(x) / (chunk)) * (chunk))

Definition at line 980 of file Common.h.

◆ ROUND_UP2

#define ROUND_UP2 (   x,
  chunk 
)    ((UINT_PTR(x)+((chunk)-1)) & -(chunk))

Unsigned power-of-2 integers (faster than ROUND_UP)

Definition at line 984 of file Common.h.

◆ ROUND_DOWN2

#define ROUND_DOWN2 (   x,
  chunk 
)    (UINT_PTR(x) & -(chunk))

Definition at line 985 of file Common.h.

◆ ALIGN_VALUE

#define ALIGN_VALUE (   x,
  aln 
)    (INT_PTR(x) + ((~INT_PTR(x)+1) & (aln-1)))

Note: 'aln' must be a power of 2. Equivalent to ROUND_UP2

Definition at line 989 of file Common.h.

◆ DELETE_ARRAY

#define DELETE_ARRAY (   A)    if (A) { delete[] (A); (A) = NULL; }

Definition at line 992 of file Common.h.

◆ DELETE_OBJ

#define DELETE_OBJ (   Obj)    if (Obj) { delete (Obj); (Obj) = NULL; }

Definition at line 993 of file Common.h.

◆ RELEASE_OBJ

#define RELEASE_OBJ (   Ifc)    if (Ifc) { (Ifc)->Release(); (Ifc) = NULL; }

Definition at line 994 of file Common.h.

◆ COMTASK_FREE

#define COMTASK_FREE (   Obj)    if (Obj) { CoTaskMemFree( PVOID(Obj) ); (Obj) = NULL; }

Definition at line 995 of file Common.h.

◆ _BAD_HANDLE

#define _BAD_HANDLE   INVALID_HANDLE_VALUE

Shorthand alias identity.

Definition at line 997 of file Common.h.

◆ BAD_HANDLE

#define BAD_HANDLE (   hnd)    ((hnd) == _BAD_HANDLE || !(hnd))

Caveat Emptor: Don't use a function call like CreateFile() as an argument
to the handle-check macros GOOD_HANDLE(), BAD_HANDLE(), and CHK_HANDLE().
The argument /must/ be a variable, else the macro will fail due to calling the function twice!

Definition at line 1004 of file Common.h.

◆ GOOD_HANDLE

#define GOOD_HANDLE (   hnd)    ((hnd) != _BAD_HANDLE && (hnd) != NULL)

Definition at line 1005 of file Common.h.

◆ CHK_HANDLE

#define CHK_HANDLE (   hnd)    ((hnd) == _BAD_HANDLE ? NULL : (hnd))

Note: It's recommended to use CheckHandle() instead of CHK_HANDLE().

Definition at line 1006 of file Common.h.

◆ _BAD_ATTRIBUTE

#define _BAD_ATTRIBUTE   INVALID_FILE_ATTRIBUTES

Shorthand alias identity.

Definition at line 1010 of file Common.h.

◆ BITS_SET

#define BITS_SET (   bits,
 
)    (((x) & (bits)) == (bits))

The nomenclature logic of the bit test macro IDs are: Left to right reading.
I.e: BITS_SET --> BITS = Bitmask --> SET = Variable that have the bits.
Or: HAVE_BITS --> HAVE = Variable that have the bits --> BITS = Bitmask.

Are all bits set?

Definition at line 1016 of file Common.h.

◆ BITS_RST

#define BITS_RST (   bits,
 
)    (((x) & (bits)) == 0)

Are all bits zero?

Definition at line 1017 of file Common.h.

◆ HAVE_BITS

#define HAVE_BITS (   var,
  mask 
)    BITS_SET( (mask),(var) )

If you want the var first.

Definition at line 1018 of file Common.h.

◆ ALL_BITS

#define ALL_BITS   0xFFFFFFFF

UINT(-1) without type spec.

Definition at line 1020 of file Common.h.

◆ ALL_BITS64

#define ALL_BITS64   0xFFFFFFFFFFFFFFFF

UINT64(-1) without type spec.

Definition at line 1021 of file Common.h.

◆ _BV

#define _BV (   bitNr)    (UINT_PTR(1) << (bitNr))

BitValue.. (Standard MCU-world macro).

Definition at line 1023 of file Common.h.

◆ PP_NSTR

#define PP_NSTR (   x)    PP_NSTR_(x)

PreProcessor Numeric String

Definition at line 1027 of file Common.h.

◆ PP_NSTR_

#define PP_NSTR_ (   x)    #x

Definition at line 1028 of file Common.h.

◆ PP_STR

#define PP_STR (   x)    _T(#x)

Transmutable stringizing macro

Definition at line 1032 of file Common.h.

◆ CCSTR_LENGTH

#define CCSTR_LENGTH (   s)    (dimof(s) - sizeof(*s))

Count of chars in a string constant, less the terminator.

Definition at line 1036 of file Common.h.

◆ CCH_AVAIL

#define CCH_AVAIL (   buf,
  pz 
)    (dimof(buf) - (CSTR(pz) - CSTR(buf)))

Remaining char count in 'buf' beyond 'pz'

Definition at line 1040 of file Common.h.

◆ _ODS

#define _ODS (   s)    OutputDebugString( s )

Finger-, and keyboard-, saver...

Definition at line 1044 of file Common.h.

◆ _ODSA

#define _ODSA (   s)    OutputDebugStringA( s )

Definition at line 1045 of file Common.h.

◆ _ODSW

#define _ODSW (   s)    OutputDebugStringW( s )

Definition at line 1046 of file Common.h.

◆ _CASSERT

#define _CASSERT (   id,
  expr 
)    typedef char __cassert_##id[ (expr) ? +1 : -1 ]

_CASSERT checks boolean/integral conditions at compile time.
It is purely a compile-time static mechanism that generates no code.
On fail it prints something like "Error: Negative subscript".
Some compilers neatly include the __cassert_Id in the error message.

Definition at line 1056 of file Common.h.

◆ OFFSETOFCLASS

#define OFFSETOFCLASS (   base,
  derived 
)    ((DWORD)(DWORD_PTR) (STATIC_CAST(base*)( (derived*)8 )) - 8)

OFFSETOFCLASS is used to help C++ resolve multiple COM interface inheritance.
Example (class CSample : public IPersistFolder)..

HRESULT CSample::QueryInterface( REFIID riid, void **ppv )
{
static const QITAB quint[] = {
{ &IID_IPersist, OFFSETOFCLASS( IPersist, CSample ) },
{ &IID_IPersistFolder, OFFSETOFCLASS( IPersistFolder, CSample ) },
{ NULL, 0 },
};
return QISearch( this, quint, riid, ppv ); // Neat.. :)
}

See also QITABENT, QITABENTMULTI, which can make the table less elaborate.
[REF] ShLwApi.h : Line 2024++

Definition at line 1079 of file Common.h.

Function Documentation

◆ bool_cast()

bool __forceinline bool_cast ( BOOL  B52)

A BOOL has (2^32)-1 'true' values, and I prefer one, to stay stringent.
The cast is forced inline, and should ideally not generate extra code,
just coax the compiler to treat the BOOL as a bool without whining.
(In practice, it generates two machine code instructions.)

Definition at line 767 of file Common.h.