//stdafx.h
#pragma once
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <io.h>
// ServiceTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#define SERVICE_NAME "MyTestService"
#define CS_TIMEOUT 5000
static SERVICE_STATUS hServStatus;
static SERVICE_STATUS_HANDLE hSStat;
volatile static BOOL ShutDown = FALSE;
TCHAR str[256];
DWORD WINAPI HandleControlEx(DWORD dwControl, DWORD dwEventType,LPVOID lpEventData,LPVOID lpContext);
void WINAPI ServiceMain(DWORD argc, LPTSTR argv[]);
void UpdateStatus (int NewStatus, int Check);
VOID SvcInstall();
int _tmain(int argc, _TCHAR* argv[])
{
if( lstrcmpi( argv[1], TEXT("install")) == 0 ){
SvcInstall();
return 0;
}
SERVICE_TABLE_ENTRY DispatchTable[] = {
{ SERVICE_NAME, ServiceMain },
{ NULL, NULL }
};
if(!StartServiceCtrlDispatcher(DispatchTable))
OutputDebugString("Failed to connect to SCManager.");
return 0;
}
VOID SvcInstall()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
TCHAR szPath[MAX_PATH];
if( !GetModuleFileName( NULL, szPath, MAX_PATH ) ){
sprintf(str,"Cannot install service (%d)/n", GetLastError());
OutputDebugString(str);
return;
}
// Get a handle to the SCM database.
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (NULL == schSCManager){
sprintf(str,"OpenSCManager failed (%d)/n", GetLastError());
OutputDebugString(str);
return;
}
// Create the service
schService = CreateService(schSCManager, SERVICE_NAME, SERVICE_NAME, SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,szPath,
NULL, NULL, NULL, NULL, NULL);
if (schService == NULL){
sprintf(str,"CreateService failed (%d)/n", GetLastError());
OutputDebugString(str);
CloseServiceHandle(schSCManager);
return;
}
sprintf(str,"Service installed successfully/n");
OutputDebugString(str);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
int InitService()
{
OutputDebugString("service is started.");
return 0;
}
void WINAPI ServiceMain(DWORD argc, LPTSTR argv[])
{
int error;
hSStat = RegisterServiceCtrlHandlerEx(SERVICE_NAME,HandleControlEx,NULL);
hServStatus.dwServiceType = SERVICE_WIN32;
hServStatus.dwCurrentState = SERVICE_START_PENDING;
hServStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN ;
hServStatus.dwWin32ExitCode = 0;
hServStatus.dwServiceSpecificExitCode = 0;
hServStatus.dwCheckPoint = 0;
hServStatus.dwWaitHint = 2 * CS_TIMEOUT;
SetServiceStatus (hSStat, &hServStatus);
error = InitService();
if(error){
UpdateStatus(SERVICE_STOPPED, -1);
}
UpdateStatus(SERVICE_RUNNING, -1);
while(!ShutDown){
Sleep(3000);
OutputDebugString("Service is running./n");
//UpdateStatus(-1,-1);
}
}
DWORD WINAPI HandleControlEx(DWORD dwControl, DWORD dwEventType,LPVOID lpEventData,LPVOID lpContext)
{
switch(dwControl){
//case SERVICE_CONTROL_PAUSE:
// printf("%s","/n service is paused./n");
// break;
//case SERVICE_CONTROL_CONTINUE:
// printf("%s","/n service is continued. /n");
// break;
case SERVICE_CONTROL_PAUSE:
ShutDown = TRUE;
Sleep(1000);
UpdateStatus (SERVICE_PAUSE_PENDING, -1);
OutputDebugString("service is to be paused./n");
hServStatus.dwWin32ExitCode = NO_ERROR;
UpdateStatus (SERVICE_PAUSED, -1);
OutputDebugString("service is paused./n");
break;
case SERVICE_CONTROL_STOP:
ShutDown = TRUE;
Sleep(1000);
UpdateStatus (SERVICE_STOP_PENDING, -1);
OutputDebugString("service is to be stopped./n");
hServStatus.dwWin32ExitCode = NO_ERROR;
UpdateStatus (SERVICE_STOPPED, -1);
OutputDebugString("service is stopped./n");
break;
default:
if (dwControl > 127 && dwControl < 256) /* User Defined */
break;
}
return 1;
}
void UpdateStatus (int NewStatus, int Check)
/* Set a new service status and checkpoint (either specific value or increment) */
{
if(Check < 0 ){
hServStatus.dwCheckPoint++;
}else{
hServStatus.dwCheckPoint = Check;
}
if(NewStatus >= 0){
hServStatus.dwCurrentState = NewStatus;
}
if (!SetServiceStatus (hSStat, &hServStatus))
OutputDebugString("Can not set service status./n");
return;
}
本程序所写的服务在启动后会不停的在DebugView中输出信息.本程序也集成了安装操作.
安装时在控制台切换到本程序编译后的可执行文件的文件夹,输入:ServcieTest install.
安装后打开DebugView,在管理工具--服务中找到MyTestService启动或停止,看输出的信息.
本程序可以当作模板来加入实际的处理代码.