-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecoverIt.cpp
More file actions
95 lines (85 loc) · 2.84 KB
/
RecoverIt.cpp
File metadata and controls
95 lines (85 loc) · 2.84 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <windows.h>
#include <string>
#include <iostream>
bool SetServiceFailureRunProgram(std::wstring serviceName,std::wstring programPath,std::wstring arguments)
{
SC_HANDLE hSCM = OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS);
if (!hSCM)
{
std::wcerr << L"OpenSCManager failed. Error: " << GetLastError() << std::endl;
return false;
}
SC_HANDLE hService = OpenService(hSCM, serviceName.c_str(), SERVICE_CHANGE_CONFIG);
if (!hService)
{
std::wcerr << L"OpenService failed. Error: " << GetLastError() << std::endl;
CloseServiceHandle(hSCM);
return false;
}
std::wstring commandLine = programPath;
if (!arguments.empty())
{
commandLine += L" ";
commandLine += arguments;
}
SC_ACTION actions[1];
actions[0].Type = SC_ACTION_RUN_COMMAND;
actions[0].Delay = 0; // no delay
SERVICE_FAILURE_ACTIONS sfa = {};
sfa.dwResetPeriod = 0; // reset failure count never
sfa.lpRebootMsg = nullptr;
sfa.lpCommand = const_cast<LPWSTR>(commandLine.c_str());
sfa.cActions = 1;
sfa.lpsaActions = actions;
BOOL success = ChangeServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, &sfa);
if (!success)
{
std::wcerr << L"ChangeServiceConfig2 failed. Error: " << GetLastError() << std::endl;
}
else
{
std::wcout << L"Service failure action updated successfully." << std::endl;
}
SERVICE_FAILURE_ACTIONS_FLAG flag = {};
flag.fFailureActionsOnNonCrashFailures = TRUE;
success = ChangeServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, &flag);
if (!success)
{
std::wcerr << L"ChangeServiceConfig2 (FAILURE_ACTIONS_FLAG) failed. Error: " << GetLastError() << std::endl;
}
else
{
std::wcout << L"Service recovery configured successfully (including stop with errors)." << std::endl;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCM);
return success == TRUE;
}
int wmain(int argc, wchar_t* argv[])
{
std::wcout << L"\nRecoverIt: Change Windows Service Recovery\n"
<< L"\nGitHub: https://github.com/TwoSevenOneT/RecoverIt\n"
<< L"\nTwo Seven One Three: https://x.com/TwoSevenOneT\n"
<< L"\n==========================================================\n\n";
if (argc < 3)
{
std::wcerr << L"Usage: RecoverIt <ServiceName> <ProgramPath> [Arguments]" << std::endl;
return 1;
}
std::wstring serviceName = argv[1];
std::wstring programPath = argv[2];
std::wstring arguments;
if (argc > 3)
{
for (int i = 3; i < argc; ++i)
{
if (!arguments.empty()) arguments += L" ";
arguments += argv[i];
}
}
if (!SetServiceFailureRunProgram(serviceName, programPath, arguments))
{
return 1;
}
return 0;
}