mirror of
https://github.com/es3n1n/defendnot.git
synced 2026-08-02 10:32:01 +00:00
feat(loader): start wscsvc manually if needed
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user