资源简介
学习如何编写Windows Service 服务程序的很好例子,可以直接在例子的基础上修改并编译运行,实用性很强.
运行环境: Windows NT/2000/XP
例子程序示例了如何在Windows NT/2000/XP系统中create, start, stop, 和 delete serverice
程序使用了如下的 API命令:
OpenSCManager()
CloseServiceHandle()
CreateService()
OpenService()
SetServiceStatus()
DeleteService()
RegisterServiceCtrlHandler()
StartServiceCtrlDispatcher()
LPSERVICE_MAIN_FUNCTION
LPHANDLER_FUNCTION
程序把所有的命令写入记录文件C:\DemoService.txt,可以试着以administrator和guest等权限运行程序.
程序有两种运行模式:
Service.exe (无参数) GUI运行
Service.exe /Service 服务模式运行

代码片段和文件信息
// cService.cpp: implementation of the cService class.
//
//////////////////////////////////////////////////////////////////////
#include “stdafx.h“
#include “Service.h“
#include “cService.h“
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern void Output(CString Out);
// global instance of Service
cService g_Service;
cService::cService()
{
}
cService::~cService()
{
}
// return Errorcode or 0
// Install the service when user clicks the Create button
DWORD cService::Create()
{
SC_HANDLE hdlSCM = OpenSCManager(0 0 SC_MANAGER_CREATE_SERVICE);
CString Calling = ::GetCommandLine();
Calling += “ /Service“;
if (hdlSCM == 0) return ::GetLastError();
SC_HANDLE hdlServ = CreateService(
hdlSCM // SCManager database
ServiceName // name of service
ServiceDisplayName // service name to display
STANDARD_RIGHTS_REQUIRED // desired access
SERVICE_WIN32_OWN_PROCESS // service type
SERVICE_DEMAND_START // start type
SERVICE_ERROR_NORMAL // error control type
Calling // service‘s binary Path name
0 // no load ordering group
0 // no tag identifier
0 // no dependencies
0 // LocalSystem account
0); // no password
DWORD Ret = 0;
if (!hdlServ) Ret = ::GetLastError();
CloseServiceHandle(hdlServ);
return Ret;
}
// return Errorcode or 0
// Uninstall the service when user clicks the Delete button
DWORD cService::Delete()
{
SC_HANDLE hdlSCM = OpenSCManager(0 0 STANDARD_RIGHTS_REQUIRED);
if (!hdlSCM) return ::GetLastError();
SC_HANDLE hdlServ = OpenService(hdlSCM ServiceName DELETE);
DWORD Ret = 0;
if (!DeleteService(hdlServ)) Ret = ::GetLastError();
CloseServiceHandle(hdlServ);
return Ret;
}
// return Errorcode or 0
// Start the service when user clicks the Start button
DWORD cService::Start()
{
SC_HANDLE hdlSCM = OpenSCManager(0 0 STANDARD_RIGHTS_REQUIRED);
if (!hdlSCM) return ::GetLastError();
SC_HANDLE hdlServ = OpenService(hdlSCM ServiceName SERVICE_START);
DWORD Ret = 0;
if (!StartService(hdlServ 0 0)) Ret = ::GetLastError();
CloseServiceHandle(hdlServ);
return Ret;
}
// return Errorcode or 0
// Stop the service when user clicks the Stop button
DWORD cService::Stop()
{
SC_HANDLE hdlSCM = OpenSCManager(0 0 STANDARD_RIGHTS_REQUIRED);
if (!hdlSCM) return ::GetLastError();
SC_HANDLE hdlServ = OpenService(hdlSCM ServiceName SERVICE_STOP);
SERVICE_STATUS ServStat;
DWORD Ret = 0;
if (!ControlService(hdlServ SERVICE_CONTROL_STOP &ServStat)) Ret = ::GetLastError();
CloseServiceHandle(hdlServ);
return Ret;
}
// this function must be in global namespace (Windows API callback)
void ApiServiceMainStarter(DWORD argc L
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2003-07-17 20:55 RES\
文件 399 2000-11-20 16:42 RES\Service.rc2
文件 1078 2000-11-20 16:42 RES\Service.ico
文件 853 2003-07-17 20:58 __Read_Me.txt
文件 7075 2003-07-17 21:47 cService.cpp
文件 904 2003-06-30 14:37 cService.h
文件 777 2003-06-30 15:00 resource.h
文件 1175 2003-06-30 15:49 Service.clw
文件 2006 2003-06-30 15:41 Service.cpp
文件 4302 2003-06-30 14:39 Service.dsp
文件 539 2000-11-20 16:42 Service.dsw
文件 1335 2000-11-20 16:42 Service.h
文件 5295 2003-07-17 21:51 Service.rc
文件 8000 2003-06-30 15:47 ServiceDlg.cpp
文件 1369 2003-06-30 15:18 ServiceDlg.h
文件 209 2000-11-20 16:42 StdAfx.cpp
文件 999 2000-11-20 16:42 StdAfx.h
相关资源
-
Windows em
bedded Compact 2013 应用开发调 - 黑苹果硬件兼容检测和查询软件
- Uninstall_Cortana_WINCLIENT.CN.rar
- VMware65_SLP_DeLLSLIC2.1
- Windows异步套接字网络编程
- WINDOWS98启动盘镜像Win98.IMA
- 仿windows记事本
- windows7用的,非常漂亮的透明计时器
- WinCE中的Web Service调用
- windows下制作macOS安装U盘,绝对简单好
- keil vcom windows 7 64bit 驱动
- windows ce 系统的GPIO驱动程序
- TCP 发包工具(windows)
- 微软的可以删除系统卸不干净的软件
- windows下生成MD5值的工具(WinMD5)
- windows cygwin ns2安装步骤
- 通过webservice上传和下载文件
- WinAPI 函数库(大全)
- 解决在Windows XP SP2下不能显示验证码的
- 基于SSM+CXF构建的RESTFul webservice
- 加快Windows XP操作系统开机速度
- Windows 1.0 软盘镜像
- Windows下访问LINUX的利器-SSH
- ChilledWindows.exe(玩笑病毒)
- CCLicenseService
- NDK-R12B windows-x86_64百度云盘
- windows3.2简体中文版,虚拟机文件
- Windows 3.0 安装软盘(3.5 720k)
- WINDOWS内核安全编程 寒江独钓 光盘源
- 寒江独钓-Windows内核安全编程(完整版
评论
共有 条评论