Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序。这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面。这种服务非常适合在服务器上使用,或任何时候,为了不影响在同一台计算机上工作的其他用户,需要长时间运行功能时使用。
服务是有状态的,可以使用windows自带的服务管理程序sc.exe查看当前服务状态,这个状态是由我们代码控制的,最好在服务初始化是将服务设置为SERVICE_START+PENDING, 当初始化完设置为SERVICE_RUNNING
在设计服务程序时必须满足特定函数调用的流程。
1、有一个入口函数,Main()\WinMain();
2、调用 StartServiceCtrlDispatcher 把向 ServiceMain 的指针传递给 SCM;
3、ServiceMain 调用 RegisterServiceCtrlHandler 并注册 Handler;
所以服务主体程序一般由 Main、ServiceMain 和 Handler 三部分组成。
编写服务程序步骤:
1.main函数,由于windows服务不需要界面,所以大部分程序为控制台程序,所以程序主函数为main而不是WinMain
主函数中主要做的就是初始化一个SERVICE_TABLE_ENTRY分派表结构体,然后调用StartServiceCtrlDispatcher,这将把调用进程的主线程转换为控制分派器
该分派器启动一个新线程,该线程运行分派表中对应的SvcMain函数
#define SVCNAME TEXT("Myservice")
SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ SVCNAME, (LPSERVICE_MAIN_FUNCTION) SvcMain },
{ NULL, NULL }
};
if (!StartServiceCtrlDispatcher(DispatchTable))
{
printf("Failed to start service control dispatcher\n");
return -1;
}
2.SvcMain函数是服务的真正入口点,必须在 main() 中进行正确的定义,VOID WINAPI SvcMain(DWORD dwArgc, LPTSTR *lpszArgv),函数名可以任意,它的作用就是将要执行的任务放在函数中循环执行
在 SvcMain()中应该立即调用 RegisterServiceCtrlHandler() 注册一个 Handler 去处理服务控制程序的要求。
SERVICE_STATUS gSvcStatus;
SERVICE_STATUS_HANDLE gSvcStatusHandle;
HANDLE ghSvcStopEvent = NULL;
VOID WINAPI SvcMain( DWORD dwArgc, LPTSTR *lpszArgv )
{
gSvcStatusHandle = RegisterServiceCtrlHandlerEx(
SVCNAME,
SvcCtrlHandler,
NULL);
if( !gSvcStatusHandle )
{
OutputDebugStringA("[ServiceTest] Failed to register service control handler");
return;
}
gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
gSvcStatus.dwServiceSpecificExitCode = 0;
ReportSvcStatus( SERVICE_START_PENDING, NO_ERROR, 3000 );
SvcInit( dwArgc, lpszArgv );
}
VOID SvcInit( DWORD dwArgc, LPTSTR *lpszArgv)
{
OutputDebugStringA("[ServiceTest] SvcInit start");
ghSvcStopEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual reset event
FALSE, // not signaled
NULL); // no name
if ( ghSvcStopEvent == NULL)
{
ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
return;
}
// Report running status when initialization is complete.
ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );
// TO_DO: Perform work until service stops.
WaitForSingleObject( ghSvcStopEvent, INFINITE );
ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
}
VOID ReportSvcStatus( DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwWaitHint)
{
static DWORD dwCheckPoint = 1;
// Fill in the SERVICE_STATUS structure.
gSvcStatus.dwCurrentState = dwCurrentState;
gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;
gSvcStatus.dwWaitHint = dwWaitHint;
if (dwCurrentState == SERVICE_START_PENDING)
gSvcStatus.dwControlsAccepted = 0;
else gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
if ( (dwCurrentState == SERVICE_RUNNING) ||
(dwCurrentState == SERVICE_STOPPED) )
gSvcStatus.dwCheckPoint = 0;
else gSvcStatus.dwCheckPoint = dwCheckPoint++;
// Report the status of the service to the SCM.
SetServiceStatus( gSvcStatusHandle, &gSvcStatus );
}
3.SvcCtrlHandler,服务控制函数,包含一个 switch 语句,它用于分发由 SCM 发送的控制通知事件,常用的:
SERVICE_CONTROL_STOP
SERVICE_CONTROL_PAUSE
SERVICE_CONTROL_CONTINUE
SERVICE_CONTROL_START
用户分别对这些通知事件进行相应处理,然后将处理后的新服务的最新状态消息发送给 SCM。
DWORD WINAPI SvcCtrlHandler(DWORD dwCtrl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
{
// Handle the requested control code.
CString strHandle;
strHandle.Format(L"[ServiceTest] SvcCtrlHandler dwCtrl = %lu, dwEventType = %lu", dwCtrl, dwEventType);
OutputDebugString(strHandle);
switch(dwCtrl)
{
case SERVICE_CONTROL_STOP:
ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
// Signal the service to stop.
SetEvent(ghSvcStopEvent);
return NO_ERROR;
case SERVICE_CONTROL_DEVICEEVENT:
break;
case SERVICE_CONTROL_INTERROGATE:
// Fall through to send current status.
break;
case SERVICE_CONTROL_SESSIONCHANGE:
//收到登录/注销消息
break;
default:
break;
}
ReportSvcStatus(gSvcStatus.dwCurrentState, NO_ERROR, 0);
return NO_ERROR;
}
服务相关的操作
// 创建服务,注意"="后面有一个空格
sc create MyService binpath= C:\xxx.exe
// 启动服务器
sc start MyService
// 查询服务当前状态
sc query MyService
// 停止服务
sc stop MyService
// 删除服务
sc delete MyService
相关 API
OpenSCManager 打开服务管理器
CreateService 创建新的服务
OpenService 打开已有服务
ControlService 给服务发送控制命令
DeleteService 删除一个服务
服务程序会遇到的问题
在服务程序启动阶段在产品中出现过服务进程没运行起来的问题,根据启动服务时的日志看是由于服务启动超时,根据在网上查找相关错误原因有以下两种情况
1.Windows系统的服务超时时间默认是30s,当一个服务的启动时间超过这个时间后,服务管理器会认为服务存在异常,并视为启动失败,然后记录一些系统事件信息。但是有时候这个时间是比较短的,比如我们的snapshot服务,这就需要修改注册表来解决这个问题。注册表项为HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/ServicesPipeTimeout,这个值有可能不存在,如果不存在需要添加。类型为DWORD,单位是毫秒。
2.Windows存在一个内存管理机制,内存映射文件在卸载的时候,并不会立即去释放内存,释放内存的时机是由操作系统来决定的。当程序在很短的时间内又重新加载此内存映射文件时,操作系统发现此内存映射文件还存放在内存中,那么就不会再加载了。所以停掉服务后不要立马再次运行服务