Class manage Windows Service - Install, Uninstall, Start, Stop action. I modify and combine from 2 source code
- https://code.msdn.microsoft.com/windowsapps/CppWindowsService-cacf4948
- https://www.codeproject.com/Articles/499465/Simple-Windows-Service-in-Cplusplus
Tested in Visual Studio 2017, 64bit platform and Unicode Character Set.
- Create new project name is "SampleService", project type is "Windows Console Application"
- add WindowsService.cpp and WindowsService.h in your project
- Write script below
#include "stdafx.h"
#include "WindowsService.h"
void serve()
{
//your logic application
}
int wmain(int argc, wchar_t *argv[])
{
const wchar_t* serviceName = _T("SampleService");
const wchar_t* displayName = _T("Sample Service");
const wchar_t* serviceDependencies = _T("");
const wchar_t* serviceAccount = _T("NT AUTHORITY\\LocalService");
const wchar_t* servicePassword = NULL;
int status = EXIT_FAILURE;
//run the main service
return Windows::Service::Main(
serviceName,
displayName,
SERVICE_DEMAND_START,
serviceDependencies,
serviceAccount,
servicePassword,
serve,
argc,
argv);
}
- Change project to 64bit platform.
- Build project. Output at /x64/SampleService.exe
- Open Console Prompt. (run as administrator)
- SampleService.exe -install
- SampleService.exe - remove
- net start SampleService
- net stop SampleService