30 #include <boost/chrono/chrono.hpp> 31 #include <boost/thread/thread.hpp> 50 typedef std::unique_ptr<std::remove_pointer<SC_HANDLE>::type, decltype(&::CloseServiceHandle)> service_handle;
54 LPSTR p_error_text =
nullptr;
57 FORMAT_MESSAGE_FROM_SYSTEM
58 | FORMAT_MESSAGE_ALLOCATE_BUFFER
59 | FORMAT_MESSAGE_IGNORE_INSERTS
62 , MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT)
63 , reinterpret_cast<LPSTR>(&p_error_text)
68 if (
nullptr == p_error_text)
75 LocalFree(p_error_text);
80 bool relaunch_as_admin(
85 SHELLEXECUTEINFO
info{};
87 info.lpVerb =
"runas";
88 info.lpFile = command.c_str();
89 info.lpParameters = arguments.c_str();
91 info.nShow = SW_SHOWNORMAL;
92 if (!ShellExecuteEx(&
info))
105 void pause_to_display_admin_window_messages()
107 boost::chrono::milliseconds how_long{1500};
108 boost::this_thread::sleep_for(how_long);
114 BOOL is_admin = FALSE;
115 PSID p_administrators_group =
nullptr;
117 SID_IDENTIFIER_AUTHORITY nt_authority = SECURITY_NT_AUTHORITY;
119 if (!AllocateAndInitializeSid(
122 , SECURITY_BUILTIN_DOMAIN_RID
123 , DOMAIN_ALIAS_RID_ADMINS
125 , &p_administrators_group
132 if (!CheckTokenMembership(
134 , p_administrators_group
142 result = is_admin ?
true :
false;
164 std::string command = epee::string_tools::get_current_module_path();
165 relaunch_as_admin(command, arguments);
175 std::string command = epee::string_tools::get_current_module_path();
178 service_handle p_manager{
182 , SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE
184 , &::CloseServiceHandle
186 if (p_manager ==
nullptr)
192 service_handle p_service{
195 , service_name.c_str()
196 , service_name.c_str()
199 , SERVICE_WIN32_OWN_PROCESS
200 , SERVICE_DEMAND_START
201 , SERVICE_ERROR_NORMAL
202 , full_command.c_str()
210 , &::CloseServiceHandle
212 if (p_service ==
nullptr)
220 pause_to_display_admin_window_messages();
231 SERVICE_STATUS_PROCESS service_status = {};
234 service_handle p_manager{
240 , &::CloseServiceHandle
242 if (p_manager ==
nullptr)
248 service_handle p_service{
251 , service_name.c_str()
255 , &::CloseServiceHandle
257 if (p_service ==
nullptr)
275 pause_to_display_admin_window_messages();
286 service_handle p_manager{
292 , &::CloseServiceHandle
294 if (p_manager ==
nullptr)
300 service_handle p_service{
303 , service_name.c_str()
304 , SERVICE_STOP | SERVICE_QUERY_STATUS
306 , &::CloseServiceHandle
308 if (p_service ==
nullptr)
314 SERVICE_STATUS status = {};
315 if (!ControlService(p_service.get(), SERVICE_CONTROL_STOP, &status))
323 pause_to_display_admin_window_messages();
332 service_handle p_manager{
338 , &::CloseServiceHandle
340 if (p_manager ==
nullptr)
346 service_handle p_service{
349 , service_name.c_str()
350 , SERVICE_QUERY_STATUS | DELETE
352 , &::CloseServiceHandle
354 if (p_service ==
nullptr)
360 SERVICE_STATUS status = {};
361 if (!DeleteService(p_service.get()))
369 pause_to_display_admin_window_messages();
bool check_admin(bool &result)
bool start_service(std::string const &service_name)
bool ensure_admin(std::string const &arguments)
bool install_service(std::string const &service_name, std::string const &arguments)
bool stop_service(std::string const &service_name)
bool uninstall_service(std::string const &service_name)