windows启动停止服务

#include <windows.h>
#include <stdio.h>
#define ____DEVICE_BASENAME			("myfirst")

//
//	Get system error message string
//
PCSTR SystemMessage(
	DWORD			nError)
{
	static CHAR		msg[256];

	if (!FormatMessage(
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL, nError, 0, msg, sizeof(msg), NULL)) {

		_snprintf(msg, sizeof(msg),
			"Unknown system error %lu (0x%08x)\n", nError, nError);
	}
	return msg;
}

DWORD WINAPI StartDriver()
{
	SC_HANDLE		hScManager;			// Service Control Manager
	SC_HANDLE		hService;			// Service (= Driver)
	SERVICE_STATUS	stat;
	DWORD			ret = ERROR_SUCCESS;
	int				i;


	//	Connect to the Service Control Manager
	hScManager = OpenSCManager(NULL, NULL, 0);

	if (hScManager == NULL)
	{
		ret = GetLastError();
		fprintf(stderr, "failed to connect scm: %s\n", SystemMessage(ret));
		return ret;
	}

	//	Open the driver entry in the service database
	hService = OpenService(
		hScManager,						// Service control manager
		____DEVICE_BASENAME,			// service name
		SERVICE_START
		| SERVICE_QUERY_STATUS);		// service access mode
	if (hService == NULL)
	{
		ret = GetLastError();
		fprintf(stderr, "failed to open service: %s\n", SystemMessage(ret));
		goto cleanup;
	}

	//	Start the driver
	if (!StartService(hService, 0, NULL))
	{
		ret = GetLastError();
		fprintf(stderr, "failed to start service: %s\n", SystemMessage(ret));
		goto cleanup;
	}

	//	Wait until the driver is properly running
	for (i = 0; i < 5; ++i)
	{
		if (!QueryServiceStatus(hService, &stat))
		{
			ret = GetLastError();
			fprintf(stderr, "failed to query service: %s\n", SystemMessage(ret));
			break;
		}
		if (stat.dwCurrentState == SERVICE_RUNNING)
			break;
		Sleep(1000);
	}

	if (stat.dwCurrentState == SERVICE_RUNNING)
	{
		fprintf(stderr, "service is running now\n");
	}
	else
	{
		fprintf(stderr, "failed to run service\n");
		ret = ERROR_SERVICE_NOT_ACTIVE;
	}

cleanup:
	//	Close the service object handle
	if (hService) {
		CloseServiceHandle(hService);
	}
	//	Close handle to the service control manager.
	if (hScManager) {
		CloseServiceHandle(hScManager);
	}

	return ret;
}

DWORD WINAPI StopDriver()
{
	SC_HANDLE		hScManager;			// Service Control Manager
	SC_HANDLE		hService;			// Service (= Driver)
	SERVICE_STATUS	stat;
	DWORD			ret = ERROR_SUCCESS;
	int				i;


	//	Connect to the Service Control Manager
	hScManager = OpenSCManager(NULL, NULL, 0);
	if (hScManager == NULL)
	{
		ret = GetLastError();
		fprintf(stderr, "failed to connect scm: %s\n", SystemMessage(ret));
		return ret;
	}

	//	Open the VFD driver entry in the service database
	hService = OpenService(
		hScManager,						// Service control manager
		____DEVICE_BASENAME,			// service name
		SERVICE_STOP
		| SERVICE_QUERY_STATUS);		// service access mode
	if (hService == NULL)
	{
		ret = GetLastError();
		fprintf(stderr, "failed to open service: %s\n", SystemMessage(ret));
		goto cleanup;
	}

	//	Stop the driver
	if (!ControlService(hService, SERVICE_CONTROL_STOP, &stat))
	{
		ret = GetLastError();
		fprintf(stderr, "failed to stop service\n");
		goto cleanup;
	}

	//	Wait until the driver is stopped
	for (i = 0; i < 5; ++i)
	{
		Sleep(1000);
		if (!QueryServiceStatus(hService, &stat))
		{
			ret = GetLastError();
			fprintf(stderr, "failed to query service: %s\n", SystemMessage(ret));
			break;
		}
		if (stat.dwCurrentState != SERVICE_RUNNING)
			break;
	}

	if (stat.dwCurrentState != SERVICE_RUNNING)
	{
		fprintf(stderr, "service is stopped now\n");
	}
	else
	{
		fprintf(stderr, "failed to stop service: %s\n", SystemMessage(ret));
		ret = ERROR_SERVICE_NOT_ACTIVE;
	}

cleanup:
	//	Close the service object handle
	if (hService) {
		CloseServiceHandle(hService);
	}
	//	Close handle to the service control manager.
	if (hScManager) {
		CloseServiceHandle(hScManager);
	}

	return ret;
}

int main(int argc, const char* argv[])
{
	if(argc < 2)
		return 1;

	switch(argv[1][0])
	{
	case 'r':
		if(StartDriver())
		{
			printf("install error\n");
			return 1;
		}
		break;
	case 's':
		if(StopDriver())
		{
			printf("remove error\n");
			return 1;
		}
		break;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值