#include <windows.h>
//全局变量
SERVICE_STATUS ServiceStatus; //服务状态
SERVICE_STATUS_HANDLE hStatus; //服务控制
//声明
VOID WINAPI ServiceHandler(DWORD fdwControl);
VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv);
DWORD WINAPI MyWork(LPVOID lpParam);
void main()
{
SERVICE_TABLE_ENTRY ServTable[2];//服务 SERVICE_TABLE_ENTRY 结构体
ServTable[0].lpServiceName = "MyTestService";//服务名
ServTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;//服务开始入口函数 ServiceMain()
//其后的(不需要的)数组设为空
ServTable[1].lpServiceName = NULL;
ServTable[1].lpServiceProc = NULL;
StartServiceCtrlDispatcher(ServTable);//通知服务控制程序,服务正运行
}
//服务入口函数
VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
DWORD result = 0;
DWORD specificError = 0xFFFFFFFF;
ServiceStatus.dwServiceType = SERVICE_WIN32;//服务类型win32
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;//设置服务当前状态,SERVICE_START_PENDING 表明服务正在初始化
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN;//服务接受的控制类型
//允许错误报告的代码,预定义在WinError.h中
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;//服务可以通过CheckPoint 报告当前处于进程的那一步
ServiceStatus.dwWaitHint = 0;//服务到达下一步的毫秒数
//注册服务控制句柄,函数ServiceHandler()
hStatus = RegisterServiceCtrlHandler("MyServiceName",(LPHANDLER_FUNCTION)ServiceHandler);
if(!hStatus)
return;
result = GetLastError();//检查上述过程是否有错误
if(result != NO_ERROR)//有错误
{
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
ServiceStatus.dwWin32ExitCode = result;
ServiceStatus.dwServiceSpecificExitCode = specificError;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
//初始化完成,开始设置服务状态
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
SetServiceStatus(hStatus,&ServiceStatus);//应用设置
HANDLE hThread = CreateThread(NULL,0,MyWork,NULL,0,NULL);//新建线程去执行服务,这里是 MyWork()函数
if(hThread == NULL)
{
return;
}
}
//服务控制函数
VOID WINAPI ServiceHandler(DWORD fdwControl)
{
switch(fdwControl)//根据DWORD fdwControl,对服务状态进行相应设置
{
case SERVICE_CONTROL_PAUSE:
{
ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
}
case SERVICE_CONTROL_CONTINUE:
{
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
}
case SERVICE_CONTROL_STOP:
{
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
ServiceStatus.dwWin32ExitCode = 0;
SetServiceStatus(hStatus, &ServiceStatus);
break;
}
case SERVICE_CONTROL_SHUTDOWN:
{
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
ServiceStatus.dwWin32ExitCode = 0;
SetServiceStatus(hStatus, &ServiceStatus);
break;
}
case SERVICE_CONTROL_INTERROGATE:
{
break;
}
default:
break;
}
SetServiceStatus(hStatus,&ServiceStatus);
return;
}
//实际的服务内容
DWORD WINAPI MyWork(LPVOID lpParam)
{
DWORD a = 0;
return a;
}
#include<stdio.h>
#include<windows.h>
//服务控制函数
void main()
{
SC_HANDLE hSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);//新建一个 SCManager
if(hSCManager == NULL)
{
return;
}
SC_HANDLE hService = OpenService(hSCManager,"AAA",SERVICE_ALL_ACCESS);//新建一个服务
if(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)//新建服务失败?
{
CreateService(hSCManager,
"MyService", //服务名称
"ShowMyService",//显示名称
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,//服务类型
SERVICE_AUTO_START,//何时启动服务
SERVICE_ERROR_IGNORE,//服务失败严重性
"C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\win32_Service\\Debug\\win32_Service.exe",//服务路径
NULL,NULL,NULL,NULL,NULL);
}
StartService(hService,NULL,NULL);//开始服务
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
}