1.枚举服务信息
LPENUM_SERVICE_STATUS st;//服务状态结构体变量
st = (LPENUM_SERVICE_STATUS)LocalAlloc(LPTR, 64 * 1024); //注意分配足够的空间
DWORD ret = 0;
DWORD size = 0;
SC_HANDLE sc = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
EnumServicesStatus(sc, SERVICE_WIN32, SERVICE_STATE_ALL, (LPENUM_SERVICE_STATUS)st, 1024 * 64, &size, &ret, NULL);
LPQUERY_SERVICE_CONFIG lsc;//服务配置结构体变量
for (DWORD i = 0; i < ret; i++)
{
SC_HANDLE ServiceConfige;
DWORD nResumeHandle = 0;
ServiceConfige = OpenService((SC_HANDLE)sc, st[i].lpServiceName, SERVICE_QUERY_CONFIG);
if (!ServiceConfige)
{
printf("OpenService error!/n");
}
lsc = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR, 8 * 1024); //注意分配足够的空间
QueryServiceConfig(ServiceConfige, lsc, 8 * 1024, &nResumeHandle); //枚举各个服务信息
//输出服务信息
st[i].lpServiceName
lsc->lpDisplayName);
lsc->lpServiceStartName
lsc->lpBinaryPathName
if (2 == lsc->dwStartType) //启动方式
{
wcscpy_s(service.StartType, L"自动");
}
if (3 == lsc->dwStartType)
{
wcscpy_s(service.StartType, L"手动");
}
if (4 == lsc->dwStartType)
{
wcscpy_s(service.StartType, L"禁止");
}
if (st[i].ServiceStatus.dwCurrentState != SERVICE_STOPPED)
{
wcscpy_s(service.ServiceState, L"正在运行");
}
else{
wcscpy_s(service.ServiceState, L"已停止");
}
CloseServiceHandle(ServiceConfige);
Sleep(20);
}
CloseServiceHandle(sc);
}
2.通过服务名启动未运行服务
BOOL ToStartService(WCHAR ServiceName[128])
{
SC_HANDLE hScm;
SC_HANDLE hService;
if ((hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
{
printf("OpenSCManager Error\n");
}
hService = OpenService(hScm, ServiceName, SERVICE_QUERY_STATUS | SERVICE_START);
if (!hService)
{
printf("OpenService error!\n");
}
SERVICE_STATUS status;
BOOL isSuccess = QueryServiceStatus(hService, &status);
if (!isSuccess)
{
printf("QueryServiceStatus error!\n");
printf("%ul\n", GetLastError());
}
if (status.dwCurrentState != SERVICE_RUNNING)
{
isSuccess = StartService(hService, NULL, NULL);
if (!isSuccess)
{
printf("启动服务失败!");
}
else{
}
}
else{
printf("服务已启动!\n");
}
CloseServiceHandle(hScm);
CloseServiceHandle(hService);
return 0;
}
3.通过服务名结束服务运行
BOOL ToCloseService(WCHAR ServiceName[128])
{
SC_HANDLE hScm;
SC_HANDLE hService;
if ((hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
{
printf("OpenSCManager Error/n");
}
hService = OpenService(hScm, ServiceName, SERVICE_QUERY_STATUS | SERVICE_STOP);
if (!hService)
{
printf("OpenService error!/n");
}
SERVICE_STATUS status;
BOOL isSuccess = QueryServiceStatus(hService, &status);
if (!isSuccess)
{
printf("QueryServiceStatus error!\n");
printf("%ul\n", GetLastError());
}
if (status.dwCurrentState == SERVICE_STOPPED)
{
printf("服务已停止!/n");
}
else{
isSuccess = ControlService(hService, SERVICE_CONTROL_STOP, &status);
if (!isSuccess)
{
printf("停止服务失败!");
}
}
CloseServiceHandle(hScm);
CloseServiceHandle(hService);
return 0;
}
4.创建服务
BOOL ToCreateService(SERVICE service)
{
SC_HANDLE hScm;
SC_HANDLE hService;
BOOL isSuccess;
if ((hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
{
printf("OpenSCManager Error/n");
}
hService = CreateService(
hScm, service.ServiceName, service.DisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
service.ServicePath, 0, 0, 0, 0, 0);
if (!hService)
{
isSuccess = FALSE;
}
else{
isSuccess = TRUE;
}
CloseServiceHandle(hScm);
CloseServiceHandle(hService);
return 0;
}
5.删除服务
BOOL CService::ToDeleteService(WCHAR ServiceName[128], SOCKET sock)
{
SC_HANDLE hScm;
SC_HANDLE hService;
if ((hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
{
printf("OpenSCManager Error\n");
}
hService = OpenService(hScm, ServiceName, SERVICE_QUERY_STATUS | SERVICE_CHANGE_CONFIG | DELETE);
if (!hService)
{
printf("OpenService error!\n");
}
SERVICE_STATUS status;
BOOL isSuccess = QueryServiceStatus(hService, &status);
if (!isSuccess)
{
printf("QueryServiceStatus error!\n");
printf("%ul\n", GetLastError());
}
if (status.dwCurrentState != SERVICE_STOPPED)
{
isSuccess = ControlService(hService, SERVICE_CONTROL_STOP, &status);
}
BOOL bRe = DeleteService(hService);
if (!bRe)
{
printf("error!");
}
CloseServiceHandle(hScm);
CloseServiceHandle(hService);
return 0;
}
6.改变服务启动类型
BOOL CService::ToChangeStartType(WCHAR ServiceName[128], WCHAR StartType[128], SOCKET sock)
{
SC_HANDLE hScm;
SC_HANDLE hService;
BOOL isSuccess;
if ((hScm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
{
printf("OpenSCManager Error/n");
}
hService = OpenService(hScm, ServiceName, SERVICE_CHANGE_CONFIG);
if (!hService)
{
printf("OpenService error!/n");
}
DWORD StartTypeValue;
if (lstrcmp(StartType, L"自动") == 0)
{
StartTypeValue = SERVICE_AUTO_START;
}
if (lstrcmp(StartType, L"手动") == 0)
{
StartTypeValue = SERVICE_DEMAND_START;
}
if (lstrcmp(StartType, L"禁止") == 0)
{
StartTypeValue = SERVICE_DISABLED;
}
SC_LOCK sclLock;
sclLock = LockServiceDatabase(hScm);
if (sclLock == NULL)
{
if (GetLastError() != ERROR_SERVICE_DATABASE_LOCKED)
printf("LockServiceDatabase error/n");
}
if (!ChangeServiceConfig(
hService, // handle of service
SERVICE_NO_CHANGE, // service type: no change
StartTypeValue, // 这里做了更改
SERVICE_NO_CHANGE, // error control: no change
NULL, // binary path: no change
NULL, // load order group: no change
NULL, // tag ID: no change
NULL, // dependencies: no change
NULL, // account name: no change
NULL, // password: no change
NULL)) //displayname
{
printf("ChangeServiceConfig error!/n");
isSuccess = FALSE;
}
UnlockServiceDatabase(sclLock);
isSuccess = TRUE;
CloseServiceHandle(hScm);
CloseServiceHandle(hService);
return 0;
}