C++安装、卸载、运行windows service

53 篇文章 0 订阅
6 篇文章 0 订阅

按照自己的理解,重新实现了一下,代码如下:


#include <string.h>
#include <stdio.h>
#include <Windows.h>

#define SERVICE_NAME TEXT("MyBeepService")

SERVICE_STATUS g_status;
SERVICE_STATUS_HANDLE g_ServiceStatusHandle;
HANDLE g_stopEvent;

BOOL InstallService() {
	SC_HANDLE hSCManager;
	SC_HANDLE hSCService;
	hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if (hSCManager == NULL) {
		return FALSE;
	}

	TCHAR szModuleFileName[MAX_PATH];
	GetModuleFileName(NULL, szModuleFileName, MAX_PATH);

	hSCService = CreateService(hSCManager,
		SERVICE_NAME,
		SERVICE_NAME, 
		SC_MANAGER_ALL_ACCESS, 
		SERVICE_WIN32_OWN_PROCESS,
		SERVICE_AUTO_START, 
		SERVICE_ERROR_NORMAL,
		szModuleFileName,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL);
	if (hSCManager == NULL) {
		return FALSE;
	}

	StartService(hSCService, NULL, NULL);
	printf("Install Service Success!\n");

	CloseServiceHandle(hSCService);
	CloseServiceHandle(hSCManager);
	
	return TRUE;
}

BOOL UninstallService() {
	SC_HANDLE hSCManager;
	SC_HANDLE hSCService;
	SERVICE_STATUS curStatus;
	SERVICE_STATUS ctrlstatus;

	hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
	if (hSCManager == NULL) {
		return FALSE;
	}
	hSCService = OpenService(hSCManager, SERVICE_NAME, SERVICE_ALL_ACCESS);
	if (hSCService == NULL) {
		return FALSE;
	}
	if (!QueryServiceStatus(hSCService, &curStatus)) {
		return FALSE;
	}

	if (curStatus.dwCurrentState != SERVICE_STOPPED) {
		if (!ControlService(hSCService, SERVICE_CONTROL_STOP, &ctrlstatus)) {
			printf("Stop Service failed:%d!\n", GetLastError());
			return FALSE;
		}
	}

	if (DeleteService(hSCService)) {
		printf("Uninstall Service Success!\n");
	} else {
		printf("Uninstall Service failed:%d!\n", GetLastError());
	}

	CloseServiceHandle(hSCService);
	CloseServiceHandle(hSCManager);

	printf("Uninstall Service Success!\n");

	return TRUE;
}

VOID WINAPI ServiceHandler(DWORD fdwControl) {
	switch (fdwControl) {
	case SERVICE_CONTROL_PAUSE:
		g_status.dwCurrentState = SERVICE_PAUSED;
		break;
	case SERVICE_CONTROL_CONTINUE:
		g_status.dwCurrentState = SERVICE_RUNNING;
		break;
	case SERVICE_CONTROL_STOP:
	case SERVICE_CONTROL_SHUTDOWN:
		g_status.dwCurrentState = SERVICE_STOPPED;
		SetEvent(g_stopEvent);
		break;
	default:
		break;
	}
	SetServiceStatus(g_ServiceStatusHandle, &g_status);
}

VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) {
	g_status.dwCurrentState = SERVICE_STOPPED;
	g_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
	g_status.dwWin32ExitCode = 0;
	g_status.dwCheckPoint = 0;
	g_status.dwWaitHint = 0;
	g_status.dwServiceSpecificExitCode = 0;
	g_status.dwServiceType = SERVICE_WIN32;

	g_ServiceStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME,
			ServiceHandler);
	if (g_ServiceStatusHandle == 0) {
		return;
	}

	g_status.dwCurrentState = SERVICE_START_PENDING;
	SetServiceStatus(g_ServiceStatusHandle, &g_status);

	g_stopEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	g_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
	g_status.dwCurrentState = SERVICE_RUNNING;
	SetServiceStatus(g_ServiceStatusHandle, &g_status);

	do {
		Beep(1000, 100);
	} while (WaitForSingleObject(g_stopEvent, 5000) == WAIT_TIMEOUT);

	g_status.dwCurrentState = SERVICE_STOP_PENDING;
	SetServiceStatus(g_ServiceStatusHandle, &g_status);

	CloseHandle(g_stopEvent);

	g_status.dwCurrentState = SERVICE_STOPPED;
	g_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
	SetServiceStatus(g_ServiceStatusHandle, &g_status);
}

void RunService() {
	SERVICE_TABLE_ENTRY dispatchTable[] = { { SERVICE_NAME, ServiceMain }, {
	NULL, NULL }, };
	StartServiceCtrlDispatcher(dispatchTable);
}

int main(int argc, char* argv[]) {
	if (argc == 1) {
		RunService();
	} else {
		if (strcmp(argv[1], "install") == 0) {
			InstallService();
		} else if (strcmp(argv[1], "uninstall") == 0) {
			UninstallService();
		}
	}

	return 0;
}



知道程序的流程就能编写出来了,否则就会报1053的错误。

一开始报1053,是因为ServiceMain没写对,没有CreateEvent、WaitForSingleObject、循环。


要注意的地方有:

1、安装好服务后,是需要手动启动还是会自动启动?

2、关机时会停止服务吗?

3、开机后,服务是否会自动运行?


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值