重启Windows系统服务,包括依赖服务的启动
2022-01-16
42
0
重启Windows系统服务,包括依赖服务的启动.
如重启远程访问的服务:
TCHAR* Service=TEXT("TermService");
DWORD err = RestartSerivce(Service);
代码如下
#include <winsvc.h>
DWORD CUtils::RestartSerivce(TCHAR* pSeriveName)
{
DWORD err = STATUS_SUCCESS;
SC_HANDLE hSC = NULL;
SC_HANDLE hSvc = NULL;
do
{
// 打开服务管理对象
hSC = ::OpenSCManager(NULL, NULL, GENERIC_EXECUTE);
if (hSC == NULL)
{
err = GetLastError();
DebugPrintA("OpenSCManager err %x\n", err);
break;
}
// 打开www服务。
SC_HANDLE hSvc = ::OpenService(hSC, pSeriveName, SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP | SERVICE_ENUMERATE_DEPENDENTS);
if (hSvc == NULL)
{
err = GetLastError();
DebugPrintA("OpenService err %x\n", err);
::CloseServiceHandle(hSC);
break;
}
// 获得服务的状态
SERVICE_STATUS status;
if (::QueryServiceStatus(hSvc, &status) == FALSE)
{
err = GetLastError();
DebugPrintA("QueryServiceStatus err %x\n", err);
break;
}
//如果处于运行态,则停止服务
if (status.dwCurrentState == SERVICE_RUNNING)
{
//停止依赖者
{
DWORD dwBytesNeeded = 0;
DWORD dwCount = 0;
ENUM_SERVICE_STATUS ess;
SC_HANDLE hDepService;
LPENUM_SERVICE_STATUS lpDependencies = NULL;
if (EnumDependentServices(hSvc, SERVICE_ACTIVE, lpDependencies, 0, &dwBytesNeeded, &dwCount))
{
err = GetLastError();
DebugPrintA("EnumDependentServices err %x\n", err);
break;
}
if (GetLastError() != ERROR_MORE_DATA)
{
err = GetLastError();
break;
}
// Allocate a buffer for the dependencies
lpDependencies = (LPENUM_SERVICE_STATUS)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwBytesNeeded);
if (!lpDependencies)
{
err = GetLastError();
DebugPrintA("HeapAlloc err %x\n", err);
break;
}
__try
{
// Enumerate the dependencies
if (!EnumDependentServices(hSvc, SERVICE_ACTIVE, lpDependencies, dwBytesNeeded, &dwBytesNeeded, &dwCount))
{
err = GetLastError();
DebugPrintA("EnumDependentServices err %x\n", err);
break;
}
for (DWORD i = 0; i < dwCount; i++)
{
ess = *(lpDependencies + i);
DebugPrintW(TEXT("SubServer:%s\n"), ess.lpServiceName);
// Open the service
hDepService = OpenService(hSC, ess.lpServiceName, SERVICE_STOP | SERVICE_QUERY_STATUS);
if (!hDepService)
{
err = GetLastError();
DebugPrintA("OpenService2 err %x\n", err);
break;
}
__try
{
SERVICE_STATUS ss;
if (!ControlService(hDepService, SERVICE_CONTROL_STOP, &ss))
{
err = GetLastError();
DebugPrintA("ControlService2 err %x\n", err);
break;
}
// Wait for the service to stop
while (ss.dwCurrentState != SERVICE_STOPPED)
{
Sleep(3000);
if (!QueryServiceStatus(hDepService, &ss))
{
err = GetLastError();
DebugPrintA("QueryServiceStatus2 err %x\n", err);
break;
}
if (ss.dwCurrentState == SERVICE_STOPPED)
{
DebugPrintA("SubServer%d/%d SERVICE_STOPPED\n", i, dwCount);
break;
}
else
{
DebugPrintA("Wait SubServer%d/%d stopeend\n", i, dwCount);
}
}
}
__finally
{
CloseServiceHandle(hDepService);
}
}
}
__finally
{
// Always free the enumeration buffer
if (lpDependencies != NULL)
{
HeapFree(GetProcessHeap(), 0, lpDependencies);
lpDependencies = NULL;
}
}
}
if (!ControlService(hSvc, SERVICE_CONTROL_STOP, &status))
{
err = GetLastError();
DebugPrintA("ControlService err %x\n", err);
break;
}
// 等待服务停止
while (::QueryServiceStatus(hSvc, &status) == TRUE)
{
// ::Sleep(status.dwWaitHint);
Sleep(3000);
if (status.dwCurrentState == SERVICE_STOPPED)
{
DebugPrintA("SERVICE_CONTROL_STOP ok\n");
break;
}
else
{
DebugPrintA("Wait SERVICE_CONTROL_STOP\n");
}
}
}
// 启动服务
if (::StartService(hSvc, NULL, NULL) == FALSE)
{
err = GetLastError();
DebugPrintA("ControlService err %x\n", err);
break;
}
// 等待服务启动
while (::QueryServiceStatus(hSvc, &status) == TRUE)
{
::Sleep(status.dwWaitHint);
if (status.dwCurrentState == SERVICE_RUNNING)
{
DebugPrintA("SERVICE_RUNNING ok\n");
break;
}
else
{
DebugPrintA("Wait SERVICE_RUNNING\n");
}
}
} while (0);
if (hSvc != NULL)
{
::CloseServiceHandle(hSvc);
}
if (hSC != NULL)
{
::CloseServiceHandle(hSC);
}
DebugPrintA("Restart Servuce %x\n", err);
return err;
}