feat(loader): start wscsvc manually if needed

This commit is contained in:
es3n1n
2025-05-30 18:35:57 +02:00
parent 6d6c3fc1c4
commit 8bc56304f5
3 changed files with 50 additions and 11 deletions

View File

@@ -3,6 +3,7 @@
#include "util/scm.hpp"
#include <format>
#include <print>
#include <stdexcept>
namespace loader {
@@ -14,13 +15,33 @@ namespace loader {
auto service = manager.get_service(L"wscsvc");
if (!service.valid() || !service.query_status()) {
throw std::runtime_error(strings::wsc_unavailable_error().data());
throw std::runtime_error(std::format("{}\nOpen error: {}", strings::wsc_unavailable_error().data(), GetLastError()));
}
const auto state = service.state();
if (state != scm::ServiceState::RUNNING) {
throw std::runtime_error(std::format("{}\nService state: {}, but should be RUNNING", strings::wsc_unavailable_error(),
scm::kServiceStateNames[static_cast<std::underlying_type_t<scm::ServiceState>>(state)]));
if (service.state() == scm::ServiceState::RUNNING) {
/// Wsc service has been already started, no need to start it ourselves
return;
}
/// Let's start the service ourselves
std::println("** wscsvc is not running, starting it..");
if (!service.start()) {
throw std::runtime_error(std::format("{}\nTried to start the service, but go an error: {}", strings::wsc_unavailable_error(), GetLastError()));
}
std::println("** successfully started the service, waiting for it to get up..");
while (true) {
if (!service.query_status(/*force=*/true)) {
throw std::runtime_error(
std::format("{}\nStarted the service, got an error while querying: {}", strings::wsc_unavailable_error(), GetLastError()));
}
if (const auto state = service.state(); state == scm::ServiceState::RUNNING) {
std::println("** we are good to go");
return;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
} // namespace loader