服务工具 - 注册/删除/启动/停止
学习编写服务程序, 先写一个这个便于调试程序, 呵呵, 各位指点一二
// ServiceTool.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include "winsvc.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
//
//安装服务
void InstallService( LPCTSTR lpszName, LPCTSTR lpszDescription, LPCTSTR lpszPath )
{
SC_HANDLE scm;
scm = OpenSCManager( 0, 0, SC_MANAGER_CREATE_SERVICE );
if ( !scm )
cerr << "in OpenSCMager: " << GetLastError() << endl;
//安装服务
cout << "开始注册服务..." << endl;
SC_HANDLE newService;
newService = CreateService( scm, lpszName, lpszDescription,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
lpszPath, 0, 0, 0, 0, 0 );
if ( !newService )
cerr << "In CreateService: " << GetLastError() << endl;
else
cout << "服务已经注册完毕..." << endl;
CloseServiceHandle( newService );
CloseServiceHandle( scm );
}
//
//删除服务
void RemoveService( LPCTSTR lpszName )
{
SC_HANDLE scm;
scm = OpenSCManager( 0, 0, SC_MANAGER_CREATE_SERVICE );
if ( !scm )
cerr << "in OpenSCMager: " << GetLastError() << endl;
//安装服务
cout << "开始删除服务..." << endl;
SC_HANDLE service;
service = OpenService( scm, lpszName, SERVICE_ALL_ACCESS | DELETE );
if ( !service )
cerr << "In OpenService:" << GetLastError() << endl;
SERVICE_STATUS status;
BOOL isSuccess = QueryServiceStatus( service, &status );
if ( !isSuccess )
cerr << "In QueryServiceStatus: " << GetLastError() << endl;
if ( status.dwCurrentState!=SERVICE_STOPPED ){
cout << "停止服务..." << endl;
isSuccess = ControlService( service, SERVICE_CONTROL_STOP, &status );
if ( !isSuccess )
cerr << "In ControlService: " << GetLastError() << endl;
Sleep( 500 );
}
isSuccess = DeleteService( service );
if ( !isSuccess )
cerr << "In DeleteService: " << GetLastError() << endl;
else
cout << "服务已经被删除..." << endl;
CloseServiceHandle( service );
CloseServiceHandle( scm );
}
//
//启动服务
void StartService( LPCTSTR lpszName )
{
SC_HANDLE scm = NULL, service = NULL;
scm = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if ( !scm ){
cerr << "Error Openning SCManager" << endl;
return;
}
service = OpenService( scm, lpszName, SERVICE_ALL_ACCESS );
if ( !service ){
cerr << "Error Openning " << lpszName << "Service" << endl;
return;
}
if ( StartService( service, 0, NULL ) ){
cout << "启动服务成功..." << endl;
}
CloseServiceHandle( service );
CloseServiceHandle( scm );
}
//
//停止服务
void StopService( LPCTSTR lpszName )
{
SC_HANDLE scm = NULL, service = NULL;
scm = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if ( !scm ){
cerr << "Error Openning SCManager" << endl;
return;
}
service = OpenService( scm, lpszName, SERVICE_ALL_ACCESS );
if ( !service ){
cerr << "Error Openning " << lpszName << "Service" << endl;
return;
}
SERVICE_STATUS status;
ControlService( service, SERVICE_CONTROL_STOP, &status);
CloseServiceHandle( service );
CloseServiceHandle( scm );
}
int main(int argc, char* argv[])
{
int nChoice = 0;
while ( nChoice != 5 ){
cout << "请选择操作( 1 - 安装服务, 2 - 删除服务, 3 - 启动服务, 4 - 停止服务, 5 - 退出 ):";
cin >> nChoice;
switch ( nChoice ) {
case 1:
{
char szName[512] = "";
char szDes[512] = "";
char szPath[512] = "";
cout << endl << "输入服务名称: ";
cin >> szName;
cout << endl << "输入服务描述: ";
cin >> szDes;
cout << endl << "输入服务文件路径: ";
cin >> szPath;
InstallService( szName, szDes, szPath );
break;
}
case 2:
{
char szName[512] = "";
cout << endl << "输入服务名称: ";
cin >> szName;
RemoveService( szName );
break;
}
case 3:
{
char szName[512] = "";
cout << endl << "输入服务名称: ";
cin >> szName;
StartService( szName );
break;
}
case 4:
{
char szName[512] = "";
cout << endl << "输入服务名称: ";
cin >> szName;
StopService( szName );
break;
}
}
}
return 0;
}