-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExampleSrv.cpp
77 lines (65 loc) · 2.56 KB
/
ExampleSrv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Copyright (C) 2016-2020 Thomas Hauck - All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
The author would be happy if changes and
improvements were reported back to him.
Author: Thomas Hauck
Email: [email protected]
*/
#include "Service.h"
#include <string>
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
#pragma comment(lib, "SrvLib.lib")
#else
#include <codecvt>
#include <locale>
#include <syslog.h>
#include <unistd.h>
#endif
int main(int argc, const char* argv[])
{
SrvParam svParam;
#if defined(_WIN32) || defined(_WIN64)
svParam.szDspName = L"Example Service"; // Servicename in Service control manager of windows
svParam.szDescribe = L"Example Service by your name"; // Description in Service control manager of windows
#endif
svParam.szSrvName = L"ExampleSrv"; // Service name (service id)
std::wstring m_strModulePath;
svParam.fnStartCallBack = [&m_strModulePath]()
{
m_strModulePath = std::wstring(FILENAME_MAX, 0);
#if defined(_WIN32) || defined(_WIN64)
if (GetModuleFileName(nullptr, &m_strModulePath[0], FILENAME_MAX) > 0)
m_strModulePath.erase(m_strModulePath.find_last_of(L'\\') + 1); // Sollte der Backslash nicht gefunden werden wird der ganz String gelöscht
if (_wchdir(m_strModulePath.c_str()) != 0)
m_strModulePath = L"./";
#else
std::string strTmpPath(FILENAME_MAX, 0);
if (readlink(std::string("/proc/" + std::to_string(getpid()) + "/exe").c_str(), &strTmpPath[0], FILENAME_MAX) > 0)
strTmpPath.erase(strTmpPath.find_last_of('/'));
//Change Directory
//If we cant find the directory we exit with failure.
if ((chdir(strTmpPath.c_str())) < 0) // if ((chdir("/")) < 0)
strTmpPath = ".";
m_strModulePath = std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>().from_bytes(strTmpPath) + L"/";
#endif
// Start you server here
//syslog(LOG_NOTICE, "StartCallBack called ");
};
svParam.fnStopCallBack = []() noexcept
{
// Stop you server here
//syslog(LOG_NOTICE, "StopCallBack called ");
};
svParam.fnSignalCallBack = []() noexcept
{
// what ever you do with this callback, maybe reload the configuration
#if defined(_WIN32) || defined(_WIN64)
OutputDebugString(L"Signal Callback\r\n");
#else
syslog(LOG_NOTICE, "SignalCallBack called ");
#endif
};
return ServiceMain(argc, argv, svParam);
}