56 #ifndef _uLib_Common_h_incl_ 57 #define _uLib_Common_h_incl_ 85 #ifdef __GNUC__ // GCC headers don't have 0x0400 conditionals 86 #pragma message("ALERT: GCC headers don't have WINVER 0x0400 conditionals, so ...") 87 #pragma message("ALERT: **YOU** must maintain NT4 compatibility, by not using NT5+ functions.") 100 #define NO_VERSION_WARNING // Legacy: For my patched NT4 SDK headers (not for you). 104 #define _WIN32_IE WINVER 107 #define _WIN32_WINNT WINVER // IsDebuggerPresent (& c...) 109 #ifndef _WIN32_WINDOWS 110 #define _WIN32_WINDOWS WINVER // -"- 112 #ifndef WIN32_LEAN_AND_MEAN 113 #define WIN32_LEAN_AND_MEAN 1 // Exclude rarely-used stuff from Windows headers 120 #define TYPEDEF_STRINGS 1 // Use 0 to make them #defines instead of typedefs. 122 #ifndef NO_NDK_FILES // Define this if you don't have the NDK. 123 #if defined(_WIN64) && (_WIN32_WINNT < 0x0502) 124 #pragma message("[uLib] ALERT: NDK files excluded in x64 due to _WIN32_WINNT < 0x0502") 127 #define NO_NDK_FILES // Exclude the NDK. 131 #ifndef NO_ULIB_BLDINFO 134 #define BLD_INFO(str) 146 #pragma warning( disable: 4005 ) 149 #include <NtStatus.h> 150 #define WIN32_NO_STATUS // Prevent subsequent #define STATUS_xx clashes. 154 #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0) 158 #define NT_ERROR(Status) (((ULONG)(Status) >> 30) == 3) 163 #include <WindowsX.h> 165 #include <WinError.h> 170 #define _USE_MATH_DEFINES 182 #include <RichEdit.h> 183 #include <CommCtrl.h> 187 #include <ShellApi.h> 198 #include <WInternl.h> 199 typedef NTSTATUS* PNTSTATUS;
207 #include <NtSecApi.h> 211 #endif//ndef _NTSECAPI_ 214 typedef LONG NTSTATUS, *PNTSTATUS;
220 #define BEGIN_EXTERN_C extern "C" { 221 #define END_EXTERN_C } // extern "C" 222 #define BEGIN_EXTERN_CPP extern "C++" { 223 #define END_EXTERN_CPP } // extern "C++" 224 #define BEGIN_NAMESPACE(name) namespace name { 225 #define END_NAMESPACE(name) } // namespace name 226 #define BEGIN_ANONYMOUS namespace { // anonymous 227 #define END_ANONYMOUS } // anonymous namespace 228 #else // C lacks those features.. 229 #define BEGIN_EXTERN_C 231 #define BEGIN_EXTERN_CPP 232 #define END_EXTERN_CPP 233 #define BEGIN_NAMESPACE(name) 234 #define END_NAMESPACE(name) 235 #define BEGIN_ANONYMOUS 236 #define END_ANONYMOUS 252 #ifndef _in // Useful where lower case is more readable than the old IN/OUT ones. 261 #ifndef INOUT // Supplements to classic annotations (when you prefer upper case) 286 extern SECURITY_ATTRIBUTES
DefSec;
292 #define USE_STATIC_SECDESC_INIT 0 326 #ifndef CSTR // Transmutable (TCHAR) strings 339 #define PCUCHAR const unsigned char* 342 #ifndef ASTR // Explicit ANSI (or MultiByte) strings 353 #ifndef MBSTR // Explicit MBCS strings (sometimes unsigned is mandated) 359 #define MBCSTR PCUCHAR 364 #ifndef WSTR // Explicit UNICODE strings 370 #define WCSTR LPCWSTR 376 #define TCHAR_SIZE sizeof(TCHAR) 379 #define WCHAR_SIZE sizeof(WCHAR) 382 #define _TBYTES( cch ) (cch * TCHAR_SIZE) 383 #define _WBYTES( cch ) (cch * WCHAR_SIZE) 386 #define PTR_SIZE sizeof(void*) 399 #if defined(_MSC_VER) 402 #elif defined(__GNUC__) 406 #error("Please add INT64/UINT64 declarations for Your compiler..") 423 #ifndef NO_CONST_HANDLES 429 #ifndef __bool_defined 431 typedef enum {
false,
true } bool;
440 #define __bool_defined 1 452 typedef WORD *PTWOCC, TWOCC; 453 #define MK_TWOCC( S2 ) (*(PTWOCC)(S2)) 461 typedef DWORD *PFOURCC, FOURCC; 462 #define MK_FOURCC( S4 ) (*(PFOURCC)(S4)) 470 typedef QWORD *PEIGHTCC, EIGHTCC; 471 #define MK_EIGHTCC( S8 ) (*(PEIGHTCC)(S8)) 486 typedef const SIZE *LPCSIZE; 492 #pragma pack( push,2 ) 494 typedef struct _RECTS { 504 typedef struct _SIZES { 522 #if defined(_M_MRX000) || defined(_M_ALPHA) || defined(_M_PPC) || defined(_M_IA64) || defined(_M_AMD64) 523 #define CPU_NEED_ALIGNMENT 1 // A good thing to know, lest things go sour. 525 #define CPU_NEED_ALIGNMENT 0 528 #if defined(_MSC_VER) //........................................................ 530 #define __thread_local __declspec(thread) // In thread-local storage. 531 #define __end_except // Part of GCC SEH kludge. 532 #define __end_finally // Part of GCC SEH kludge. 534 #define HAVE_STRUCTURED_EH // Have proper SEH support, a.k.a. Borland SEH. 535 #define HAVE_SAFE_CSTRINGS // The nnn_s'es. 542 #define HAVE_BSWAP16_INTRINSIC 0 543 #define HAVE_BSWAP32_INTRINSIC 0 544 #define HAVE_BSWAP64_INTRINSIC 0 545 #define HAVE_ASM_BSWAP 1 547 #define HAVE_BSWAP16_INTRINSIC 1 548 #define HAVE_BSWAP32_INTRINSIC 1 549 #define HAVE_BSWAP64_INTRINSIC 1 552 #elif defined(__GNUC__) //...................................................... 554 #define __thread_local __thread 556 #ifndef __forceinline 557 #define __forceinline __attribute__((always_inline)) 561 #define HAVE_SAFE_CSTRINGS 564 #ifndef HAVE_SAFE_CSTRINGS 565 #pragma message("[uLib] ALERT: Circumventig safe string functions :ALERT") 566 #define wcscpy_s( dst,dim,src ) wcscpy( dst,src ) 567 #define strcpy_s( dst,dim,src ) strcpy( dst,src ) 568 #define _tcscpy_s( dst,dim,src ) _tcscpy( dst,src ) 569 #define _tcsncpy_s( dst,dim,src,n ) _tcsncpy( dst,src,n ) 570 #define _stprintf_s( dst,dim,fmt, ... ) _stprintf( dst,fmt, __VA_ARGS__ ) 571 #define _vstprintf_s( dst,dim,fmt,va ) _vstprintf( dst,fmt,va ) 572 #define vsprintf_s( dst,dim,fmt,va ) vsprintf( dst,fmt,va ) 573 #define vswprintf_s( dst,dim,fmt,va ) vswprintf( dst,fmt,va ) 574 #define swprintf_s( dst,dim,fmt, ... ) swprintf( dst,fmt, __VA_ARGS__ ) 579 #define _ASSERTE(e) assert(e) 582 #undef __STRICT_ANSI__ // MinGW tucks _strnicmp away behind STRICT_ANSI 584 #if ((__GNUC__ >= 4) && (__GNUC_MINOR__ >= 8)) 585 #define HAVE_BSWAP16_INTRINSIC 1 586 #define _byteswap_ushort __builtin_bswap16 588 #define HAVE_BSWAP16_INTRINSIC 0 589 #define _byteswap_ushort _bswap16 590 #define __builtin_bswap16 _bswap16 593 #define HAVE_BSWAP32_INTRINSIC 1 594 #define HAVE_BSWAP64_INTRINSIC 1 595 #define _byteswap_ulong __builtin_bswap32 596 #define _byteswap_uint64 __builtin_bswap64 598 #define HAVE_ASM_BSWAP 0 // I haven't written a GCC _bswapXX asm implementation 608 #if 0 // Transparently (well.. almost) use GCC kludge for SEH. 613 #include <pseh/pseh3.h> 615 #define HAVE_STRUCTURED_EH 617 #define __try _SEH3_TRY 618 #define __except _SEH3_EXCEPT 619 #define __finally _SEH3_FINALLY 620 #define __end_except _SEH3_END // GCC kludge 621 #define __end_finally _SEH3_END // -"- 622 #define __leave _SEH3_LEAVE 624 #ifndef GetExceptionCode 625 #define GetExceptionCode() _exception_code() 627 #ifndef GetExceptionInformation 628 #define GetExceptionInformation() (PEXCEPTION_POINTERS)_exception_info() 631 #undef HAVE_STRUCTURED_EH 635 #error("Please add __thread_local definition for your compiler") 645 #if defined(HAVE_STRUCTURED_EH) 646 #define __except_execute __except( EXCEPTION_EXECUTE_HANDLER ) 654 #define FACILITY_ULIB 0x800 // Sets bit 27 662 #define MAKE_SOFTEXCEPTION( Severity, Facility, Exception ) \ 664 ((Severity) << 0) | \ 667 ((Facility) << 16) | \ 677 #define __assembly __declspec(naked) 688 #if HAVE_BSWAP16_INTRINSIC 689 #define _bswap16 _byteswap_ushort 691 UINT16 __cdecl _bswap16( UINT16 Val ); 692 #define _byteswap_ushort _bswap16 696 #if HAVE_BSWAP32_INTRINSIC 697 #define _bswap32 _byteswap_ulong 699 UINT32 __cdecl _bswap32( UINT32 Val ); 700 #define _byteswap_ulong _bswap32 704 #if HAVE_BSWAP64_INTRINSIC 706 #define _bswap64 _byteswap_uint64 709 UINT64 __cdecl _bswap64( UINT64 Val ); 710 #define _byteswap_uint64 _bswap64 752 #define DISABLE_BOOLCAST_WARNING __pragma( warning( disable: 4800 )) 753 #define REENABLE_BOOLCAST_WARNING __pragma( warning( default: 4800 )) 755 #define DISABLE_BOOLCAST_WARNING 756 #define REENABLE_BOOLCAST_WARNING 759 #ifndef __bool_cast_defined 770 #define __bool_cast_defined 780 #define IS_WINNT(getver) (DWORD( getver ) < 0x80000000) 781 #define MAJ_WINVER(getver) LOBYTE(LOWORD( getver )) 782 #define MIN_WINVER(getver) HIBYTE(LOWORD( getver )) 824 long map(
long x,
long xMin,
long xMax,
long toMin,
long toMax );
827 long constrain(
long x,
long lo,
long hi );
830 short random(
short iMin,
short iMax );
836 long runningAvg(
long Avg,
long X,
unsigned int Period );
855 long double _root(
long double base,
long double X );
859 long double _log(
long double base,
long double X );
863 double _round(
double x,
unsigned char ndecimals );
875 long double EngUnits(
long double Value,
int* pExpo );
890 template <
typename T>
891 T
map_( T x, T xMin, T xMax, T toMin, T toMax )
893 return toMin + ((x - xMin) * (toMax - toMin)) / (xMax - xMin);
900 template <
typename T>
903 return (x > hi ? hi : (x < lo ? lo : x));
906 template <
typename T> T
min_( T a, T b ) {
return (a < b ? a : b); }
907 template <
typename T> T
max_( T a, T b ) {
return (a > b ? a : b); }
911 template <
typename T>
914 return (X + (Avg * ((T)Period-1))) / (T)Period;
919 template <
typename T>
920 T
mulDiv_(
const T X,
const T Mu,
const T Dv )
925 #endif//def __cplusplus 927 #ifdef NOMINMAX // Located here, for proximity with min_/max_ above. 933 #define min(a,b) ((a)<(b)?(a):(b)) 936 #define max(a,b) ((a)>(b)?(a):(b)) 949 #define dimof(x) (sizeof(x)/sizeof(x[0])) 956 #define offsetof( stype,field ) ((size_t)&reinterpret_cast<char const volatile&>((((stype*)0)->field))) 958 #define offsetof( stype,field ) ((size_t)&(((stype*)0)->field)) 962 #define structof( faddr,stype,field ) ((stype*)( (ULONG_PTR)(faddr) - (ULONG_PTR)offsetof(stype,field))) 963 #define fieldsize( stype,field ) sizeof(((stype*)0)->field) 965 #define struct_subsize( stype,field ) (offsetof( stype,field ) + fieldsize( stype,field )) 969 #define LITERAL(type,x) (*((type*)&(x))) 970 #define UNUSED(x) ((void)&(x)) 971 #define ZEROMEM(var) memset( &(var),0,sizeof(var) ) 972 #define YIELD() SleepEx( 0, true ) 974 #define SWAP_INPLACE(a,b) a ^= b ^= a ^= b 979 #define ROUND_UP(x,chunk) (((UINT_PTR(x) + (chunk) - 1) / (chunk)) * (chunk)) 980 #define ROUND_DOWN(x,chunk) ((UINT_PTR(x) / (chunk)) * (chunk)) 984 #define ROUND_UP2(x,chunk) ((UINT_PTR(x)+((chunk)-1)) & -(chunk)) 985 #define ROUND_DOWN2(x,chunk) (UINT_PTR(x) & -(chunk)) 989 #define ALIGN_VALUE(x,aln) (INT_PTR(x) + ((~INT_PTR(x)+1) & (aln-1))) 992 #define DELETE_ARRAY(A) if (A) { delete[] (A); (A) = NULL; } 993 #define DELETE_OBJ(Obj) if (Obj) { delete (Obj); (Obj) = NULL; } 994 #define RELEASE_OBJ(Ifc) if (Ifc) { (Ifc)->Release(); (Ifc) = NULL; } 995 #define COMTASK_FREE(Obj) if (Obj) { CoTaskMemFree( PVOID(Obj) ); (Obj) = NULL; } 997 #define _BAD_HANDLE INVALID_HANDLE_VALUE 1004 #define BAD_HANDLE(hnd) ((hnd) == _BAD_HANDLE || !(hnd)) 1005 #define GOOD_HANDLE(hnd) ((hnd) != _BAD_HANDLE && (hnd) != NULL) 1006 #define CHK_HANDLE(hnd) ((hnd) == _BAD_HANDLE ? NULL : (hnd)) 1009 #define _BAD_ATTRIBUTE INVALID_FILE_ATTRIBUTES 1015 #define BITS_SET(bits,x) (((x) & (bits)) == (bits)) 1016 #define BITS_RST(bits,x) (((x) & (bits)) == 0) 1017 #define HAVE_BITS(var,mask) BITS_SET( (mask),(var) ) 1019 #define ALL_BITS 0xFFFFFFFF 1020 #define ALL_BITS64 0xFFFFFFFFFFFFFFFF 1022 #define _BV(bitNr) (UINT_PTR(1) << (bitNr)) 1026 #define PP_NSTR(x) PP_NSTR_(x) 1027 #define PP_NSTR_(x) #x 1031 #define PP_STR(x) _T(#x) 1034 #ifndef CCSTR_LENGTH 1035 #define CCSTR_LENGTH( s ) (dimof(s) - sizeof(*s)) 1039 #define CCH_AVAIL( buf,pz ) (dimof(buf) - (CSTR(pz) - CSTR(buf))) 1043 #define _ODS( s ) OutputDebugString( s ) 1044 #define _ODSA( s ) OutputDebugStringA( s ) 1045 #define _ODSW( s ) OutputDebugStringW( s ) 1055 #define _CASSERT(id,expr) typedef char __cassert_##id[ (expr) ? +1 : -1 ] 1057 #define _CASSERT(id,expr) 1077 #ifndef OFFSETOFCLASS // Borrowed from ShLwApi, who borrowed it from ATL. 1078 #define OFFSETOFCLASS( base, derived ) \ 1079 ((DWORD)(DWORD_PTR) (STATIC_CAST(base*)( (derived*)8 )) - 8) 1097 #define WINDOWPROC LRESULT CALLBACK 1098 #define DIALOGPROC DRESULT CALLBACK 1101 #define DRESULT INT_PTR 1103 #define DRESULT BOOL 1108 #define WINHOOKPROC LRESULT CALLBACK 1113 #define THREADPROC DWORD WINAPI 1117 #define EXITPROC void __cdecl 1127 #define GENERIC_RW (GENERIC_READ | GENERIC_WRITE) 1128 #define FILE_SHARE_NONE 0 1129 #define FILE_SHARE_RW (FILE_SHARE_READ | FILE_SHARE_WRITE) 1130 #define FILE_SHARE_ALL (FILE_SHARE_RW| FILE_SHARE_DELETE) 1132 #define FILE_MAP_RW (FILE_MAP_READ | FILE_MAP_WRITE) 1133 #define KEY_RW (KEY_READ | KEY_WRITE) 1135 #ifndef GENERIC_ACCESS 1136 #define GENERIC_ACCESS (GENERIC_RW | GENERIC_EXECUTE | GENERIC_ALL) 1139 #ifndef ALL_SECURITY_INFORMATION 1140 #define ALL_SECURITY_INFORMATION \ 1141 ( OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION \ 1142 | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION ) 1146 #define WINSTA_ALL \ 1147 ( WINSTA_ENUMDESKTOPS | WINSTA_READATTRIBUTES | WINSTA_ACCESSCLIPBOARD \ 1148 | WINSTA_CREATEDESKTOP | WINSTA_WRITEATTRIBUTES | WINSTA_ACCESSGLOBALATOMS \ 1149 | WINSTA_EXITWINDOWS | WINSTA_ENUMERATE | WINSTA_READSCREEN \ 1150 | STANDARD_RIGHTS_REQUIRED ) 1154 #define DESKTOP_ALL \ 1155 ( DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU \ 1156 | DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK \ 1157 | DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP \ 1158 | STANDARD_RIGHTS_REQUIRED ) 1162 #ifndef NO_UNDOC_WINMSG 1170 #ifndef WM_FULLSCREEN 1171 #define WM_FULLSCREEN 0x003A 1172 #define WM_CLIENTSHUTDOWN 0x003B 1173 #define WM_DDEMLEVENT 0x003C 1175 #ifndef WM_CALCSCROLL 1176 #define WM_CALCSCROLL 0x003F 1178 #ifndef WM_SYNCPAINT 1179 #define WM_SYNCPAINT 0x0088 1181 #ifndef WM_UAHDESTROYWINDOW // Aero, undocumented 1182 #define WM_UAHDESTROYWINDOW 0x0090 1183 #define WM_UAHDRAWMENU 0x0091 1184 #define WM_UAHDRAWMENUITEM 0x0092 1185 #define WM_UAHINITMENU 0x0093 1186 #define WM_UAHMEASUREMENUITEM 0x0094 1187 #define WM_UAHNCPAINTMENUPOPUP 0x0095 1188 #define WM_UAHUPDATE 0x0096 1190 #ifndef WM_NCUAHDRAWCAPTION // Aero, undocumented 1191 #define WM_NCUAHDRAWCAPTION 0x00AE 1192 #define WM_NCUAHDRAWFRAME 0x00AF 1195 #define EM_MSGMAX 0x00DA 1198 #define WM_SYSTIMER 0x0118 1200 #ifndef WM_GETISHELLBROWSER // Revealed by Ivo Beltcher. Thank You. 1201 #define WM_GETISHELLBROWSER (WM_USER+7) 1208 #endif//ndef NO_UNDOC_WINMSG 1216 #define BSLASH _T('\\') 1217 #define SLASH _T('/') 1218 #define SPACE _T(' ') 1219 #define QUOTE _T('"') 1220 #define SQUOTE _T('\'') 1221 #define COLON _T(':') 1222 #define SEMICOLON _T(';') 1223 #define COMMA _T(',') 1224 #define EQUALS _T('=') 1225 #define PERCENT _T('%') 1226 #define HASH _T('#') 1228 #define TAB _T('\t') 1232 #define DOTDOT _T("..") 1233 #define CRLF _T("\r\n") 1234 #define S_BLANK _T("") 1237 #ifndef NO_CONTROL_CHARS 1245 #define _NUL _T('\x00') 1246 // Start of keyboard range 1273 // End of keyboard range 1284 #endif//ndef NO_CONTROL_CHARS 1288 #endif //ndef _uLib_Common_h_incl_
short random(short iMin, short iMax)
long double _log(long double base, long double X)
DWORD __stdcall _rol32(DWORD value, BYTE bits)
long double EngUnits(long double Value, int *pExpo)
T constrain_(T x, T lo, T hi)
T mulDiv_(const T X, const T Mu, const T Dv)
uint64 ipow2x(ushort expo)
WORD __stdcall _ror16(WORD value, BYTE bits)
#define REENABLE_BOOLCAST_WARNING
QWORD __stdcall _ror64(QWORD value, BYTE bits)
bool IsWinVer(BYTE vMajor, BYTE vMinor, bool WinNT=true)
BYTE __stdcall _rol8(BYTE value, BYTE bits)
T map_(T x, T xMin, T xMax, T toMin, T toMax)
BOOL(WINAPI *SysImgList::Shell_GetImageLists)(HIMAGELIST *pimlLarge
bool _isprime(unsigned long X)
void GetWinSystemInfo(SYSTEM_INFO *si)
SECURITY_DESCRIPTOR DefSecDesc
QWORD __stdcall _rol64(QWORD value, BYTE bits)
Build feedback definitions..
long constrain(long x, long lo, long hi)
DWORD __stdcall _ror32(DWORD value, BYTE bits)
long runningAvg(long Avg, long X, unsigned int Period)
SECURITY_ATTRIBUTES DefSec
bool __forceinline bool_cast(BOOL B52)
double _round(double x, unsigned char ndecimals)
long map(long x, long xMin, long xMax, long toMin, long toMax)
T runningAvg_(T Avg, T X, unsigned int Period)
long double _root(long double base, long double X)
WORD __stdcall _rol16(WORD value, BYTE bits)
unsigned __int64 * PUINT64
#define DISABLE_BOOLCAST_WARNING
BYTE __stdcall _ror8(BYTE value, BYTE bits)