Windows Service 程序的安装与移除

1、安装

VOID InstallService(LPCTSTR lpName, LPCTSTR lpPath)
{
SC_HANDLE schSCManager;
SC_HANDLE schService;

schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager)
{
schService = CreateService(
schSCManager,
lpName,
lpName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
lpPath,
NULL,
NULL,
NULL,
NULL,
NULL);
if (schService)
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
}

2、移除

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

schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager)
{
schService = OpenService(schSCManager, lpName, DELETE);
if(schService)
{
DeleteService(schService);

CloseServiceHandle(schService);
}

CloseServiceHandle(schSCManager);
}
}

留下评论

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