// Determine if the autodial service is running on this PC. bool nsAutodial::IsAutodialServiceRunning() { nsAutoServiceHandle hSCManager(OpenSCManager(nullptr, SERVICES_ACTIVE_DATABASE, SERVICE_QUERY_STATUS)); if (hSCManager == nullptr) { LOGE(("Autodial: failed to open service control manager. Error %d.", ::GetLastError())); return false; } nsAutoServiceHandle hService(OpenServiceW(hSCManager, L"RasAuto", SERVICE_QUERY_STATUS)); if (hSCManager == nullptr) { LOGE(("Autodial: failed to open RasAuto service.")); return false; } SERVICE_STATUS status; if (!QueryServiceStatus(hService, &status)) { LOGE(("Autodial: ::QueryServiceStatus() failed. Error: %d", ::GetLastError())); return false; } return (status.dwCurrentState == SERVICE_RUNNING); }
bool ServiceUtil::send_control_code(const tstring& name, const DWORD code) { bool bReturn = false; do { scoped_svc_handle hService(name, GENERIC_WRITE | GENERIC_EXECUTE, GENERIC_WRITE | GENERIC_EXECUTE); if (!hService.valid()) { break; } SERVICE_STATUS status = {0}; if (!ControlService(hService.get(), code, &status)) { ErrorLogLastErr(TSTR("ControlService[%s:%lu] fail"), name.c_str(), code); break; } bReturn = true; } while (false); return bReturn; }
bool ServiceUtil::is_running(const tstring& name) { bool bReturn = false; do { scoped_svc_handle hService(name, GENERIC_READ, GENERIC_READ); if (!hService.valid()) { break; } SERVICE_STATUS status = {0}; if (!QueryServiceStatus(hService.get(), &status)) { ErrorLogLastErr(TSTR("QueryServiceStatus[%s] fail"), name.c_str()); break; } if (SERVICE_RUNNING != status.dwCurrentState) { DebugLog(TSTR("service[%s] is not running. status: %lu, %s"), name.c_str(), status.dwCurrentState, service_status_str(status.dwCurrentState)); break; } bReturn = true; } while (false); return bReturn; }
bool ServiceUtil::remove(const tstring& name) { bool bReturn = false; do { scoped_svc_handle hService(name, GENERIC_ALL, SERVICE_ALL_ACCESS); if (!hService.valid()) { break; } if (!DeleteService(hService.get())) { ErrorLogLastErr(TSTR("DeleteService[%s] fail"), name.c_str()); break; } bReturn = true; } while (false); return bReturn; }
bool ServiceUtil::stop(const tstring& name, const DWORD timeout_ms) { InfoLog(TSTR("StopService[%s] begin"), name.c_str()); bool bReturn = false; do { scoped_svc_handle hService(name, GENERIC_ALL, GENERIC_EXECUTE | GENERIC_READ); if (!hService.valid()) { break; } SERVICE_STATUS status = {0}; if (!QueryServiceStatus(hService.get(), &status)) { ErrorLogLastErr(TSTR("QueryServiceStatus[%s] fail"), name.c_str()); break; } if (SERVICE_STOPPED == status.dwCurrentState) { bReturn = true; break; } if (SERVICE_STOP_PENDING != status.dwCurrentState) { if (!ControlService(hService.get(), SERVICE_CONTROL_STOP, &status)) { ErrorLogLastErr(TSTR("ControlService[%s] for stopping fail"), name.c_str()); break; } } const DWORD interval_ms = 500;//ÿ500ºÁÃë¼ì²âÒ»´Î DWORD total_ms = timeout_ms > interval_ms ? timeout_ms : interval_ms; DWORD total_count = total_ms / interval_ms; for (DWORD index = 0; index != total_count; ++index) { if (!QueryServiceStatus(hService.get(), &status)) { ErrorLogLastErr(TSTR("QueryServiceStatus[%s] fail"), name.c_str()); break; } if (SERVICE_RUNNING == status.dwCurrentState) { DebugLog("SERVICE_RUNNING"); Sleep(interval_ms); } else if (SERVICE_STOP_PENDING == status.dwCurrentState) { DebugLog("SERVICE_STOP_PENDING"); Sleep(interval_ms); } else { break; } } if (SERVICE_STOPPED != status.dwCurrentState) { ErrorLog(TSTR("stop service[%s] fail, current status: %lu, %s"), name.c_str(), status.dwCurrentState, service_status_str(status.dwCurrentState)); break; } bReturn = true; } while (false); InfoLog(TSTR("StopService[%s] end"), name.c_str()); return bReturn; }
bool ServiceUtil::startup(const tstring& name, const DWORD timeout_ms) { InfoLog(TSTR("StartupService[%s] begin"), name.c_str()); bool bReturn = false; do { scoped_svc_handle hService(name, GENERIC_ALL, GENERIC_EXECUTE | GENERIC_READ); if (!hService.valid()) { break; } SERVICE_STATUS status = {0}; if (!QueryServiceStatus(hService.get(), &status)) { ErrorLogLastErr(TSTR("QueryServiceStatus[%s] fail"), name.c_str()); break; } if (SERVICE_RUNNING == status.dwCurrentState) { bReturn = true; break; } if (SERVICE_START_PENDING != status.dwCurrentState) { if (!StartService(hService.get(), 0, NULL)) { CLastErrorFormat e; if (ERROR_SERVICE_ALREADY_RUNNING == e.code()) { DebugLog("service is already running"); bReturn = true; } else { ErrorLogLastErrEx(e, TSTR("StartService[%s] fail"), name.c_str()); } break; } } const DWORD interval_ms = 500;//ÿ500ºÁÃë¼ì²âÒ»´Î DWORD total_ms = timeout_ms > interval_ms ? timeout_ms : interval_ms; DWORD total_count = total_ms / interval_ms; for (DWORD index = 0; index != total_count; ++index) { if (!QueryServiceStatus(hService.get(), &status)) { ErrorLogLastErr(TSTR("QueryServiceStatus[%s] fail"), name.c_str()); break; } if (SERVICE_START_PENDING == status.dwCurrentState) { DebugLog("SERVICE_START_PENDING"); Sleep(interval_ms); } else { break; } } if (SERVICE_RUNNING != status.dwCurrentState) { ErrorLog(TSTR("start service[%s] fail, current status: %lu, %s"), name.c_str(), status.dwCurrentState, service_status_str(status.dwCurrentState)); break; } bReturn = true; } while (false); InfoLog(TSTR("StartupService[%s] end"), name.c_str()); return bReturn; }