Windows Service 程序的启动与停止

1、启动

VOID StartService(LPCTSTR lpName)
{
SC_HANDLE schSCManager;
SC_HANDLE schService;

schSCManager = OpenSCManager(NULL, NULL, SERVICE_START);
if (schSCManager)
{
schService = OpenService(schSCManager, lpName, SERVICE_START);
if (schService)
{
StartService(schService, 0, NULL);

CloseServiceHandle(schService);
}

CloseServiceHandle(schSCManager);
}
}

2、停止

VOID StopService(LPCTSTR lpName)
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS_PROCESS ssp;

schSCManager = OpenSCManager(NULL, NULL, SERVICE_START);
if (schSCManager)
{
schService = OpenService(schSCManager, lpName, SERVICE_STOP);
if (schService)
{
ControlService(schService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);

CloseServiceHandle(schService);
}

CloseServiceHandle(schSCManager);
}
}

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注