C++ windows服务操作
相关API
OpenSCManager():打开服务控制管理器。
CreateService():创建系统服务。
OpenService():打开服务。
StartService():启动服务。
ControlService():控制服务。
SetServiceStatus():设置服务状态。
遍历服务 EnumServicesStatus
#include <iostream>
#include<Windows.h>
using namespace std;
#define MAX_SERVICE_SIZE (1024 * 64)
#define MAX_QUERY_SIZE (1024 * 8)
int main()
{
cout << "开始获取" << endl;
::setlocale(LC_ALL, ""); //支持中文显示控制台
do
{
SC_HANDLE schandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //打开服务管理
if (schandle == NULL)
{
cout << "打开服务管理失败" << endl;
break; //跳出
}
LPENUM_SERVICE_STATUS service_staus;
DWORD cbBytesNeeded = 0;
DWORD serviceReturned = 0;
DWORD resumeHandle = 0;
DWORD len = sizeof(ENUM_SERVICE_STATUSW);
service_staus = (LPENUM_SERVICE_STATUS)LocalAlloc(LPTR, MAX_SERVICE_SIZE);
if (service_staus == nullptr)
{
cout << "申请内存失败" << endl;
break;
}
BOOL ess = ::EnumServicesStatus(
schandle, //句柄
SERVICE_WIN32, //服务类型 SERVICE_DRIVER驱动 SERVICE_TYPE_ALL所有
SERVICE_STATE_ALL, //服务的状态
service_staus, //输出参数,系统服务的结构
MAX_SERVICE_SIZE, //结构的大小
&cbBytesNeeded, //输出参数,接收返回所需的服务
&serviceReturned, //输出参数,接收返回服务的数量
&resumeHandle); //输入输出参数,第一次调用必须为0,返回0代表成功
if (!ess)
{
cout << "枚举失败" << endl;
break;
}
for (int i = 0; i < serviceReturned; i++)
{
ENUM_SERVICE_STATUS service = service_staus[i];
LPWSTR displayName = service.lpDisplayName;
LPWSTR serviceName = service.lpServiceName;
switch (service.ServiceStatus.dwCurrentState) //服务状态
{
case SERVICE_STOPPED:
break;
case SERVICE_START_PENDING:
break;
case SERVICE_STOP_PENDING:
break;
case SERVICE_RUNNING:
break;
case SERVICE_CONTINUE_PENDING:
break;
case SERVICE_PAUSE_PENDING:
break;
case SERVICE_PAUSED:
break;
default:
cout << "未知状态" << endl;
break;
}
LPQUERY_SERVICE_CONFIG lpServiceConfig = nullptr; //服务详细信息结构
SC_HANDLE service_current = nullptr; //当前的服务句柄
service_current = ::OpenService(schandle, service.lpServiceName, SERVICE_QUERY_CONFIG); //打开当前服务
if (service_current == nullptr)
{
cout << "当前服务打开失败" << endl;
break;
}
lpServiceConfig = (LPQUERY_SERVICE_CONFIG)::LocalAlloc(LPTR, MAX_QUERY_SIZE); //分配内存,最大为8KB
if (lpServiceConfig == nullptr)
{
cout << "申请LPQUERY_SERVICE_CONFIG内存失败" << endl;
break;
}
if (!::QueryServiceConfig(service_current, lpServiceConfig, MAX_QUERY_SIZE, &resumeHandle))
{
cout << "查询服务失败" << endl;
break;
}
wcout << L"服务显示名称:" << displayName << L" 服务名称:" << serviceName << L"路径:" << lpServiceConfig->lpBinaryPathName << endl;
::CloseServiceHandle(service_current);// 关闭当前服务
}
::CloseServiceHandle(schandle); //关闭服务
} while (0);
::getchar(); //暂停
}
停止服务 ControlService
#include <iostream>
#include<Windows.h>
using namespace std;
#define MAX_SERVICE_SIZE (1024 * 64)
#define MAX_QUERY_SIZE (1024 * 8)
int main()
{
cout << "开始获取" << endl;
::setlocale(LC_ALL, ""); //支持中文显示控制台
SC_HANDLE schandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //打开服务管理
SC_HANDLE cur = ::OpenService(schandle, L"RuijieSCMS", SERVICE_ALL_ACCESS);
SERVICE_STATUS status = { 0 };
::ControlService(cur, SERVICE_CONTROL_STOP, &status); //控制服务停止
::CloseServiceHandle(cur); //关闭
::CloseServiceHandle(schandle);//关闭
::getchar(); //暂停
}
启动服务 StartService
#include <iostream>
#include<Windows.h>
using namespace std;
#define MAX_SERVICE_SIZE (1024 * 64)
#define MAX_QUERY_SIZE (1024 * 8)
int main()
{
cout << "开始获取" << endl;
::setlocale(LC_ALL, ""); //支持中文显示控制台
SC_HANDLE schandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //打开服务管理
SC_HANDLE cur = ::OpenService(schandle, L"RuijieSCMS", SERVICE_ALL_ACCESS);
SERVICE_STATUS status = { 0 };
::StartService(cur, 0, nullptr); //启动服务
::CloseServiceHandle(cur); //关闭
::CloseServiceHandle(schandle);//关闭
::getchar(); //暂停
}
创建服务 CreateService
#include <iostream>
#include<Windows.h>
using namespace std;
#define MAX_SERVICE_SIZE (1024 * 64)
#define MAX_QUERY_SIZE (1024 * 8)
int main()
{
cout << "开始获取" << endl;
::setlocale(LC_ALL, ""); //支持中文显示控制台
SC_HANDLE schandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //打开服务管理
SC_HANDLE h = ::CreateService(schandle,
L"women", //服务名
L"我们", //显示用的服务名
SERVICE_ALL_ACCESS, //所有访问权限
SERVICE_WIN32_OWN_PROCESS, //私有类型
SERVICE_AUTO_START, //自启动类型
SERVICE_ERROR_NORMAL, //忽略错误处理
L"sfsf.exe", //应用程序路径
nullptr, nullptr, nullptr, nullptr, nullptr);
::CloseServiceHandle(h); //关闭
::CloseServiceHandle(schandle);//关闭
::getchar(); //暂停
}