mirror of
https://github.com/momo5502/hypervisor.git
synced 2025-04-19 05:32:55 +00:00
47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
#include "std_include.hpp"
|
|
#include "driver.hpp"
|
|
|
|
driver::driver(const std::filesystem::path& driver_file, const std::string& service_name)
|
|
{
|
|
this->manager_ = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_ALL_ACCESS);
|
|
if (!this->manager_)
|
|
{
|
|
throw std::runtime_error("Unable to open SC manager");
|
|
}
|
|
|
|
this->service_ = OpenServiceA(this->manager_, service_name.data(), SERVICE_ALL_ACCESS);
|
|
|
|
if (!this->service_)
|
|
{
|
|
this->service_ = CreateServiceA(this->manager_, service_name.data(),
|
|
service_name.data(), SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER,
|
|
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
|
|
driver_file.generic_string().data(), nullptr, nullptr,
|
|
nullptr, nullptr, nullptr);
|
|
}
|
|
|
|
if (!this->service_)
|
|
{
|
|
this->service_ = OpenServiceA(this->manager_, service_name.data(), SERVICE_ALL_ACCESS);
|
|
}
|
|
|
|
if (!this->service_)
|
|
{
|
|
this->manager_ = {};
|
|
throw std::runtime_error("Unable to create service");
|
|
}
|
|
|
|
StartServiceA(this->service_, 0, nullptr);
|
|
}
|
|
|
|
driver::~driver()
|
|
{
|
|
if (this->service_)
|
|
{
|
|
SERVICE_STATUS status{};
|
|
ControlService(this->service_, SERVICE_CONTROL_STOP, &status);
|
|
|
|
DeleteService(this->service_);
|
|
}
|
|
}
|