-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceCtrl.cpp
More file actions
156 lines (136 loc) · 4.37 KB
/
ServiceCtrl.cpp
File metadata and controls
156 lines (136 loc) · 4.37 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "ServiceCtrl.h"
CServiceCtrl ServiceCtrl;
CServiceCtrl::CServiceCtrl() {
serviceHandle = NULL;
}
bool CServiceCtrl::Create(char* serviceName, char* displayName, char* binPath) {
SC_HANDLE scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (scmHandle == NULL) {
return false;
}
serviceHandle = CreateServiceA(scmHandle,
serviceName,
displayName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
binPath,
NULL, NULL, NULL, NULL, NULL);
CloseServiceHandle(scmHandle);
if (serviceHandle == NULL) {
return false;
}
SERVICE_SID_INFO serviceSidInfo = { 0 };
serviceSidInfo.dwServiceSidType = SERVICE_SID_TYPE_UNRESTRICTED;
if (!ChangeServiceConfig(serviceHandle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) {
return false;
}
return true;
}
bool CServiceCtrl::IsRunning(char* serviceName) {
bool result = false;
SC_HANDLE scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (scmHandle == NULL) {
return false;
}
SC_HANDLE serviceHandle = OpenService(scmHandle, StrCtrl.AnsiStringToWideString(serviceName).c_str(), SERVICE_QUERY_STATUS);
if (serviceHandle == NULL) {
CloseServiceHandle(scmHandle);
return false;
}
SERVICE_STATUS serviceStatus;
if (QueryServiceStatus(serviceHandle, &serviceStatus)) {
result = (serviceStatus.dwCurrentState == SERVICE_RUNNING);
}
CloseServiceHandle(serviceHandle);
CloseServiceHandle(scmHandle);
return result;
}
bool CServiceCtrl::Start(char* serviceName, bool force_admin) {
std::wstring wstrServiceName = StrCtrl.AnsiStringToWideString(serviceName);
if (force_admin) {
// 관리자 권한으로 실행하기 위해 ShellExecute 함수를 사용합니다.
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.lpVerb = L"runas";
sei.lpFile = wstrServiceName.c_str();
sei.nShow = SW_SHOW;
if (!ShellExecuteEx(&sei)) {
return false;
}
return true;
}
else {
serviceHandle = OpenService(NULL, wstrServiceName.c_str(), SERVICE_ALL_ACCESS);
if (serviceHandle == NULL) {
return false;
}
if (!StartService(serviceHandle, 0, NULL)) {
return false;
}
return true;
}
}
bool CServiceCtrl::Stop(char* serviceName) {
serviceHandle = OpenServiceA(NULL, serviceName, SERVICE_ALL_ACCESS);
if (serviceHandle == NULL) {
return false;
}
SERVICE_STATUS status;
if (!ControlService(serviceHandle, SERVICE_CONTROL_STOP, &status)) {
return false;
}
return true;
}
bool CServiceCtrl::Restart(char* serviceName) {
if (!Stop(serviceName)) {
return false;
}
return Start(serviceName);
}
bool CServiceCtrl::Delete(char* serviceName) {
serviceHandle = OpenServiceA(NULL, serviceName, SERVICE_ALL_ACCESS);
if (serviceHandle == NULL) {
return false;
}
if (!DeleteService(serviceHandle)) {
return false;
}
CloseServiceHandle(serviceHandle);
return true;
}
/*
* Example Code
int main() {
CServiceCtrl service;
const char* serviceName = "MyService";
const char* displayName = "My Service";
const char* binPath = "C:\\MyService\\MyService.exe";
if (service.Create(serviceName, displayName, binPath)) {
std::cout << "Service created successfully!" << std::endl;
} else {
std::cout << "Failed to create service!" << std::endl;
}
if (service.Start(serviceName)) {
std::cout << "Service started successfully!" << std::endl;
} else {
std::cout << "Failed to start service!" << std::endl;
}
if (service.Stop(serviceName)) {
std::cout << "Service stopped successfully!" << std::endl;
} else {
std::cout << "Failed to stop service!" << std::endl;
}
if (service.Restart(serviceName)) {
std::cout << "Service restarted successfully!" << std::endl;
} else {
std::cout << "Failed to restart service!" << std::endl;
}
if (service.Delete(serviceName)) {
std::cout << "Service deleted successfully!" << std::endl;
} else {
std::cout << "Failed to delete service!" << std::endl;
}
return 0;
}
*/