Skip to content

Commit cf8c3b3

Browse files
committed
added:windows service app
1 parent 6e5b7f6 commit cf8c3b3

File tree

9 files changed

+479
-4
lines changed

9 files changed

+479
-4
lines changed

XEngine_Source/XEngine.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "XEngine_APIModuleIPMac", "X
9090
EndProject
9191
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "XEngine_APIModulePhone", "XEngine_DBDepend\XEngine_PhoneData\Source\C\XEngine_APIModulePhone\XEngine_APIModulePhone.vcxproj", "{A13B72E7-FC40-4A27-81C3-26DF3C8F4C0A}"
9292
EndProject
93+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "XEngine_APIServiceApp", "XEngine_ServiceApp\XEngine_APIServiceApp\XEngine_APIServiceApp.vcxproj", "{096BEF37-4AF6-490D-868B-6306D3E251E7}"
94+
EndProject
9395
Global
9496
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9597
Debug|ARM64 = Debug|ARM64
@@ -292,6 +294,18 @@ Global
292294
{A13B72E7-FC40-4A27-81C3-26DF3C8F4C0A}.Release|x64.Build.0 = Release|x64
293295
{A13B72E7-FC40-4A27-81C3-26DF3C8F4C0A}.Release|x86.ActiveCfg = Release|Win32
294296
{A13B72E7-FC40-4A27-81C3-26DF3C8F4C0A}.Release|x86.Build.0 = Release|Win32
297+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Debug|ARM64.ActiveCfg = Debug|x64
298+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Debug|ARM64.Build.0 = Debug|x64
299+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Debug|x64.ActiveCfg = Debug|x64
300+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Debug|x64.Build.0 = Debug|x64
301+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Debug|x86.ActiveCfg = Debug|Win32
302+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Debug|x86.Build.0 = Debug|Win32
303+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Release|ARM64.ActiveCfg = Release|x64
304+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Release|ARM64.Build.0 = Release|x64
305+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Release|x64.ActiveCfg = Release|x64
306+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Release|x64.Build.0 = Release|x64
307+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Release|x86.ActiveCfg = Release|Win32
308+
{096BEF37-4AF6-490D-868B-6306D3E251E7}.Release|x86.Build.0 = Release|Win32
295309
EndGlobalSection
296310
GlobalSection(SolutionProperties) = preSolution
297311
HideSolutionNode = FALSE
@@ -307,6 +321,7 @@ Global
307321
{F6520D2C-BB8E-45BB-964B-F5D6A4318A89} = {2B89A3C7-0CC8-4881-B195-61BAA9ACB78B}
308322
{FFAC032D-4F8C-4C70-AF36-D79685A6961F} = {91E64672-DF39-4F32-87FA-0F7AADA92D73}
309323
{A13B72E7-FC40-4A27-81C3-26DF3C8F4C0A} = {91E64672-DF39-4F32-87FA-0F7AADA92D73}
324+
{096BEF37-4AF6-490D-868B-6306D3E251E7} = {79A9A569-7DA8-4D9D-AD34-D11115199EA1}
310325
EndGlobalSection
311326
GlobalSection(ExtensibilityGlobals) = postSolution
312327
SolutionGuid = {9B202F91-A601-429E-BB0F-880DDEE096FE}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#include "XEngine_CommHdr.h"
2+
/********************************************************************
3+
// Created: 2025/04/10 13:57:58
4+
// File Name: D:\XEngine_APIService\XEngine_Source\XEngine_ServiceApp\XEngine_APIServiceApp\XEngine_APIServiceApp.cpp
5+
// File Path: D:\XEngine_APIService\XEngine_Source\XEngine_ServiceApp\XEngine_APIServiceApp
6+
// File Base: XEngine_APIServiceApp
7+
// File Ext: cpp
8+
// Project: XEngine
9+
// Author: qyt
10+
// Purpose: 服务代码
11+
// History:
12+
*********************************************************************/
13+
bool bIsRun = false;
14+
XHANDLE xhLog = NULL;
15+
XENGINE_SERVICECONFIG st_ServiceConfig = {};
16+
// 服务名称,使用 constexpr 替代宏定义
17+
constexpr LPCWSTR SERVICE_NAME = L"XEngine_APIService";
18+
// 全局变量
19+
SERVICE_STATUS st_ServiceStatus = { 0 };
20+
SERVICE_STATUS_HANDLE st_hServiceStatusHandle = nullptr;
21+
// 函数声明
22+
void WINAPI XEngine_ServiceCtrlHandler(DWORD dwControl);
23+
void WINAPI XEngine_ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv);
24+
void XEngine_InstallService();
25+
void XEngine_UninstallService();
26+
// 主函数
27+
int wmain(int argc, wchar_t* argv[])
28+
{
29+
//初始化参数
30+
if (!XEngine_Configure_Parament(argc, argv))
31+
{
32+
return 0;
33+
}
34+
35+
if (argc > 1)
36+
{
37+
if (_wcsicmp(argv[1], L"install") == 0)
38+
{
39+
XEngine_InstallService();
40+
return 0;
41+
}
42+
else if (_wcsicmp(argv[1], L"uninstall") == 0)
43+
{
44+
XEngine_UninstallService();
45+
return 0;
46+
}
47+
}
48+
49+
SERVICE_TABLE_ENTRYW ServiceTable[] =
50+
{
51+
{ const_cast<LPWSTR>(SERVICE_NAME), (LPSERVICE_MAIN_FUNCTIONW)XEngine_ServiceMain },
52+
{ nullptr, nullptr }
53+
};
54+
55+
if (!StartServiceCtrlDispatcherW(ServiceTable))
56+
{
57+
std::wcerr << L"StartServiceCtrlDispatcher failed (" << GetLastError() << L")\n";
58+
return 1;
59+
}
60+
return 0;
61+
}
62+
63+
// 服务主函数
64+
void WINAPI XEngine_ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
65+
{
66+
// 初始化服务状态
67+
st_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
68+
st_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
69+
st_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
70+
st_ServiceStatus.dwWin32ExitCode = 0;
71+
st_ServiceStatus.dwServiceSpecificExitCode = 0;
72+
st_ServiceStatus.dwCheckPoint = 0;
73+
st_ServiceStatus.dwWaitHint = 0;
74+
75+
// 注册服务控制处理程序
76+
st_hServiceStatusHandle = RegisterServiceCtrlHandlerW(SERVICE_NAME, XEngine_ServiceCtrlHandler);
77+
if (NULL == st_hServiceStatusHandle)
78+
{
79+
throw std::runtime_error("RegisterServiceCtrlHandler failed");
80+
}
81+
82+
// 服务启动完成
83+
st_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
84+
if (!SetServiceStatus(st_hServiceStatusHandle, &st_ServiceStatus))
85+
{
86+
throw std::runtime_error("SetServiceStatus failed");
87+
}
88+
89+
// 服务主循环(示例)
90+
while (st_ServiceStatus.dwCurrentState == SERVICE_RUNNING)
91+
{
92+
Sleep(1000); // 模拟服务工作
93+
// 在这里添加你的服务逻辑
94+
}
95+
try
96+
{
97+
98+
}
99+
catch (const std::exception& e)
100+
{
101+
std::wcerr << L"ServiceMain exception: " << e.what() << L"\n";
102+
st_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
103+
SetServiceStatus(st_hServiceStatusHandle, &st_ServiceStatus);
104+
}
105+
}
106+
107+
// 服务控制处理函数
108+
void WINAPI XEngine_ServiceCtrlHandler(DWORD dwControl)
109+
{
110+
switch (dwControl)
111+
{
112+
case SERVICE_CONTROL_STOP:
113+
case SERVICE_CONTROL_SHUTDOWN:
114+
st_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
115+
SetServiceStatus(st_hServiceStatusHandle, &st_ServiceStatus);
116+
break;
117+
default:
118+
break;
119+
}
120+
}
121+
122+
// 安装服务
123+
void XEngine_InstallService()
124+
{
125+
try
126+
{
127+
wchar_t szPath[MAX_PATH];
128+
if (!GetModuleFileNameW(nullptr, szPath, MAX_PATH))
129+
{
130+
throw std::runtime_error("GetModuleFileName failed");
131+
}
132+
133+
auto hSCManager = std::unique_ptr<std::remove_pointer_t<SC_HANDLE>, decltype(&CloseServiceHandle)>(
134+
OpenSCManagerW(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE),
135+
CloseServiceHandle
136+
);
137+
if (!hSCManager)
138+
{
139+
throw std::runtime_error("OpenSCManager failed");
140+
}
141+
142+
auto hService = std::unique_ptr<std::remove_pointer_t<SC_HANDLE>, decltype(&CloseServiceHandle)>(CreateServiceW(hSCManager.get(), SERVICE_NAME, L"My Modern Service",SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,szPath, nullptr, nullptr, nullptr, nullptr, nullptr),CloseServiceHandle
143+
);
144+
if (!hService)
145+
{
146+
throw std::runtime_error("CreateService failed");
147+
}
148+
149+
std::wcout << L"Service installed successfully\n";
150+
}
151+
catch (const std::exception& e)
152+
{
153+
std::wcerr << L"XEngine_InstallService exception: " << e.what() << L"\n";
154+
}
155+
}
156+
157+
// 卸载服务
158+
void XEngine_UninstallService()
159+
{
160+
try
161+
{
162+
auto hSCManager = std::unique_ptr<std::remove_pointer_t<SC_HANDLE>, decltype(&CloseServiceHandle)>(OpenSCManagerW(nullptr, nullptr, SC_MANAGER_ALL_ACCESS), CloseServiceHandle);
163+
if (!hSCManager)
164+
{
165+
throw std::runtime_error("OpenSCManager failed");
166+
}
167+
auto hService = std::unique_ptr<std::remove_pointer_t<SC_HANDLE>, decltype(&CloseServiceHandle)>(OpenServiceW(hSCManager.get(), SERVICE_NAME, DELETE), CloseServiceHandle);
168+
if (!hService)
169+
{
170+
throw std::runtime_error("OpenService failed");
171+
}
172+
173+
if (!DeleteService(hService.get()))
174+
{
175+
throw std::runtime_error("DeleteService failed");
176+
}
177+
std::wcout << L"Service uninstalled successfully\n";
178+
}
179+
catch (const std::exception& e)
180+
{
181+
std::wcerr << L"XEngine_UninstallService exception: " << e.what() << L"\n";
182+
}
183+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>17.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{096bef37-4af6-490d-868b-6306d3e251e7}</ProjectGuid>
25+
<RootNamespace>XEngineAPIServiceApp</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v143</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v143</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v143</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v143</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<IncludePath>$(XEngine_Include);$(IncludePath)</IncludePath>
75+
<LibraryPath>$(XEngine_Lib32);$(LibraryPath)</LibraryPath>
76+
</PropertyGroup>
77+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
78+
<ClCompile>
79+
<WarningLevel>Level3</WarningLevel>
80+
<SDLCheck>true</SDLCheck>
81+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
82+
<ConformanceMode>true</ConformanceMode>
83+
</ClCompile>
84+
<Link>
85+
<SubSystem>Console</SubSystem>
86+
<GenerateDebugInformation>true</GenerateDebugInformation>
87+
</Link>
88+
</ItemDefinitionGroup>
89+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
90+
<ClCompile>
91+
<WarningLevel>Level3</WarningLevel>
92+
<FunctionLevelLinking>true</FunctionLevelLinking>
93+
<IntrinsicFunctions>true</IntrinsicFunctions>
94+
<SDLCheck>true</SDLCheck>
95+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
96+
<ConformanceMode>true</ConformanceMode>
97+
</ClCompile>
98+
<Link>
99+
<SubSystem>Console</SubSystem>
100+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
101+
<OptimizeReferences>true</OptimizeReferences>
102+
<GenerateDebugInformation>true</GenerateDebugInformation>
103+
</Link>
104+
</ItemDefinitionGroup>
105+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
106+
<ClCompile>
107+
<WarningLevel>Level3</WarningLevel>
108+
<SDLCheck>true</SDLCheck>
109+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
110+
<ConformanceMode>true</ConformanceMode>
111+
</ClCompile>
112+
<Link>
113+
<SubSystem>Console</SubSystem>
114+
<GenerateDebugInformation>true</GenerateDebugInformation>
115+
</Link>
116+
</ItemDefinitionGroup>
117+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
118+
<ClCompile>
119+
<WarningLevel>Level3</WarningLevel>
120+
<FunctionLevelLinking>true</FunctionLevelLinking>
121+
<IntrinsicFunctions>true</IntrinsicFunctions>
122+
<SDLCheck>true</SDLCheck>
123+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
124+
<ConformanceMode>true</ConformanceMode>
125+
</ClCompile>
126+
<Link>
127+
<SubSystem>Console</SubSystem>
128+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
129+
<OptimizeReferences>true</OptimizeReferences>
130+
<GenerateDebugInformation>true</GenerateDebugInformation>
131+
</Link>
132+
</ItemDefinitionGroup>
133+
<ItemGroup>
134+
<ClCompile Include="XEngine_APIServiceApp.cpp" />
135+
<ClCompile Include="XEngine_Configure.cpp" />
136+
</ItemGroup>
137+
<ItemGroup>
138+
<ClInclude Include="XEngine_CommHdr.h" />
139+
<ClInclude Include="XEngine_Configure.h" />
140+
</ItemGroup>
141+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
142+
<ImportGroup Label="ExtensionTargets">
143+
</ImportGroup>
144+
</Project>

0 commit comments

Comments
 (0)