当前位置:
文档之家› C++编写Windows服务程序
C++编写Windows服务程序
创建服务程序除了编写服务代码外,还必须做一些其它额外的编码工作:
• 在系统日志或应用程序日志中报告警告信息和出错信息,不能用输出到屏 幕的方式,因为用户根本就没有登陆。 • 服务程序的控制即可以通过单独的应用程序,也可以通过控制面版程序。 这取决于你的服务实现什么样的通讯机制。 • 从系统中安装和卸载服务
简介
Windows NT 中 的 服 务 实 际 上 是 一 个 程 序 , 只 要 计 算 机 操 作 系 统 一 启 动 , 服 务就可以运行其中。它不需要用户登陆。服务程序是一种与用户无关的任务,比 如 目 录 复 制 , 进 程 监 控 或 网 络 上 供 其 它 机 器 使 用 的 服 务 , 比 如 HTTP 协 议 支 持 。
gSvcStatus.dwServiceSpecificExitCode = 0;
// Report initial status to the SCM ReportSvcStatus( SERVICE_START_PENDING, NO_ERROR, 3000 );
// Perform service-specific initialization and work. ghSvcStopEvent = CreateEvent(
hEventSource = RegisterEventSource(NULL, szName);
if( NULL != hEventSource )
{ //StringCchPrintf(Buffer, 80, TEXT("%s failed with %d"), szFunction,
GetLastError()); strcpy(Buffer,szFunction); lpszStrings[0] = szName; lpszStrings[1] = Buffer;
用 C++ 创 建 简 单 的 Win32 服 务 程 序 作 者 : Nigel Thomson( MSDN 技 术 组 )
翻 译 : NorthTibet 原文出处:Creating a Simple Win32 Service in C++ 下载 NTService 例子源代码 下载 NTServCpl 例子源代码 下载 NTServCtrl 例子源代码
DWORD dwWin32ExitCode,
DWORD dwWaitHint)
static DWORD dwCheckPoint = 1;
// Fill in the SERVICE_STATUS structure.
gSvcStatus.dwCurrentState = dwCurrentState; gSvcStatus.dwWin32ExitCode = dwWin32ExitCode; gSvcStatus.dwWaitHint = dwWaitHint;
• NTServic e 是 一 个 简 单 的 Win32 服 务 , 它 就 是 用 本 文 所 描 述 的 方 法 建 立 的; • NTServC pl 是 一 个 控 制 面 版 程 序 , 用 来 控 制 NTServic e 服 务 ; • NTServC trl 是 一 个 独 立 的 程 序 例 子 , 用 它 可 以 监 控 某 个 Win32 服 务 ;
C++编写 Windows 服务程序
2010-10-28 18:16:05| 分类: vc++ |字号大中小 订阅
环境 VC6.0
#include "windows.h"
SERVICE_STATUS
gSvcStatus; //服务状态
SERVICE_STATUS_HANDLE gSvcStatusHandle; //服务状态句柄
while(1) {
// Check whether to stop the service. WaitForSingleObject(ghSvcStopEvent, INFINITE); ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 ); return; } } void ServerReportEvent(LPTSTR szName,LPTSTR szFunction) { HANDLE hEventSource; LPCTSTR lpszStrings[2]; unsigned int len = sizeof(szFunction); TCHAR *Buffer = new TCHAR[len];
创 建 Windows NT 服 务 程 序 并 不 是 很 难 。但 调 试 某 个 服 务 程 序 不 是 一 件 容 易 的 事 。 就 我 自 己 而 言 , 我 喜 欢 用 Visual C++ 编 写 自 己 的 C++ 程 序 。 大 多 数 Win32 服 务 都 是 用 C 写 的 , 所 以 我 觉 得 如 果 用 某 个 C++ 类 来 实 现 Win32 服 务 的 基 本 功 能 一 定 很 有 意 思 。有 了 这 个 C++ 类 ,谁 要 想 用 C++ 创 建 Win32 服 务 就 是 一 件 很 简 单 的 事 情 了 。 我 为 此 开 发 了 一 个 C++ 基 类 , 用 它 作 为 编 写 Win32 服 务 的 起 点 应 该 没 有 什 么 大 问 题 。
SERVER_NAME, SvcCtrlHandler);
if( !gSvcStatusHandle ) {
ServerReportEvent(SERVER_NAME,TEXT("RegisterServiceCtrlHandler")); return; }
// These SERVICE_STATUS members remain as set here gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; //只有一个 单独的服务
2,
// size of lpszStrings array
0,
// no binary data
lpszStrings,
// array of strings
NULL);
// no binary data
DeregisterEventSource(hEventSource);
}
}
VOID ReportSvcStatus( DWORD dwCurrentState,
void main()
{
SERVICE_TABLE_ENTRY DispatchTable[] = {
{ SERVER_NAME, (LPSERVICE_MAIN_FUNCTION)Server_main }, { NULL, NULL }
};
if (!StartServiceCtrlDispatcher(DispatchTable)) {
// Handle the requested control code. switch(dwCtrl)
{ case SERVICE_CONTROL_STOP:
ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);
// Signal the service to stop.
// Report running status when initialization is complete. ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );
// TO_DO: Perform work until service stops. ServerProgram(dwArgc, lpszArgv); //你需要的操作在此添加代码
if (dwCurrentState == SERVICE_START_PENDING) gSvcStatus.dwControlsAccepted = 0;
else gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
if ( (dwCurrentState == SERVICE_RUNNING) || (dwCurrentState == SERVICE_STOPPED) ) gSvcStatus.dwCheckPoint = 0;
NULL, // default security attributes TRUE, // manual reset event FALSE, // not signaled NULL); // no name
if ( ghSvcStopEvent == NULL) {
ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 ); return; }
摘要 本 文 描 述 如 何 用 Visual C++ 创 建 Windows NT 服 务 程 序 。 创 建 该 服 务 仅
用 到 一 个 C++ 类 , 这 个 类 提 供 服 务 与 操 作 系 统 之 间 一 个 简 单 的 接 口 。 使 用 这 个 类实现自己的服务非常简单,只要改写少数几个基类中的虚拟函数即可。在本文 有三个源代码参考例子:
HANDLE
ghSvcStopEvent = NULL;//服务停止句柄
#define SERVER_NAME TEXT("my_server") //服务名
VOID WINAPI ServerMain( DWORD, LPTSTR *); //服务入口点
void ServerReportEvent(LPTSTR szName,LPTSTR szFunction); //写 Windows 日志