diff --git a/include/MUtils/Exception.h b/include/MUtils/Exception.h index a9b256d..1ece717 100644 --- a/include/MUtils/Exception.h +++ b/include/MUtils/Exception.h @@ -55,3 +55,11 @@ while(0) throw std::runtime_error((MESSAGE)); \ } \ while(0) + +#define MUTILS_THROW_FMT(MESSAGE, ...) do \ +{ \ + char _message[256]; \ + _snprintf_s(_message, 256, _TRUNCATE, (MESSAGE), __VA_ARGS__); \ + throw std::runtime_error(_message); \ +} \ +while(0) diff --git a/include/MUtils/Global.h b/include/MUtils/Global.h index f44cec3..16029ca 100644 --- a/include/MUtils/Global.h +++ b/include/MUtils/Global.h @@ -101,25 +101,12 @@ namespace MUtils /////////////////////////////////////////////////////////////////////////////// -#define MUTILS_DELETE(PTR) do \ -{ \ - if((PTR)) \ - { \ - delete (PTR); \ - (PTR) = NULL; \ - } \ -} \ -while(0) +//Delete helper +#define MUTILS_DELETE(PTR) do { if((PTR)) { delete (PTR); (PTR) = NULL; } } while(0) +#define MUTILS_DELETE_ARRAY(PTR) do { if((PTR)) { delete [] (PTR); (PTR) = NULL; } } while(0) -#define MUTILS_DELETE_ARRAY(PTR) do \ -{ \ - if((PTR)) \ - { \ - delete [] (PTR); \ - (PTR) = NULL; \ - } \ -} \ -while(0) +//Zero memory +#define MUTILS_ZERO_MEMORY(PTR) memset(&(PTR), 0, sizeof((PTR))) //String conversion macros #define MUTILS_WCHR(STR) (reinterpret_cast((STR).utf16())) diff --git a/include/MUtils/OSSupport.h b/include/MUtils/OSSupport.h index 1cd5498..f6c8a8c 100644 --- a/include/MUtils/OSSupport.h +++ b/include/MUtils/OSSupport.h @@ -123,6 +123,12 @@ namespace MUtils //Message handler MUTILS_API bool handle_os_message(const void *const message, long *result); + //Sleep + MUTILS_API void sleep_ms(const size_t &duration); + + //Check if debugger is present + MUTILS_API void check_debugger(void); + //Error handling MUTILS_API void fatal_exit(const wchar_t* const errorMessage); } diff --git a/src/OSSupport_Win32.cpp b/src/OSSupport_Win32.cpp index 46dd086..b31adda 100644 --- a/src/OSSupport_Win32.cpp +++ b/src/OSSupport_Win32.cpp @@ -609,6 +609,59 @@ bool MUtils::OS::handle_os_message(const void *const message, long *result) } } +/////////////////////////////////////////////////////////////////////////////// +// SLEEP +/////////////////////////////////////////////////////////////////////////////// + +void MUtils::OS::sleep_ms(const size_t &duration) +{ + Sleep((DWORD) duration); +} + +/////////////////////////////////////////////////////////////////////////////// +// DEBUGGER CHECK +/////////////////////////////////////////////////////////////////////////////// + +#if (!(MUTILS_DEBUG)) +static __forceinline bool is_debugger_present(void) +{ + __try + { + CloseHandle((HANDLE)((DWORD_PTR)-3)); + } + __except(1) + { + return true; + } + + BOOL bHaveDebugger = FALSE; + if(CheckRemoteDebuggerPresent(GetCurrentProcess(), &bHaveDebugger)) + { + if(bHaveDebugger) return true; + } + + return IsDebuggerPresent(); +} +static __forceinline bool check_debugger_helper(void) +{ + if(is_debugger_present()) + { + MUtils::OS::fatal_exit(L"Not a debug build. Please unload debugger and try again!"); + return true; + } + return false; +} +#else +#define check_debugger_helper() (false) +#endif + +void MUtils::OS::check_debugger(void) +{ + check_debugger_helper(); +} + +static volatile bool g_debug_check = check_debugger_helper(); + /////////////////////////////////////////////////////////////////////////////// // FATAL EXIT /////////////////////////////////////////////////////////////////////////////// diff --git a/src/Startup.cpp b/src/Startup.cpp index 5d64585..16da7a4 100644 --- a/src/Startup.cpp +++ b/src/Startup.cpp @@ -104,6 +104,7 @@ int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entr __try { MUtils::ErrorHandler::initialize(); + MUtils::OS::check_debugger(); iResult = startup_helper(argc, argv, entry_point); } __except(1) @@ -113,6 +114,7 @@ int MUtils::Startup::startup(int &argc, char **argv, main_function_t *const entr } #else //_MSCVER MUtils::ErrorHandler::initialize(); + MUtils::OS::check_debugger(); iResult = startup_helper(argc, argv, entry_point); #endif //_MSCVER #endif //MUTILS_DEBUG