Windows下的Service控制器
#ifndef SCONTROLLER_H
#define SCONTROLLER_H
#include <iostream>
#include <string>
class ServiceController
{
public:
ServiceController(const std::string& name);
int printServiceStatus();
void start();
void stop();
private:
std::string m_sname;
};
#endif
.cpp
#include "scontroller.h"
#include <windows.h>
#include <iostream>
#include <string>
#include <spdlog.h>
std::wstring string2wstring(std::string str)
{
std::wstring result;
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
TCHAR* buffer = new TCHAR[len + 1];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0';
result.append(buffer);
delete[] buffer;
return result;
}
DWORD GetServiceStatus(const std::wstring& serviceName) {
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (!hSCManager) {
spdlog::error("GetStatus-OpenSCManager failed: {}", GetLastError());
return -1;
}
SC_HANDLE hService = OpenService(hSCManager, serviceName.c_str(), SERVICE_QUERY_STATUS);
if (!hService) {
spdlog::error("GetStatus-Start-OpenService failed: {}", GetLastError());
CloseServiceHandle(hSCManager);
return -1;
}
SERVICE_STATUS_PROCESS ssp;
DWORD bytesNeeded;
if (!QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &bytesNeeded)) {
spdlog::error("GetStatus-QueryServiceStatusEx failed: ", GetLastError());
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return -1;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return ssp.dwCurrentState;
}
bool StartServiceByName(const std::wstring& serviceName) {
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (!hSCManager) {
spdlog::error("Start-OpenSCManager failed: {}",GetLastError());
return false;
}
SC_HANDLE hService = OpenService(hSCManager, serviceName.c_str(), SERVICE_START);
if (!hService) {
spdlog::error("Start-OpenService failed: {}",GetLastError());
CloseServiceHandle(hSCManager);
return false;
}
if (!StartService(hService, 0, NULL)) {
spdlog::error("Start-Service failed: {}",GetLastError());
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return false;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return true;
}
bool StopServiceByName(const std::wstring& serviceName) {
SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (!hSCManager) {
spdlog::error("Stop-OpenSCManager failed: {}",GetLastError());
return false;
}
SC_HANDLE hService = OpenService(hSCManager, serviceName.c_str(), SERVICE_STOP);
if (!hService) {
spdlog::error("Stop-OpenService failed: {}",GetLastError());
CloseServiceHandle(hSCManager);
return false;
}
SERVICE_STATUS status;
if (!ControlService(hService, SERVICE_CONTROL_STOP, &status)) {
spdlog::error("Stop-ControlService failed: {}",GetLastError());
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return false;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return true;
}
ServiceController::ServiceController(const std::string &name)
:m_sname(name)
{
}
int ServiceController::printServiceStatus()
{
auto w_name = string2wstring(m_sname);
auto ret = GetServiceStatus(w_name);
std::string status;
switch (ret) {
case SERVICE_STOPPED:
status = "SERVICE_STOPPED";
break;
case SERVICE_START_PENDING:
status = "SERVICE_START_PENDING";
break;
case SERVICE_STOP_PENDING:
status = "SERVICE_STOP_PENDING";
break;
case SERVICE_RUNNING:
status = "SERVICE_RUNNING";
break;
case SERVICE_CONTINUE_PENDING:
status = "SERVICE_CONTINUE_PENDING";
break;
case SERVICE_PAUSE_PENDING:
status = "SERVICE_PAUSE_PENDING";
break;
case SERVICE_PAUSED:
status = "SERVICE_PAUSED";
break;
}
spdlog::info("Service status: {}", status);
return ret;
}
void ServiceController::start()
{
auto w_name = string2wstring(m_sname);
StartServiceByName(w_name);
}
void ServiceController::stop()
{
auto w_name = string2wstring(m_sname);
StopServiceByName(w_name);
}