怎样写一个 NT 服务程序
先说一下NT服务的特点:
1.随系统的启动而启动
2.隐蔽性比较好,一般的手动方式不能删除
3.只适用于NT内核的OS
WIN98下的请参考RegisterServiceProcess API,这个API函数将进程注册为一个服务模式的进程.
下面是我写好的class,你只要在这个程序中用这个包含这个.h文件就可以了。
======================
service.h
// Service.h: interface for the CService class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SERVICE_H__6ED74430_7123_484D_8CBD_A8E15381A869__INCLUDED_)
#define AFX_SERVICE_H__6ED74430_7123_484D_8CBD_A8E15381A869__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "winsvc.h"
class CService
{
public:
void Init_Service_Table_Entry();
CService();
virtual ~CService();
void InstallService();
static void WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv);
static void WINAPI BeginSrv(DWORD Opcode);
//
//static SERVICE_STATUS_HANDLE hServiceStatus;
//static SC_HANDLE scm,svc;
//static SERVICE_STATUS serviceStatus;
};
#endif // !defined(AFX_SERVICE_H__6ED74430_7123_484D_8CBD_A8E15381A869__INCLUDED_)
////////////////////////////
Service.cpp
// Service.cpp: implementation of the CService class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Service.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
SERVICE_STATUS_HANDLE hServiceStatus;
SC_HANDLE scm,svc;
SERVICE_STATUS serviceStatus;
CService::CService()
{
}
CService::~CService()
{
}
void WINAPI CService::BeginSrv(DWORD Opcode)
{
switch(Opcode)
{
case SERVICE_CONTROL_STOP:
serviceStatus.dwCurrentState =SERVICE_STOPPED;
SetServiceStatus (hServiceStatus,&serviceStatus);
break;
case SERVICE_CONTROL_CONTINUE:
serviceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus (hServiceStatus,&serviceStatus);
break;
case SERVICE_CONTROL_PAUSE:
serviceStatus.dwCurrentState = SERVICE_PAUSED;
SetServiceStatus (hServiceStatus,&serviceStatus);
break;
case SERVICE_CONTROL_INTERROGATE:
break;
}
SetServiceStatus (hServiceStatus,&serviceStatus);
}
void CService::Init_Service_Table_Entry()
{
SERVICE_TABLE_ENTRY ste[2];
ste[0].lpServiceName="VirusService";//Service Name
ste[0].lpServiceProc=ServiceMain; //service function Name
// the last one must be NULL
ste[1].lpServiceName=NULL;
ste[1].lpServiceProc=NULL;
StartServiceCtrlDispatcher(ste);
InstallService();
}
void WINAPI CService::ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
serviceStatus.dwServiceType = SERVICE_WIN32;
serviceStatus.dwCurrentState = SERVICE_START_PENDING;
serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_PAUSE_CONTINUE;
serviceStatus.dwServiceSpecificExitCode = 0;
serviceStatus.dwWin32ExitCode = 0;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
RegisterServiceCtrlHandler("VirusService",BeginSrv);
serviceStatus.dwCurrentState = SERVICE_RUNNING;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
SetServiceStatus(hServiceStatus,&serviceStatus);
serviceStatus.dwCurrentState = SERVICE_RUNNING;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 0;
SetServiceStatus(hServiceStatus,&serviceStatus); //set service status
}
void CService::InstallService()
{
//Get the modulefilename
char currentPath[128];
GetModuleFileName(NULL,currentPath,128);
char* p = strrchr(currentPath,´\\´); //检测最后是否以"
if (*p) //返回当前文件的路径
*p = 0;
LPCTSTR lpsysfilename;
lpsysfilename=(LPCTSTR)lstrcat(currentPath,"");
scm=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(scm!=NULL)
svc=CreateService(scm,"VirusService","VirusService",SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_IGNORE,lpsysfilename,NULL,NULL,NULL,NULL,NULL);
if(svc!=NULL)
svc=OpenService(scm,"VirusService",SERVICE_START);
if (svc!=NULL)
{
StartService(svc,0,NULL);
CloseServiceHandle(svc);
}
CloseServiceHandle(scm);
}
好了,这个服务程序到此就结束了,写个测试程序吧,然后用
CService ntservice;
ntservice.Init_Service_Table_Entry();
到你的控制面板,可以看到系统的服务中是否多了一个virusservice
本程序在winxp+vc6下测通过。
- · 如何在一个容器中放入不同对象(续)
- · “瑜珈山夜话” ----内存分配(一)
- · 最简单的屏幕拷贝程序(象素拷贝)
- · vc入门宝典(十)
- · Win32学习笔记 第六章 程序6-3 Typer 说明
- · 自动建库的安装程序制作方法
- · “瑜珈山夜话” ----内存分配(二)
- · 让Windows 2000/XP中的任意窗口透明起来
- · 不比不知道,一比吓一跳!
- · “瑜珈山夜话” ----内存分配(三)
- · WTL体系结构(3)
- · 控制台程序的事件處理
- · 深入DCOM之DCOM的发展史
- · Windows程序效率
- · C++入门解惑(2)——初探指针(上)
- · 读好文章,快速理解COM组件知识,10分钟深刻阅读,获得超过2000元的培训
- · WTL的消息机制
- · DirectShow应用——支持DVD播放
- · 浏览器集成教学 自定义浏览器
- · 计算机科学与技术学生学习基础材料参考
- · 关于std::list的sort函数在VC6下的修正
- · 数据结构学习(C++)——线性链式结构总结(代后记)【2】
- · 深入DCOM之STA
- · 在COM中使用数组参数-数组指针
- · 在COM中使用数组参数-SafeArray
- · 在COM中使用数组参数-ICollection
- · win2000下“打开”对话框实现2000的风格综述
- · WTL体系结构(4)
- · 深入DCOM之MTA
- · 调试Release版本应用程序
- · 简化异常安全代码
- · Nt/2000/xp平台下的存储控制模型(Access Control Model)
- · Direct3D9初级教程
- · What are you, Anyway
- · 泛型编程:再现Min和Max
- · 在IE工具栏上简单定制按钮
- · Nt/2000/xp平台下的存储控制模型(Access Control Model) (二)
- · Multithreading in .NET Applications
