Skip to content

Commit

Permalink
Minor fixes in console & service wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmauro committed May 22, 2021
1 parent 3f32a8a commit 987900f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
74 changes: 36 additions & 38 deletions Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,62 @@

//-----------------------------------------------------------

static LONG volatile nEnabled = 0;
static LONG volatile nInitialized = 0;
static LONG volatile nMutex = 0;
static HANDLE hConsoleOut = NULL;
static int nOldStdOutMode = 0;

//-----------------------------------------------------------

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;
}
Expand Down
2 changes: 1 addition & 1 deletion Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 15 additions & 4 deletions Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}

Expand All @@ -203,6 +209,11 @@ VOID DisableStop()
return;
}

BOOL IsInteractive()
{
return bRunningAsConsole;
}

}; //namespace Service

}; //namespace MX
Expand Down Expand Up @@ -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))
Expand Down
7 changes: 5 additions & 2 deletions Service.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ namespace MX {

namespace Service {

typedef Callback<HRESULT (_In_ HANDLE hShutdownEvent, _In_ int argc, _In_ WCHAR* argv[],
_In_ BOOL bIsInteractiveApp)> OnStartCallback;
typedef Callback<HRESULT (_In_ int argc, _In_ WCHAR* argv[])> OnStartCallback;
typedef Callback<HRESULT ()> OnStopCallback;
typedef Callback<VOID (_In_ DWORD dwEventType, _In_ PDEV_BROADCAST_HDR lpDevBroadcastHdr)> OnDeviceChangeCallback;

Expand All @@ -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
Expand Down

0 comments on commit 987900f

Please sign in to comment.