VC+++ 实现window后台服务

头文件 WinServer.h

#pragma once
//使用智能指针的头文件
#include <memory>
//服务程序入口头文件
#include "AccountHandler_RPC.h"
//既然是编写window后台服务, 肯定要包含window头文件
#include <Windows.h>
//服务名称
#define SERVICE_NAME L"AccoutSync"
using namespace std;
class WinServer
{
public:
    WinServer();
    ~WinServer();

    bool StartServer();
private:
    static void WINAPI RegisterServer(int argc, char** argv);
    static void WINAPI ServiceHandler(DWORD fdwControl);
    static DWORD WINAPI SerWorkThread(LPVOID para);
private:
    SERVICE_TABLE_ENTRY m_SerTable[2];
    SERVICE_STATUS m_SerStatus;
    SERVICE_STATUS_HANDLE m_hSerStatusHandle;
    HANDLE m_SerHandle;
    shared_ptr<AccountHandler_RPC> m_AccSyncHandle;
};

  源文件

#include "WinServer.h"
#include <stdio.h>

#pragma warning(disable:4996)

static WinServer* g_glpbalObj;

WinServer::WinServer()
{
	m_AccSyncHandle = make_shared<AccountHandler_RPC>();
	g_glpbalObj = this;
}

WinServer::~WinServer()
{

}

bool WinServer::StartServer()
{
	m_SerTable[0].lpServiceName = SERVICE_NAME;
	m_SerTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)RegisterServer;
	m_SerTable[1].lpServiceName = NULL;
	m_SerTable[1].lpServiceProc = NULL;
	//启动服务的控制分派机线程
	if (StartServiceCtrlDispatcher(m_SerTable) == FALSE)
	{
		printf("启动失败....错误编码: %d\n", GetLastError());
		return false;
	}
	printf("服务启动成功");
	return true;
}

void WinServer::RegisterServer(int argc, char** argv)
{
	g_glpbalObj->m_SerStatus.dwServiceType = SERVICE_WIN32;
	g_glpbalObj->m_SerStatus.dwCurrentState = SERVICE_START_PENDING;
	g_glpbalObj->m_SerStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
	g_glpbalObj->m_SerStatus.dwWin32ExitCode = 0;
	g_glpbalObj->m_SerStatus.dwServiceSpecificExitCode = 0;
	g_glpbalObj->m_SerStatus.dwCheckPoint = 0;
	g_glpbalObj->m_SerStatus.dwWaitHint = 0;
	g_glpbalObj->m_hSerStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceHandler);
	if (g_glpbalObj->m_hSerStatusHandle == 0)
	{
		DWORD nError = GetLastError();
	}
	//服务线程入口
	g_glpbalObj->m_AccSyncHandle->StartThread();
	g_glpbalObj->m_SerHandle = CreateThread(NULL, NULL, SerWorkThread, NULL, 0, 0);
	if (g_glpbalObj->m_SerHandle == INVALID_HANDLE_VALUE)
	{
		DWORD nError = GetLastError();
		return;
	}
	// 初始化完成, 记录服务运行状态
	g_glpbalObj->m_SerStatus.dwCurrentState = SERVICE_RUNNING;
	g_glpbalObj->m_SerStatus.dwCheckPoint = 0;
	g_glpbalObj->m_SerStatus.dwWaitHint = 9000;
	if (!SetServiceStatus(g_glpbalObj->m_hSerStatusHandle, &g_glpbalObj->m_SerStatus))
	{
		DWORD nError = GetLastError();
	}
}

DWORD WinServer::SerWorkThread(LPVOID para)
{
	while (true)
	{
		Sleep(100);
	}
	return 0;
}

void WinServer::ServiceHandler(DWORD fdwControl)
{
	switch (fdwControl)
	{
	case SERVICE_CONTROL_STOP:
	case SERVICE_CONTROL_SHUTDOWN:
		g_glpbalObj->m_SerStatus.dwWin32ExitCode = 0;
		g_glpbalObj->m_SerStatus.dwCurrentState = SERVICE_STOPPED;
		g_glpbalObj->m_SerStatus.dwCheckPoint = 0;
		g_glpbalObj->m_SerStatus.dwWaitHint = 0;
		//退出服务代码
		//g_glpbalObj->m_SerProcessPtr->UnInit();
		break;
	default:
		return;
	};
	if (!SetServiceStatus(g_glpbalObj->m_hSerStatusHandle, &g_glpbalObj->m_SerStatus))
	{
		DWORD nError = GetLastError();
	}
}  

main函数调用

#include "winServer.h"
#define SERVICES
int main()
{
#ifdef SERVICES
	WinServer service;
	service.StartServer();
#else
	AccountHandler_RPC AccHandle;
	AccHandle.StartThread();
return 0; }

  

在window上面安装服务

方法1:

 使用sc(server command)命令安装后台服务

1: 创建后台服务

sc create 服务名称 binPath= "exe程序路径"(备注: =号后面一定要有一个空格, 不然服务创建失败)

2: 启动后台服务

sc start 服务名称

 

方法2: 使用创建window服务小工具(上传不了, 需要的邮箱留言, 单独发送)

posted @ 2020-05-11 10:41  Software_hul  阅读(1165)  评论(0编辑  收藏  举报