uLib  User mode C/C++ extended API library for Win32 programmers.
Common.h
Go to the documentation of this file.
1 //==============================================================================
2 // Project uLib - User mode utility library.
3 //==============================================================================
10 //==============================================================================
54 //==============================================================================
55 
56 #ifndef _uLib_Common_h_incl_
57 #define _uLib_Common_h_incl_
58 
59 // PONDER: Make all functions explicitly WINAPI, unless they have var-args,
60 // in which case they should be explicitly __cdecl, or they have been
61 // designated __fastcall for some reason.
62 
63 // TODO: Make sure all C functions are EXTERN_C,
64 // unless they are overloaded and hence unavailable in C.
65 
66 //==============================================================================
76 //==============================================================================
78 
80 //==----------------------------------------------------------------------------
81 
82 #ifdef NT4_COMP
83  #undef WINVER
84  #define WINVER 0x400
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.")
88  #endif
89 #endif
90 
91 #ifndef WINVER
92  // x601 == Win7
93  // x600 == Vista
94  // x501 == WinXP
95  // x502 == Svr2003 & ReactOS
96  //#define WINVER 0x502
97  #define WINVER 0x600
98 #endif
99 #if (WINVER == 0x400)
100 #define NO_VERSION_WARNING // Legacy: For my patched NT4 SDK headers (not for you).
101 #endif
102 
103 #ifndef _WIN32_IE
104 #define _WIN32_IE WINVER
105 #endif
106 #ifndef _WIN32_WINNT
107 #define _WIN32_WINNT WINVER // IsDebuggerPresent (& c...)
108 #endif
109 #ifndef _WIN32_WINDOWS
110 #define _WIN32_WINDOWS WINVER // -"-
111 #endif
112 #ifndef WIN32_LEAN_AND_MEAN
113 #define WIN32_LEAN_AND_MEAN 1 // Exclude rarely-used stuff from Windows headers
114 #endif
115 
116 #ifndef STRICT
117 #define STRICT 1
118 #endif
119 
120 #define TYPEDEF_STRINGS 1 // Use 0 to make them #defines instead of typedefs.
121 
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")
125  // NOTA BENE: struct TEB, in <ndk/peb_teb.h>, is currently broken in x64
126  // when _WIN32_WINNT < 0x0502 (i.e. NTDDI_VERSION < NTDDI_WS03).
127  #define NO_NDK_FILES // Exclude the NDK.
128  #endif
129 #endif
130 
131 #ifndef NO_ULIB_BLDINFO
132 #include "BldInfo.h" // Programmer feedback
133 #else
134  #define BLD_INFO(str)
135 #endif
136 
137 #ifdef __GNUC__
138 //#define NO_NTSTATUS // Comment out if you have it
139 //#define NO_WINTERNAL // -"-
140 #endif
141 
142 //==----------------------------------------------------------------------------
144 
145 #ifndef NO_NTSTATUS
146  #pragma warning( disable: 4005 )
147  // Squelch W4005 "re-definitions" in NtStatus.h visavi WinNT.h,
148  // in case Windows.h et al was previosly included in the build process.
149  #include <NtStatus.h>
150  #define WIN32_NO_STATUS // Prevent subsequent #define STATUS_xx clashes.
151 
153  #ifndef NT_SUCCESS
154  #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
155  #endif
156 
157  #ifndef NT_ERROR
158  #define NT_ERROR(Status) (((ULONG)(Status) >> 30) == 3)
159  #endif
160 #endif
161 
162 #include <Windows.h>
163 #include <WindowsX.h> /* Useful control class macros */
164 //#include <WinBase.h>
165 #include <WinError.h>
166 #include <WinNT.h> /* For IDE convenience (PopMenu: Open Document <...>) */
167 #include <Limits.h>
168 #ifndef NO_MATHRTL
169  #include <Float.h>
170  #define _USE_MATH_DEFINES
171  #include <Math.h>
173  #include <ErrNo.h>
174 #endif
175 #include <StdDef.h>
176 #include <StdLib.h>
177 #include <StdIo.h>
178 #include <ConIo.h>
179 #include <CType.h>
180 #include <TChar.h>
181 #include <Lm.h> /* NetShareGetInfo */
182 #include <RichEdit.h>
183 #include <CommCtrl.h>
184 #include <CommDlg.h>
185 #include <CdErr.h>
186 #include <ShlObj.h>
187 #include <ShellApi.h>
188 #include <ShLwApi.h>
189 #include <OleCtl.h> /* OleLoadPicture */
190 #include <PsApi.h>
191 #ifdef _MSC_VER
192  #include <CrtDbg.h>
193 #endif
194 #ifndef NO_WINTERNAL
195  #ifndef NO_NDK_FILES
196  #include <uLib/NtTypes.h> /* Requires <ndk/Nnn.h> access to the NDK. */
197  #else
198  #include <WInternl.h> /* NT internals we may officially use in user mode. */
199  typedef NTSTATUS* PNTSTATUS; /* Forgotten in WInternl.h */
200  #endif
201  #ifndef _NTSECAPI_
202  // As a cheat, define _NTDEF_ before including NtSecApi.h, to avoid
203  // UNICODE_STRING clash. It's normally defined in NtDef.h, which can't
204  // be coerced to coexist with WinNT.h et alumni in user mode.
206  #define _NTDEF_
207  #include <NtSecApi.h> // These LSA can coexist with user mode SDK stuff.
210  #undef _NTDEF_
211  #endif//ndef _NTSECAPI_
213 #else
214  typedef LONG NTSTATUS, *PNTSTATUS;
215 #endif
216 
217 //==----------------------------------------------------------------------------
218 
219 #ifdef __cplusplus
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
230  #define END_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
237 #endif
238 
239 #ifdef __cplusplus
240  #define DEF_(x) = x
241 #else
242  #define DEF_(x) /* C doesn't have that feature */
243 #endif
244 
245 //==----------------------------------------------------------------------------
249 //==----------------------------------------------------------------------------
251 
252 #ifndef _in // Useful where lower case is more readable than the old IN/OUT ones.
253  #define _in
254  #define _out
255  #define _inout
256  #define _opt
257  #define _optin
258  #define _optout
259  #define _optio
260 #endif
261 #ifndef INOUT // Supplements to classic annotations (when you prefer upper case)
262  #define INOUT
263  #define OPTIN
264  #define OPTOUT
265  #define OPTIO
266 #endif
267 
269 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
276 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278 
279 extern HINSTANCE hInstance;
280 extern HWND hMainWnd;
281 extern HWND hCurrDlg;
282 extern HACCEL hMainAcc;
283 extern HWND hMdiClient;
284 
286 extern SECURITY_ATTRIBUTES DefSec; //= { sizeof(DefSec), NULL, TRUE };
287 
289 extern SECURITY_DESCRIPTOR DefSecDesc; //= { 0,0,0,NULL,NULL,NULL,NULL };
290 
292 #define USE_STATIC_SECDESC_INIT 0
293 
301 
304 //==----------------------------------------------------------------------------
307 //==----------------------------------------------------------------------------
309 
310 //#ifdef WANT_LOCASE_TYPES
311  #include <uLib/Types.h>
312 //#endif
313 
314 #ifdef __DOXYGEN__
315 typedef bool bool_t;
316 #endif
319 
320 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
323 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
325 
326 #ifndef CSTR // Transmutable (TCHAR) strings
327  //rescinded//#define PSTR LPTSTR // Changable ptr to changable char.
328  #define TSTR LPTSTR
329  #define CSTR LPCTSTR
330 #endif
331 
332 #ifndef CCSTR
333 typedef const CSTR CCSTR;
336 #endif
337 
338 #ifndef PCUCHAR
339  #define PCUCHAR const unsigned char*
340 #endif
341 
342 #ifndef ASTR // Explicit ANSI (or MultiByte) strings
343 #if TYPEDEF_STRINGS
344 typedef char* ASTR;
345 typedef const char* ACSTR;
346 #else
347  #define ASTR LPSTR
348  #define ACSTR LPCSTR
349 #endif
350 typedef const ACSTR CCASTR;
351 #endif
352 
353 #ifndef MBSTR // Explicit MBCS strings (sometimes unsigned is mandated)
354 #if TYPEDEF_STRINGS
355 typedef UCHAR* MBSTR;
356 typedef const UCHAR* MBCSTR;
357 #else
358  #define MBSTR PUCHAR
359  #define MBCSTR PCUCHAR
360 #endif
361 typedef const MBCSTR CCMBSTR;
362 #endif
363 
364 #ifndef WSTR // Explicit UNICODE strings
365 #if TYPEDEF_STRINGS
366 typedef wchar_t* WSTR;
367 typedef const wchar_t* WCSTR;
368 #else
369  #define WSTR LPWSTR
370  #define WCSTR LPCWSTR
371 #endif
372 typedef const WCSTR CCWSTR;
373 #endif
374 
375 #ifndef TCHAR_SIZE
376  #define TCHAR_SIZE sizeof(TCHAR)
377 #endif
378 #ifndef WCHAR_SIZE
379  #define WCHAR_SIZE sizeof(WCHAR)
380 #endif
381 
382 #define _TBYTES( cch ) (cch * TCHAR_SIZE)
383 #define _WBYTES( cch ) (cch * WCHAR_SIZE)
384 
385 #ifndef PTR_SIZE
386  #define PTR_SIZE sizeof(void*)
387 #endif
388 
390 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
393 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
395 
396 // Note: The oddball reverse order of some type id declarations are due to doxygen's parser.
397 
398 #ifndef UINT64
399 #if defined(_MSC_VER)
400  typedef unsigned __int64 UINT64, *PUINT64;
401  typedef signed __int64 INT64, *PINT64;
402 #elif defined(__GNUC__)
403  typedef unsigned long long UINT64, *PUINT64;
404  typedef signed long long INT64, *PINT64;
405 #else
406  #error("Please add INT64/UINT64 declarations for Your compiler..")
407  //.. or add your compiler to one of the previous clauses, if applicable.
408 #endif
409 #endif
410 
411 #ifndef BYTE
412 typedef unsigned char BYTE, *PBYTE;
413 typedef unsigned short WORD, *PWORD;
414 typedef unsigned long DWORD, *PDWORD;
415 #endif
416 
417 #ifndef QWORD
418 typedef UINT64 QWORD, *PQWORD;
419 #endif
420 
421 // Not really integers, but treated as such, and I don't want yet another group.
422 
423 #ifndef NO_CONST_HANDLES
424 typedef const HANDLE CHANDLE;
425 typedef const HWND CHWND;
426 #endif
427 
428 #ifndef __cplusplus
429 #ifndef __bool_defined
430  #if 0
431  typedef enum { false, true } bool;
432  //typedef enum : char { false, true } bool; //<< Alas.. Sizing requires C++
433  #else
434  typedef char bool;
435  #define true (1==1)
438  #define false (1==0)
439  #endif
440  #define __bool_defined 1
441 #endif
442 #endif
443 
445 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
448 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
450 
451 #ifndef TWOCC
452 typedef WORD *PTWOCC, TWOCC;
453 #define MK_TWOCC( S2 ) (*(PTWOCC)(S2))
458 #endif
459 
460 #ifndef FOURCC
461 typedef DWORD *PFOURCC, FOURCC;
462 #define MK_FOURCC( S4 ) (*(PFOURCC)(S4))
467 #endif
468 
469 #ifndef EIGHTCC
470 typedef QWORD *PEIGHTCC, EIGHTCC;
471 #define MK_EIGHTCC( S8 ) (*(PEIGHTCC)(S8))
476 #endif
477 
479 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
482 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
484 
485 #ifndef LPCSIZE
486  typedef const SIZE *LPCSIZE;
487 #endif
488 
489 // The quaint "plural" naming of the following two structs
490 // are dictated by compliance with the naming of POINTS (small point).
491 
492 #pragma pack( push,2 )
493 #ifndef RECTS
494 typedef struct _RECTS {
495  SHORT left;
496  SHORT top;
497  SHORT right;
498  SHORT bottom;
499 } *PRECTS, *LPRECTS, RECTS;
500 typedef const RECTS *LPCRECTS;
501 #endif
502 
503 #ifndef SIZES
504 typedef struct _SIZES {
505  SHORT cx;
506  SHORT cy;
507 } *PSIZES, *LPSIZES, SIZES;
508 typedef const SIZES *LPCSIZES;
509 #endif
510 #pragma pack( pop )
511 
514 //==----------------------------------------------------------------------------
515 // compat Compiler/RTL Compatibility Issues
516 //==----------------------------------------------------------------------------
518 
519 // PONDER: Migrate compatibility issues and other build options to separate file.
520 // e.g. #include <uLib/BuildCfg.h>
521 
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.
524 #else
525  #define CPU_NEED_ALIGNMENT 0
526 #endif
527 
528 #if defined(_MSC_VER) //........................................................
529 
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.
533 
534  #define HAVE_STRUCTURED_EH // Have proper SEH support, a.k.a. Borland SEH.
535  #define HAVE_SAFE_CSTRINGS // The nnn_s'es.
536  //#define HAVE_SCALECOLOR_ASM // FIXME: ScaleColorRef (in _gradcolor.asm)
537 
538  // Debug versions of MSVC byte swappers are very inefficient,
539  // so I rolled my own in _bswap.asm.
540 
541  #ifdef _DEBUG
542  #define HAVE_BSWAP16_INTRINSIC 0
543  #define HAVE_BSWAP32_INTRINSIC 0
544  #define HAVE_BSWAP64_INTRINSIC 0
545  #define HAVE_ASM_BSWAP 1
546  #else
547  #define HAVE_BSWAP16_INTRINSIC 1
548  #define HAVE_BSWAP32_INTRINSIC 1
549  #define HAVE_BSWAP64_INTRINSIC 1
550  #endif
551 
552 #elif defined(__GNUC__) //......................................................
553 
554  #define __thread_local __thread /* version specific? */
555 
556  #ifndef __forceinline
557  #define __forceinline __attribute__((always_inline))
558  #endif
559 
560  #if (__GNUC__ >= 5)
561  #define HAVE_SAFE_CSTRINGS
562  #endif
563 
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__ )
575  #endif
576 
577  #ifndef _ASSERTE
578  #include <assert.h>
579  #define _ASSERTE(e) assert(e)
580  #endif
581 
582  #undef __STRICT_ANSI__ // MinGW tucks _strnicmp away behind STRICT_ANSI
583 
584  #if ((__GNUC__ >= 4) && (__GNUC_MINOR__ >= 8))
585  #define HAVE_BSWAP16_INTRINSIC 1
586  #define _byteswap_ushort __builtin_bswap16
587  #else
588  #define HAVE_BSWAP16_INTRINSIC 0
589  #define _byteswap_ushort _bswap16
590  #define __builtin_bswap16 _bswap16
591  #endif
592 
593  #define HAVE_BSWAP32_INTRINSIC 1
594  #define HAVE_BSWAP64_INTRINSIC 1
595  #define _byteswap_ulong __builtin_bswap32
596  #define _byteswap_uint64 __builtin_bswap64
597 
598  #define HAVE_ASM_BSWAP 0 // I haven't written a GCC _bswapXX asm implementation
599 
600  // In case you really don't know, SEH is a method (invented by Borland) to
601  // vector CPU fault interrupts to high level C++ code in user mode, and has
602  // absolutely nothing to do with C++ try/catch (which is little more than
603  // a compiler supported setjmp/longjmp user mode mechanism).
604  //
605  // GCC folks: Borland's previous patent on SEH has now been rescinded,
606  // so you are free to include and improve C.Hornstrups implementation!
607 
608  #if 0 // Transparently (well.. almost) use GCC kludge for SEH.
609  // Sorry: So far, I can't get PSEH or other to work with any of my GCC's.
610  // Until I (or some contributor) can, SEH is only available with MSVC or
611  // Borland builds, which means f.ex the kill-safe Thread is not available
612  // in GCC builds. PONDER: Can clang get it right ?
613  #include <pseh/pseh3.h>
614  //#include <excpt.h>
615  #define HAVE_STRUCTURED_EH
616 
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
623 
624  #ifndef GetExceptionCode
625  #define GetExceptionCode() _exception_code()
626  #endif
627  #ifndef GetExceptionInformation
628  #define GetExceptionInformation() (PEXCEPTION_POINTERS)_exception_info()
629  #endif
630  #else
631  #undef HAVE_STRUCTURED_EH
632  #endif
633 
634 #else
635  #error("Please add __thread_local definition for your compiler")
636 #endif
637 
639 //==----------------------------------------------------------------------------
642 //==----------------------------------------------------------------------------
644 
645 #if defined(HAVE_STRUCTURED_EH)
646  #define __except_execute __except( EXCEPTION_EXECUTE_HANDLER )
648 #endif
649 
650 // NOTE: These two are not really dependent on SEH availability.
651 // They might be used with RaiseException() as well.
652 
654 #define FACILITY_ULIB 0x800 // Sets bit 27
655 
661 
662 #define MAKE_SOFTEXCEPTION( Severity, Facility, Exception ) \
663  ((DWORD) ( \
664  /* Severity code */ ((Severity) << 0) | \
665  /* MS(0) or Cust(1) */ (1 << 29) | \
666  /* Reserved(0) */ (0 << 28) | \
667  /* Facility code */ ((Facility) << 16) | \
668  /* Exception code */ ((Exception) << 0)))
669 
671 //==----------------------------------------------------------------------------
672 // Boiler plate stuff and assembly language speed demons
673 //==----------------------------------------------------------------------------
674 
675 #ifndef _WIN64
676  #ifndef __assembly
677  #define __assembly __declspec(naked)
678  #endif
679 #endif
680 
682 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
685 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
687 
688 #if HAVE_BSWAP16_INTRINSIC
689  #define _bswap16 _byteswap_ushort
690 #else
691  UINT16 __cdecl _bswap16( UINT16 Val );
692  #define _byteswap_ushort _bswap16
693  //BLD_INFO("[uLib] Using custom _bswap16 implementation.")
694 #endif
695 
696 #if HAVE_BSWAP32_INTRINSIC
697  #define _bswap32 _byteswap_ulong
698 #else
699  UINT32 __cdecl _bswap32( UINT32 Val );
700  #define _byteswap_ulong _bswap32
701  //BLD_INFO("[uLib] Using custom _bswap32 implementation.")
702 #endif
703 
704 #if HAVE_BSWAP64_INTRINSIC
705  #ifndef _bswap64
706  #define _bswap64 _byteswap_uint64
707  #endif
708 #else
709  UINT64 __cdecl _bswap64( UINT64 Val );
710  #define _byteswap_uint64 _bswap64
711  //BLD_INFO("[uLib] Using custom _bswap64 implementation.")
712 #endif
713 
715 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
718 //+- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
720 #ifdef _MSC_VER
721 
722 // Rotate left.. (_asmop.asm)
723 
724 BYTE __stdcall _rol8( BYTE value, BYTE bits );
725 WORD __stdcall _rol16( WORD value, BYTE bits );
726 DWORD __stdcall _rol32( DWORD value, BYTE bits );
727 QWORD __stdcall _rol64( QWORD value, BYTE bits );
729 
730 // Rotate right.. (_asmop.asm)
731 
732 BYTE __stdcall _ror8( BYTE value, BYTE bits );
733 WORD __stdcall _ror16( WORD value, BYTE bits );
734 DWORD __stdcall _ror32( DWORD value, BYTE bits );
735 QWORD __stdcall _ror64( QWORD value, BYTE bits );
737 
738 #else
739 
740  // Sorry; No GCC/GAS/NASM implementation, nor any C fallback yet...
741  // <intrin.h> has some rotators...
742 
743 #endif//def _MSC_VER
746 
747 //==----------------------------------------------------------------------------
748 // bool_cast
749 //==----------------------------------------------------------------------------
750 
751 #ifdef _MSC_VER
752  #define DISABLE_BOOLCAST_WARNING __pragma( warning( disable: 4800 ))
753  #define REENABLE_BOOLCAST_WARNING __pragma( warning( default: 4800 ))
754 #else
755  #define DISABLE_BOOLCAST_WARNING
756  #define REENABLE_BOOLCAST_WARNING
757 #endif
758 
759 #ifndef __bool_cast_defined
760  DISABLE_BOOLCAST_WARNING // Warning 4800 is totally unwarranted here.
761 
766 
767  bool __forceinline bool_cast( BOOL B52 ) { return (bool) B52; }
768  //bool __forceinline bool_cast( BOOL B52 ) { return !!B52; } //<-- PONDER this cute idea..
769 
770  #define __bool_cast_defined
772 #endif
773 
774 //==----------------------------------------------------------------------------
777 //==----------------------------------------------------------------------------
779 
780 #define IS_WINNT(getver) (DWORD( getver ) < 0x80000000)
781 #define MAJ_WINVER(getver) LOBYTE(LOWORD( getver ))
782 #define MIN_WINVER(getver) HIBYTE(LOWORD( getver ))
783 
785 
788 
789 bool IsWinVer( BYTE vMajor, BYTE vMinor, bool WinNT DEF_(true) );
790 
791 bool IsWin7();
792 bool IsWinVista();
793 bool IsWinXP();
794 bool IsWin2000();
795 bool IsWinNT();
796 bool IsWin98();
797 bool IsWin95();
798 
799 bool IsWin64Bit();
800 
803 
804 bool IsWow64();
805 
807 
808 void GetWinSystemInfo( SYSTEM_INFO* si );
809 
811 
813 //==----------------------------------------------------------------------------
819 //==----------------------------------------------------------------------------
821 
823 
824 long map( long x, long xMin, long xMax, long toMin, long toMax );
826 
827 long constrain( long x, long lo, long hi );
829 
830 short random( short iMin, short iMax );
832 
833 void randomize();
835 
836 long runningAvg( long Avg, long X, unsigned int Period );
839 
840 ulong ipow2( ushort expo );
842 uint64 ipow2x( ushort expo );
844 
845 ushort ilog2( ulong X );
847 ushort ilog2x( uint64 X );
848 
849 ushort ilog10( ulong X );
853 ushort ilog10x( uint64 X );
854 
855 long double _root( long double base, long double X );
858 
859 long double _log( long double base, long double X );
862 
863 double _round( double x, unsigned char ndecimals );
869 
870 bool _isprime( unsigned long X );
874 
875 long double EngUnits( long double Value, int* pExpo );
878 // (UtilFunc.cpp)
879 
881 
882 #ifdef __cplusplus
883 
884 // A few function templates. (I normally prefer not to template due to bloat-risk).
885 // Note the disambiguating underscore in each one.
886 
889 
890 template <typename T>
891  T map_( T x, T xMin, T xMax, T toMin, T toMax )
892  {
893  return toMin + ((x - xMin) * (toMax - toMin)) / (xMax - xMin);
894  // I.e: toMin + xRelative * toRange / fromRange
895  // ---> toMin + mulDiv_<T>( x - xMin, toMax - toMin, xMax - xMin );
896  }
897 
899 
900 template <typename T>
901  T constrain_( T x, T lo, T hi )
902  {
903  return (x > hi ? hi : (x < lo ? lo : x));
904  }
905 
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); }
908 
910 
911 template <typename T>
912  T runningAvg_( T Avg, T X, unsigned int Period )
913  {
914  return (X + (Avg * ((T)Period-1))) / (T)Period;
915  }
916 
918 
919 template <typename T>
920  T mulDiv_( const T X, const T Mu, const T Dv )
921  {
922  return X * Mu / Dv;
923  }
924 
925 #endif//def __cplusplus
926 
927 #ifdef NOMINMAX // Located here, for proximity with min_/max_ above.
928  // Let's treat this case a bit more radical than <windef.h>
929  #undef min
930  #undef max
931 #else
932  #ifndef min
933  #define min(a,b) ((a)<(b)?(a):(b))
934  #endif
935  #ifndef max
936  #define max(a,b) ((a)>(b)?(a):(b))
937  #endif
938 #endif
939 
941 //==----------------------------------------------------------------------------
942 // Macros
943 //==----------------------------------------------------------------------------
944 
948 
949 #define dimof(x) (sizeof(x)/sizeof(x[0]))
950 
951 #if 1
952  #ifndef offsetof
953  #ifdef __cplusplus
956  #define offsetof( stype,field ) ((size_t)&reinterpret_cast<char const volatile&>((((stype*)0)->field)))
957  #else
958  #define offsetof( stype,field ) ((size_t)&(((stype*)0)->field))
959  #endif
960  #endif
961  #ifndef structof
962  #define structof( faddr,stype,field ) ((stype*)( (ULONG_PTR)(faddr) - (ULONG_PTR)offsetof(stype,field)))
963  #define fieldsize( stype,field ) sizeof(((stype*)0)->field)
964  #endif
965  #define struct_subsize( stype,field ) (offsetof( stype,field ) + fieldsize( stype,field ))
967 #endif
968 
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 )
973 
974 #define SWAP_INPLACE(a,b) a ^= b ^= a ^= b
975 
976 
978 #ifndef ROUND_UP
979 #define ROUND_UP(x,chunk) (((UINT_PTR(x) + (chunk) - 1) / (chunk)) * (chunk))
980 #define ROUND_DOWN(x,chunk) ((UINT_PTR(x) / (chunk)) * (chunk))
981 #endif
982 
983 #ifndef ROUND_UP2
984 #define ROUND_UP2(x,chunk) ((UINT_PTR(x)+((chunk)-1)) & -(chunk))
985 #define ROUND_DOWN2(x,chunk) (UINT_PTR(x) & -(chunk))
986 #endif
987 
988 #ifndef ALIGN_VALUE
989 #define ALIGN_VALUE(x,aln) (INT_PTR(x) + ((~INT_PTR(x)+1) & (aln-1)))
990 #endif
991 
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; }
996 
997 #define _BAD_HANDLE INVALID_HANDLE_VALUE
998 
999 
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))
1007 
1009 #define _BAD_ATTRIBUTE INVALID_FILE_ATTRIBUTES
1011 
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
1021 #ifndef _BV
1022  #define _BV(bitNr) (UINT_PTR(1) << (bitNr))
1023 #endif
1024 
1025 #ifndef PP_NSTR
1026  #define PP_NSTR(x) PP_NSTR_(x)
1027  #define PP_NSTR_(x) #x
1028 #endif
1029 
1030 #ifndef PP_STR
1031  #define PP_STR(x) _T(#x)
1032 #endif
1033 
1034 #ifndef CCSTR_LENGTH
1035  #define CCSTR_LENGTH( s ) (dimof(s) - sizeof(*s))
1036 #endif
1037 
1038 #ifndef CCH_AVAIL
1039  #define CCH_AVAIL( buf,pz ) (dimof(buf) - (CSTR(pz) - CSTR(buf)))
1040 #endif
1041 
1042 #ifndef _ODS
1043  #define _ODS( s ) OutputDebugString( s )
1044  #define _ODSA( s ) OutputDebugStringA( s )
1045  #define _ODSW( s ) OutputDebugStringW( s )
1046 #endif
1047 
1052 
1053 #ifndef _CASSERT
1054  #if _DEBUG
1055  #define _CASSERT(id,expr) typedef char __cassert_##id[ (expr) ? +1 : -1 ]
1056  #else
1057  #define _CASSERT(id,expr)
1058  #endif
1059 #endif
1060 
1076 
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)
1080 #endif
1081 
1082 //==----------------------------------------------------------------------------
1093 //==----------------------------------------------------------------------------
1095 
1096 #ifndef WINDOWPROC
1097  #define WINDOWPROC LRESULT CALLBACK
1098  #define DIALOGPROC DRESULT CALLBACK
1099  #ifdef _WIN64
1101  #define DRESULT INT_PTR
1102  #else
1103  #define DRESULT BOOL
1104  #endif
1105 #endif
1106 
1107 #ifndef WINHOOKPROC
1108  #define WINHOOKPROC LRESULT CALLBACK
1109  // Full signature: LRESULT (CALLBACK* HOOKPROC)( int code, WPARAM wp, LPARAM lp );
1110 #endif
1111 
1112 #ifndef THREADPROC
1113  #define THREADPROC DWORD WINAPI
1114 #endif
1115 
1116 #ifndef EXITPROC
1117  #define EXITPROC void __cdecl
1118 #endif
1119 
1121 //==----------------------------------------------------------------------------
1124 //==----------------------------------------------------------------------------
1126 
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)
1137 #endif
1138 
1139 #ifndef ALL_SECURITY_INFORMATION
1140 #define ALL_SECURITY_INFORMATION \
1141  ( OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION \
1142  | DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION )
1143 #endif
1144 
1145 #ifndef WINSTA_ALL
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 )
1151 #endif
1152 
1153 #ifndef DESKTOP_ALL
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 )
1159 #endif
1160 
1162 #ifndef NO_UNDOC_WINMSG
1163 //==----------------------------------------------------------------------------
1167 //==----------------------------------------------------------------------------
1169 
1170 #ifndef WM_FULLSCREEN
1171  #define WM_FULLSCREEN 0x003A
1172  #define WM_CLIENTSHUTDOWN 0x003B
1173  #define WM_DDEMLEVENT 0x003C
1174 #endif
1175 #ifndef WM_CALCSCROLL
1176  #define WM_CALCSCROLL 0x003F
1177 #endif
1178 #ifndef WM_SYNCPAINT
1179  #define WM_SYNCPAINT 0x0088
1180 #endif
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
1189 #endif
1190 #ifndef WM_NCUAHDRAWCAPTION // Aero, undocumented
1191  #define WM_NCUAHDRAWCAPTION 0x00AE
1192  #define WM_NCUAHDRAWFRAME 0x00AF
1193 #endif
1194 #ifndef EM_MSGMAX
1195  #define EM_MSGMAX 0x00DA
1196 #endif
1197 #ifndef WM_SYSTIMER
1198  #define WM_SYSTIMER 0x0118
1199 #endif
1200 #ifndef WM_GETISHELLBROWSER // Revealed by Ivo Beltcher. Thank You.
1201  #define WM_GETISHELLBROWSER (WM_USER+7)
1205 #endif
1206 
1208 #endif//ndef NO_UNDOC_WINMSG
1209 //==----------------------------------------------------------------------------
1212 //==----------------------------------------------------------------------------
1214 
1215 #define DOT _T('.')
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('#')
1227 // Common control chars that have C escapes
1228 #define TAB _T('\t')
1229 #define CR _T('\r')
1230 #define LF _T('\n')
1231 // Frequently needed short strings
1232 #define DOTDOT _T("..")
1233 #define CRLF _T("\r\n")
1234 #define S_BLANK _T("")
1237 #ifndef NO_CONTROL_CHARS
1238 //==----------------------------------------------------------------------------
1242 //==----------------------------------------------------------------------------
1244 
1245 #define _NUL _T('\x00')
1246 // Start of keyboard range
1247 #define _SOH '\x01'
1248 #define _STX '\x02'
1249 #define _ETX '\x03'
1250 #define _EOT '\x04'
1251 #define _ENQ '\x05'
1252 #define _ACK '\x06'
1253 #define _BEL '\x07'
1254 #define _BS '\x08'
1255 #define _HT '\x09'
1256 #define _LF '\x0A'
1257 #define _VT '\x0B'
1258 #define _FF '\x0C'
1259 #define _CR '\x0D'
1260 #define _SO '\x0E'
1261 #define _SI '\x0F'
1262 #define _DLE '\x10'
1263 #define _DC1 '\x11'
1264 #define _DC2 '\x12'
1265 #define _DC3 '\x13'
1266 #define _DC4 '\x14'
1267 #define _NAK '\x15'
1268 #define _SYN '\x16'
1269 #define _ETB '\x17'
1270 #define _CAN '\x18'
1271 #define _EM '\x19'
1272 #define _SUB '\x1A'
1273 // End of keyboard range
1274 #define _ESC '\x1B'
1275 #define _FS '\x1C'
1276 #define _GS '\x1D'
1277 #define _RS '\x1E'
1278 #define _US '\x1F'
1280 #define _XON _DC1
1281 #define _XOFF _DC3
1282 #define _EOF _SUB
1284 #endif//ndef NO_CONTROL_CHARS
1286 // end comn ==============================================================================
1288 #endif //ndef _uLib_Common_h_incl_
1289 // EOF
ushort ilog2(ulong X)
Definition: Common.cpp:208
unsigned long DWORD
Definition: Common.h:414
short random(short iMin, short iMax)
Definition: Common.cpp:156
UINT64 * PQWORD
Definition: Common.h:418
bool IsWin7()
Definition: Common.cpp:92
Definition: Common.h:494
Definition: Common.h:504
const char * ACSTR
Definition: Common.h:345
long double _log(long double base, long double X)
Definition: Common.cpp:303
#define CSTR
Definition: Common.h:329
SHORT cy
Definition: Common.h:506
bool IsWin98()
Definition: Common.cpp:97
unsigned short WORD
Definition: Common.h:413
bool IsWinVista()
Definition: Common.cpp:93
HWND hCurrDlg
Definition: Common.cpp:13
struct _RECTS * LPRECTS
DWORD __stdcall _rol32(DWORD value, BYTE bits)
wchar_t * WSTR
Definition: Common.h:366
ushort ilog2x(uint64 X)
Definition: Common.cpp:241
long double EngUnits(long double Value, int *pExpo)
Definition: UtilFunc.cpp:180
struct _SIZES * LPSIZES
T constrain_(T x, T lo, T hi)
Definition: Common.h:901
unsigned char * PBYTE
Definition: Common.h:412
char * ASTR
Definition: Common.h:344
ulong ipow2(ushort expo)
Definition: Common.cpp:183
T mulDiv_(const T X, const T Mu, const T Dv)
Definition: Common.h:920
uint64 ipow2x(ushort expo)
Definition: Common.cpp:189
unsigned __int64 uint64
Definition: uLib/Types.h:51
WORD __stdcall _ror16(WORD value, BYTE bits)
T max_(T a, T b)
Definition: Common.h:907
#define REENABLE_BOOLCAST_WARNING
Definition: Common.h:753
ushort ilog10x(uint64 X)
Definition: Common.cpp:271
QWORD __stdcall _ror64(QWORD value, BYTE bits)
bool IsWinNT()
Definition: Common.cpp:96
UINT64 QWORD
Definition: Common.h:418
bool IsWinVer(BYTE vMajor, BYTE vMinor, bool WinNT=true)
Definition: Common.cpp:78
const wchar_t * WCSTR
Definition: Common.h:367
SHORT cx
Definition: Common.h:505
BYTE __stdcall _rol8(BYTE value, BYTE bits)
T map_(T x, T xMin, T xMax, T toMin, T toMax)
Definition: Common.h:891
void randomize()
Definition: Common.cpp:167
const WCSTR CCWSTR
Definition: Common.h:372
BOOL(WINAPI *SysImgList::Shell_GetImageLists)(HIMAGELIST *pimlLarge
const LPCTSTR CCSTR
Definition: Common.h:335
bool _isprime(unsigned long X)
Definition: Common.cpp:373
void GetWinSystemInfo(SYSTEM_INFO *si)
Definition: Common.cpp:135
HACCEL hMainAcc
Definition: Common.cpp:14
SECURITY_DESCRIPTOR DefSecDesc
Definition: Common.cpp:21
bool IsWin95()
Definition: Common.cpp:98
QWORD __stdcall _rol64(QWORD value, BYTE bits)
Build feedback definitions..
SHORT left
Definition: Common.h:495
bool IsWinXP()
Definition: Common.cpp:94
const MBCSTR CCMBSTR
Definition: Common.h:361
signed __int64 INT64
Definition: Common.h:401
long constrain(long x, long lo, long hi)
Definition: Common.cpp:151
T min_(T a, T b)
Definition: Common.h:906
DWORD __stdcall _ror32(DWORD value, BYTE bits)
unsigned __int64 UINT64
Definition: Common.h:400
bool IsWin2000()
Definition: Common.cpp:95
unsigned short ushort
Definition: uLib/Types.h:46
SHORT bottom
Definition: Common.h:498
long runningAvg(long Avg, long X, unsigned int Period)
Definition: Common.cpp:176
const UCHAR * MBCSTR
Definition: Common.h:356
SECURITY_ATTRIBUTES DefSec
Definition: Common.cpp:20
unsigned long ulong
Definition: uLib/Types.h:47
struct _RECTS * PRECTS
bool __forceinline bool_cast(BOOL B52)
Definition: Common.h:767
double _round(double x, unsigned char ndecimals)
Definition: Common.cpp:343
HWND hMdiClient
Definition: Common.cpp:15
long map(long x, long xMin, long xMax, long toMin, long toMax)
Definition: Common.cpp:145
const RECTS * LPCRECTS
Definition: Common.h:500
T runningAvg_(T Avg, T X, unsigned int Period)
Definition: Common.h:912
long double _root(long double base, long double X)
Definition: Common.cpp:296
ushort ilog10(ulong X)
Definition: Common.cpp:247
HINSTANCE hInstance
Definition: Common.cpp:10
SHORT top
Definition: Common.h:496
#define DEF_(x)
Definition: Common.h:240
signed __int64 * PINT64
Definition: Common.h:401
#define END_EXTERN_C
Definition: Common.h:221
#define BEGIN_EXTERN_C
Definition: Common.h:220
WORD __stdcall _rol16(WORD value, BYTE bits)
unsigned short * PWORD
Definition: Common.h:413
bool IsWow64()
Definition: Common.cpp:111
unsigned __int64 * PUINT64
Definition: Common.h:400
struct _SIZES * PSIZES
HWND hMainWnd
Definition: Common.cpp:12
bool bool_t
Definition: Common.h:315
const HWND CHWND
Definition: Common.h:425
bool IsWin64Bit()
Definition: Common.cpp:100
const HANDLE CHANDLE
Definition: Common.h:424
const ACSTR CCASTR
Definition: Common.h:350
SHORT right
Definition: Common.h:497
const SIZES * LPCSIZES
Definition: Common.h:508
unsigned char BYTE
Definition: Common.h:412
UCHAR * MBSTR
Definition: Common.h:355
unsigned long * PDWORD
Definition: Common.h:414
#define DISABLE_BOOLCAST_WARNING
Definition: Common.h:752
BYTE __stdcall _ror8(BYTE value, BYTE bits)