From 987900f53b0784c2773207d89591423bd7429aaf Mon Sep 17 00:00:00 2001 From: Mauro Leggieri Date: Sat, 22 May 2021 10:06:51 -0300 Subject: [PATCH] Minor fixes in console & service wrapper --- Console.cpp | 74 ++++++++++++++++++++++++++--------------------------- Console.h | 2 +- Service.cpp | 19 +++++++++++--- Service.h | 7 +++-- 4 files changed, 57 insertions(+), 45 deletions(-) diff --git a/Console.cpp b/Console.cpp index 32fea6f..727174c 100644 --- a/Console.cpp +++ b/Console.cpp @@ -25,7 +25,10 @@ //----------------------------------------------------------- -static LONG volatile nEnabled = 0; +static LONG volatile nInitialized = 0; +static LONG volatile nMutex = 0; +static HANDLE hConsoleOut = NULL; +static int nOldStdOutMode = 0; //----------------------------------------------------------- @@ -33,56 +36,51 @@ namespace MX { namespace Console { -VOID Initialize(_In_ BOOL bAppIsInteractive) +VOID Initialize() { -#pragma warning(suppress: 6031) - _setmode(_fileno(stdout), _O_U16TEXT); - _InterlockedExchange(&nEnabled, (bAppIsInteractive != FALSE) ? 1 : 0); + nOldStdOutMode = _setmode(_fileno(stdout), _O_U16TEXT); + hConsoleOut = ::GetStdHandle(STD_OUTPUT_HANDLE); + + _InterlockedExchange(&nInitialized, 1); return; } VOID Print(_In_ Console::eColor nColor, _In_ LPCWSTR szFormatW, ...) { - static LONG volatile nMutex = 0; - static HANDLE hConsoleOut = NULL; - CONSOLE_SCREEN_BUFFER_INFO sCsbi; - va_list args; - - if (__InterlockedRead(&nEnabled) != 0) + if (__InterlockedRead(&nInitialized) != 0) { CFastLock cLock(&nMutex); + CONSOLE_SCREEN_BUFFER_INFO sCsbi; + va_list args; - if (__InterlockedRead(&nEnabled) != 0) + if (nColor != ColorNormal) { - if (hConsoleOut == NULL) - hConsoleOut = ::GetStdHandle(STD_OUTPUT_HANDLE); - if (nColor != ColorNormal) - { - ::GetConsoleScreenBufferInfo(hConsoleOut, &sCsbi); - switch (nColor) - { - case ColorError: - ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_RED|FOREGROUND_INTENSITY); - break; - case ColorSuccess: - ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN|FOREGROUND_INTENSITY); - break; - case ColorYellow: - ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY); - break; - case ColorBlue: - ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_BLUE|FOREGROUND_INTENSITY); - break; - } - } - va_start(args, szFormatW); - vwprintf_s(szFormatW, args); - va_end(args); - if (nColor != ColorNormal) + ::GetConsoleScreenBufferInfo(hConsoleOut, &sCsbi); + switch (nColor) { - ::SetConsoleTextAttribute(hConsoleOut, sCsbi.wAttributes); + case ColorError: + ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_RED | FOREGROUND_INTENSITY); + break; + case ColorSuccess: + ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY); + break; + case ColorYellow: + ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); + break; + case ColorBlue: + ::SetConsoleTextAttribute(hConsoleOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY); + break; } } + + va_start(args, szFormatW); + vwprintf_s(szFormatW, args); + va_end(args); + + if (nColor != ColorNormal) + { + ::SetConsoleTextAttribute(hConsoleOut, sCsbi.wAttributes); + } } return; } diff --git a/Console.h b/Console.h index b9ca4a0..abb88a2 100644 --- a/Console.h +++ b/Console.h @@ -42,7 +42,7 @@ namespace MX { namespace Console { -VOID Initialize(_In_ BOOL bAppIsInteractive); +VOID Initialize(); VOID Print(_In_ Console::eColor nColor, _In_ LPCWSTR szFormatW, ...); VOID PrintError(_In_ HRESULT hRes); diff --git a/Service.cpp b/Service.cpp index 03d1ff0..e0cde44 100644 --- a/Service.cpp +++ b/Service.cpp @@ -79,7 +79,7 @@ HRESULT Run(_In_opt_z_ LPCWSTR szServiceNameW, _In_ OnStartCallback _cStartCallb szServiceNameW = L""; //check for single instance - if (*szServiceNameW != NULL) + if (*szServiceNameW != 0) { hRes = MX::SingleInstanceCheck(szServiceNameW); if (FAILED(hRes)) @@ -127,7 +127,7 @@ HRESULT Run(_In_opt_z_ LPCWSTR szServiceNameW, _In_ OnStartCallback _cStartCallb return MX_HRESULT_FROM_LASTERROR(); //send start callback - hRes = cStartCallback(cShutdownEv.Get(), nArgumentsCount, lpArguments, TRUE); + hRes = cStartCallback(nArgumentsCount, lpArguments); if (SUCCEEDED(hRes)) { bCallStop = TRUE; @@ -167,6 +167,12 @@ HRESULT Run(_In_opt_z_ LPCWSTR szServiceNameW, _In_ OnStartCallback _cStartCallb return hRes; } +VOID SignalShutdown() +{ + cShutdownEv.Set(); + return; +} + VOID SignalStarting() { if (bRunningAsConsole == FALSE) @@ -177,7 +183,7 @@ VOID SignalStarting() VOID SignalStopping() { if (bRunningAsConsole == FALSE) - _SetServiceStatus(SERVICE_START_PENDING, ERROR_SUCCESS, 5000); + _SetServiceStatus(SERVICE_STOP_PENDING, ERROR_SUCCESS, 5000); return; } @@ -203,6 +209,11 @@ VOID DisableStop() return; } +BOOL IsInteractive() +{ + return bRunningAsConsole; +} + }; //namespace Service }; //namespace MX @@ -239,7 +250,7 @@ static VOID WINAPI _ServiceMain(_In_ DWORD dwArgc, _In_ LPWSTR *pszArgv) { _SetServiceStatus(SERVICE_START_PENDING, ERROR_SUCCESS, 5000); //send start callback - hRes = cStartCallback(cShutdownEv.Get(), nArgumentsCount, lpArguments, FALSE); + hRes = cStartCallback(nArgumentsCount, lpArguments); } if (SUCCEEDED(hRes)) diff --git a/Service.h b/Service.h index ba60497..864ad61 100644 --- a/Service.h +++ b/Service.h @@ -30,8 +30,7 @@ namespace MX { namespace Service { -typedef Callback OnStartCallback; +typedef Callback OnStartCallback; typedef Callback OnStopCallback; typedef Callback OnDeviceChangeCallback; @@ -48,12 +47,16 @@ namespace Service { HRESULT Run(_In_opt_z_ LPCWSTR szServiceNameW, _In_ OnStartCallback cStartCallback, _In_ OnStopCallback cStopCallback, _In_opt_ OnDeviceChangeCallback cDeviceChangeCallback, _In_ int argc, _In_ WCHAR* argv[]); +VOID SignalShutdown(); + VOID SignalStarting(); VOID SignalStopping(); VOID EnableStop(); VOID DisableStop(); +BOOL IsInteractive(); + }; //namespace Service }; //namespace MX