#include <windows.h>
BOOL EasyStartService(LPCSTR ServiceName)
{
SC_HANDLE hSCManager = NULL;
SC_HANDLE hService = NULL;
SERVICE_STATUS ServiceStatus;
BOOL Ret = FALSE;
do
{
hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
if (hSCManager == NULL)
break;
hService = OpenServiceA(hSCManager, ServiceName, SERVICE_START | SERVICE_QUERY_STATUS);
if (hService == NULL)
break;
if (StartServiceA(hService, 0, NULL) == FALSE)
break;
do
{
if (QueryServiceStatus(hService, &ServiceStatus) == FALSE)
break;
if (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
Ret = TRUE;
} while (ServiceStatus.dwCurrentState == SERVICE_START_PENDING);
} while (0);
if (hService)
CloseServiceHandle(hService);
if (hSCManager)
CloseServiceHandle(hSCManager);
return Ret;
}
BOOL EasyStopService(LPCSTR ServiceName)
{
SC_HANDLE hSCManager = NULL;
SC_HANDLE hService = NULL;
SERVICE_STATUS ServiceStatus;
BOOL Ret = FALSE;
do
{
hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
if (hSCManager == NULL)
break;
hService = OpenServiceA(hSCManager, ServiceName, SERVICE_STOP | SERVICE_QUERY_STATUS);
if (hService == NULL)
break;
if (ControlService(hService, SERVICE_CONTROL_STOP, &ServiceStatus) == FALSE)
break;
if (ServiceStatus.dwCurrentState == SERVICE_STOPPED)
Ret = TRUE;
while (ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
if (QueryServiceStatus(hService, &ServiceStatus) == FALSE)
break;
if (ServiceStatus.dwCurrentState == SERVICE_STOPPED)
Ret = TRUE;
}
} while (0);
if (hService)
CloseServiceHandle(hService);
if (hSCManager)
CloseServiceHandle(hSCManager);
return Ret;
}
VOID StopIIS()
{
EasyStopService("AppHostSvc");
EasyStopService("w3logsvc");
EasyStopService("W3SVC");
//必须最后结束下面这个服务,否则该服务结束不了
//WAS服务的显示名称是Windows Process Activation Service
EasyStopService("WAS");
}
VOID StartIIS()
{
EasyStartService("AppHostSvc");
EasyStartService("w3logsvc");
EasyStartService("W3SVC");
EasyStartService("WAS");
}
启动、停止Windows服务
由
·
《“启动、停止Windows服务”》 有 1 条评论
-
[…] 启动、停止Windows服务 […]
发表回复