Skip to content

Commit

Permalink
win32: Change service files structure, keep the logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kumajaya committed May 5, 2024
1 parent b5089e4 commit c6d5884
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/arch/win32/service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
# Ketut Kumajaya - initial API and implementation and/or initial documentation
# *******************************************************************************/
if(FORTE_WINDOWS_SERVICE)
forte_add_to_executable_h(SampleService ServiceBase ServiceInstaller ThreadPool)
forte_add_to_executable_cpp(CppWindowsService SampleService ServiceBase ServiceInstaller)
forte_add_to_executable_h(ForteService ServiceBase ServiceInstaller ThreadPool)
forte_add_to_executable_cpp(wmain ForteService ServiceBase ServiceInstaller)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
\***************************************************************************/

#pragma region Includes
#include "SampleService.h"
#include "ForteService.h"
#include "ThreadPool.h"
#pragma endregion

Expand All @@ -38,7 +38,7 @@ void endForte(int paSig);
int _main(int argc, char *arg[]);


CSampleService::CSampleService(PWSTR pszServiceName,
CForteService::CForteService(PWSTR pszServiceName,
int argc,
char *argv[],
BOOL fCanStop,
Expand All @@ -60,7 +60,7 @@ CSampleService::CSampleService(PWSTR pszServiceName,
}


CSampleService::~CSampleService(void)
CForteService::~CForteService(void)
{
if (m_hStoppedEvent)
{
Expand All @@ -71,7 +71,7 @@ CSampleService::~CSampleService(void)


// Modified from https://stackoverflow.com/a/21376268
void CSampleService::StartLogRedirection(const char *fname, FILE *fstream)
void CForteService::StartLogRedirection(const char *fname, FILE *fstream)
{
// Duplicate stdout/stderr and give it a new file descriptor
int i = _dup(fstream == stdout ? STDOUT_FILENO : STDERR_FILENO);
Expand All @@ -88,7 +88,7 @@ void CSampleService::StartLogRedirection(const char *fname, FILE *fstream)
}


bool CSampleService::StopLogRedirection(FILE *fstream)
bool CForteService::StopLogRedirection(FILE *fstream)
{
bool ret = false;
if (m_fDescriptor.size() != 0)
Expand All @@ -113,7 +113,7 @@ bool CSampleService::StopLogRedirection(FILE *fstream)


//
// FUNCTION: CSampleService::OnStart(DWORD, LPWSTR *)
// FUNCTION: CForteService::OnStart(DWORD, LPWSTR *)
//
// PURPOSE: The function is executed when a Start command is sent to the
// service by the SCM or when the operating system starts (for a service
Expand All @@ -137,7 +137,7 @@ bool CSampleService::StopLogRedirection(FILE *fstream)
// other solution is to spawn a new thread to perform the main service
// functions, which is demonstrated in this code sample.
//
void CSampleService::OnStart(DWORD, LPWSTR*)
void CForteService::OnStart(DWORD, LPWSTR*)
{
// Log command line arguments
std::ostringstream stream;
Expand All @@ -147,7 +147,7 @@ void CSampleService::OnStart(DWORD, LPWSTR*)
}

// Queue the main service function for execution in a worker thread.
CThreadPool::QueueUserWorkItem(&CSampleService::ServiceWorkerThread, this);
CThreadPool::QueueUserWorkItem(&CForteService::ServiceWorkerThread, this);

// Log a service start message to the Application log.
WriteEventLogEntry((PWSTR)(forte_stringToWstring(("FORTE service started:" + stream.str())).c_str()),
Expand All @@ -156,12 +156,12 @@ void CSampleService::OnStart(DWORD, LPWSTR*)


//
// FUNCTION: CSampleService::ServiceWorkerThread(void)
// FUNCTION: CForteService::ServiceWorkerThread(void)
//
// PURPOSE: The method performs the main function of the service. It runs
// on a thread pool worker thread.
//
void CSampleService::ServiceWorkerThread(void)
void CForteService::ServiceWorkerThread(void)
{
// Periodically check if the service is stopping.
while (!m_fStopping)
Expand All @@ -184,7 +184,7 @@ void CSampleService::ServiceWorkerThread(void)


//
// FUNCTION: CSampleService::OnStop(void)
// FUNCTION: CForteService::OnStop(void)
//
// PURPOSE: The function is executed when a Stop command is sent to the
// service by SCM. It specifies actions to take when a service stops
Expand All @@ -195,7 +195,7 @@ void CSampleService::ServiceWorkerThread(void)
// Be sure to periodically call ReportServiceStatus() with
// SERVICE_STOP_PENDING if the procedure is going to take long time.
//
void CSampleService::OnStop()
void CForteService::OnStop()
{
// Indicate that the service is stopping and wait for the finish of the
// main service function (ServiceWorkerThread).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
#include <cstdio>
#include <vector>

class CSampleService : public CServiceBase
class CForteService : public CServiceBase
{
public:

CSampleService(PWSTR pszServiceName,
CForteService(PWSTR pszServiceName,
int argc,
char *argv[],
BOOL fCanStop = TRUE,
BOOL fCanShutdown = TRUE,
BOOL fCanPauseContinue = FALSE);
virtual ~CSampleService(void);
virtual ~CForteService(void);

void StartLogRedirection(const char *fname, FILE *fstream);
bool StopLogRedirection(FILE *fstream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <windows.h>
#include "ServiceInstaller.h"
#include "ServiceBase.h"
#include "SampleService.h"
#include "ForteService.h"
#pragma endregion


Expand Down Expand Up @@ -131,7 +131,7 @@ int wmain(int argc, wchar_t *argv[])
}
else if (_wcsicmp(L"service", argv[1] + 1) == 0 || _wcsicmp(L"s", argv[1] + 1) == 0)
{
CSampleService service(SERVICE_DISPLAY_NAME, static_cast<int>(l_argv.size()), (char **)l_argv.data());
CForteService service(SERVICE_DISPLAY_NAME, static_cast<int>(l_argv.size()), (char **)l_argv.data());

// Start setup stdout/stderr redirection
wchar_t szPath[MAX_PATH];
Expand Down

0 comments on commit c6d5884

Please sign in to comment.