mirror of
https://gitea.tendokyu.moe/moegrid/sega-mount.git
synced 2026-10-06 05:27:58 +03:00
优化fscrypt,添加GUI目标
This commit is contained in:
+31
-12
@@ -11,45 +11,64 @@ namespace driver {
|
||||
bool ensure_running(std::string *error) {
|
||||
SC_HANDLE hScm = OpenSCManagerW(nullptr, nullptr, SC_MANAGER_ALL_ACCESS);
|
||||
if (!hScm) {
|
||||
if (error) *error = "OpenSCManager failed: " + std::to_string(GetLastError());
|
||||
if (error) *error = "OpenSCManager failed: " + utils::last_error_str();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try to open existing service
|
||||
SC_HANDLE hSvc = OpenServiceW(hScm, SERVICE_NAME,
|
||||
SERVICE_ALL_ACCESS);
|
||||
SC_HANDLE hSvc = OpenServiceW(hScm, SERVICE_NAME,SERVICE_ALL_ACCESS);
|
||||
|
||||
if (!hSvc) {
|
||||
DWORD last = GetLastError();
|
||||
if (last != ERROR_SERVICE_DOES_NOT_EXIST) {
|
||||
if (error) *error = "OpenService failed: " + std::to_string(last);
|
||||
if (error) *error = "OpenService failed: " + utils::last_error_str(last);
|
||||
CloseServiceHandle(hScm);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Service does not exist - create it
|
||||
std::wstring driver_path = utils::get_exe_dir() + L"\\" + DRIVER_FILENAME;
|
||||
// Service does not exist - copy driver to system directory and create service
|
||||
std::wstring src_path = utils::get_exe_dir() + L"\\" + DRIVER_FILENAME;
|
||||
|
||||
// Get system drivers directory (typically C:\Windows\System32\drivers)
|
||||
wchar_t sys_dir[MAX_PATH] = {};
|
||||
UINT sys_len = GetSystemDirectoryW(sys_dir, MAX_PATH);
|
||||
if (sys_len == 0 || sys_len > MAX_PATH) {
|
||||
if (error) *error = "GetSystemDirectory failed: " + utils::last_error_str();
|
||||
CloseServiceHandle(hScm);
|
||||
return false;
|
||||
}
|
||||
std::wstring dst_path = std::wstring(sys_dir) + L"\\drivers\\" + DRIVER_FILENAME;
|
||||
|
||||
// Copy driver file to system directory (overwrite if exists)
|
||||
if (!CopyFileW(src_path.c_str(), dst_path.c_str(), FALSE)) {
|
||||
if (error) *error = "Copy driver to system directory failed: " + utils::last_error_str();
|
||||
CloseServiceHandle(hScm);
|
||||
return false;
|
||||
}
|
||||
LOG_INFO("Driver copied to {}", utils::wstr_to_str(dst_path));
|
||||
|
||||
hSvc = CreateServiceW(
|
||||
hScm, SERVICE_NAME, SERVICE_NAME,
|
||||
SERVICE_ALL_ACCESS,
|
||||
SERVICE_KERNEL_DRIVER,
|
||||
SERVICE_DEMAND_START,
|
||||
SERVICE_ERROR_NORMAL,
|
||||
driver_path.c_str(),
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr);
|
||||
dst_path.c_str(),
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr
|
||||
);
|
||||
|
||||
if (!hSvc) {
|
||||
if (error) *error = "CreateService failed: " + std::to_string(GetLastError());
|
||||
if (error) *error = "CreateService failed: " + utils::last_error_str();
|
||||
CloseServiceHandle(hScm);
|
||||
return false;
|
||||
}
|
||||
LOG_INFO("Service installed: {}", utils::wstr_to_str(driver_path));
|
||||
LOG_INFO("Service installed: {}", utils::wstr_to_str(dst_path));
|
||||
}
|
||||
|
||||
// Query current state
|
||||
SERVICE_STATUS status{};
|
||||
if (!QueryServiceStatus(hSvc, &status)) {
|
||||
if (error) *error = "QueryServiceStatus failed: " + std::to_string(GetLastError());
|
||||
if (error) *error = "QueryServiceStatus failed: " + utils::last_error_str();
|
||||
CloseServiceHandle(hSvc);
|
||||
CloseServiceHandle(hScm);
|
||||
return false;
|
||||
@@ -65,7 +84,7 @@ namespace driver {
|
||||
if (!StartServiceW(hSvc, 0, nullptr)) {
|
||||
DWORD last = GetLastError();
|
||||
if (last != ERROR_SERVICE_ALREADY_RUNNING) {
|
||||
if (error) *error = "StartService failed: " + std::to_string(last);
|
||||
if (error) *error = "StartService failed: " + utils::last_error_str(last);
|
||||
CloseServiceHandle(hSvc);
|
||||
CloseServiceHandle(hScm);
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user