用VC++建立Service服务应用程序

 

本文转自:http://blog.csdn.net/niying/archive/2006/08/12/1054180.aspx

 

为什么要使用服务应该程序呢?服务程序就像系统的一些服务一样,能够自动地启动,并执行相应的操作;而且因为服务程序的在层次上和一般的应用程序不同,其能够在系统启动时就自动地运行,而不像一般的应用程序那样一定要在登陆后才能运行,这些就是服务的一些好处了,如果你也想你的程序具有这样的功能,那么你就可以建立一个服务应用程序了。

下面就跟着我一步一步地教你怎么去创建一个服务应用程序吧。

 

本文主要介绍了OpenSCManagerCreateServiceOpenServiceControlServiceDeleteServiceRegisterServiceCtrlHandlerSetServiceStatusStartServiceCtrlDispatcher等操作服务程序的主要几个API的用法,具体的函数参数大家可以查阅MSDN

 

一、建立Win32 Application应用程序(当然你也可以建立其它的应用程序,但服务一般是没有用户界面的),并命名为ServiceTest

 

二、定义全局函数变量。

//定义全局函数变量

void Init();

BOOL IsInstalled();

BOOL Install();

BOOL Uninstall();

void LogEvent(LPCTSTR pszFormat, ...);

void WINAPI ServiceMain();

void WINAPI ServiceStrl(DWORD dwOpcode);

 

TCHAR szServiceName[] = _T("ServiceTest");

BOOL bInstall;

SERVICE_STATUS_HANDLE hServiceStatus;

SERVICE_STATUS status;

DWORD dwThreadID;

 

三、添加Init初始化函数。这里主要是设置服务句柄和状态。

hServiceStatus = NULL;

status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;

status.dwCurrentState = SERVICE_STOPPED;

tatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;

status.dwWin32ExitCode = 0;

status.dwServiceSpecificExitCode = 0;

status.dwCheckPoint = 0;

status.dwWaitHint = 0;

 

四、添加安装和删除服务函数。这里主要是用到了五个函数OpenSCManagerCreateServiceOpenServiceControlServiceDeleteServiceOpenSCManager用于打开服务控制管理器;CreateService用于创建服务;OpenService用于打开已有的服务,返回该服务的句柄;ControlService则用于控制已打开的服务状态,这里是让服务停止后才删除;DeleteService用于删除指定服务。

BOOL Install();

{

//这里列出主要的两个函数,其它的可以在代码里找。

//打开服务控制管理器

OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

    //创建服务

  SC_HANDLE hService = ::CreateService(

        hSCM, szServiceName, szServiceName,

        SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,

        SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,

        szFilePath, NULL, NULL, _T(""), NULL, NULL);

 

    ::CloseServiceHandle(hService);

    ::CloseServiceHandle(hSCM);

}

BOOL Uninstall();

{

//这里列出主要的两个函数,其它的可以在代码里找。

//打开服务控制管理器

OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

//打开服务

OpenService(hSCM, szServiceName, SERVICE_STOP | DELETE);

//停止服务

ControlService(hService, SERVICE_CONTROL_STOP, &status);

//删除服务

DeleteService(hService);

    …

}

五、添加服务主线程函数和控制函数。这里调用RegisterServiceCtrlHandler来注册服务的控制函数,这里要设置status.dwControlsAcceptedSERVICE_ACCEPT_STOP,否则你不能控制这个服务的状态。

void WINAPI ServiceMain()

{

    // Register the control request handler

    status.dwCurrentState = SERVICE_START_PENDING;

    status.dwControlsAccepted = SERVICE_ACCEPT_STOP;//这个要使用,否则你不能控制

 

    //注册服务控制

    hServiceStatus = RegisterServiceCtrlHandler(szServiceName, ServiceStrl);

    if (hServiceStatus == NULL)

    {

        LogEvent(_T("Handler not installed"));

        return;

    }

    SetServiceStatus(hServiceStatus, &status);

 

    status.dwWin32ExitCode = S_OK;

    status.dwCheckPoint = 0;

    status.dwWaitHint = 0;

    status.dwCurrentState = SERVICE_RUNNING;

    SetServiceStatus(hServiceStatus, &status);

 

    //模拟服务的运行,10后自动退出。应用时将主要任务放于此即可

    int i = 0;

    while (i < 10)

    {

           Sleep(1000);

           i++;

    }

    //

 

    status.dwCurrentState = SERVICE_STOPPED;

    SetServiceStatus(hServiceStatus, &status);

    LogEvent(_T("Service stopped"));

}

 

六、在主线程函数里注册控制函数和程序执行主体。

void WINAPI ServiceMain()

{

       //如上,这里主要是说明这就是程序的执行体

    //模拟服务的运行,10后自动退出。应用时将主要任务放于此即可

    int i = 0;

    while (i < 10)

    {

           Sleep(1000);

           i++;

    }

    …

}

 

七、最后,要在main函数里注册添加安装、删除、注册主函数。

int APIENTRY WinMain(HINSTANCE hInstance,

                     HINSTANCE hPrevInstance,

                     LPSTR     lpCmdLine,

                     int       nCmdShow)

{

    Init();

    dwThreadID = ::GetCurrentThreadId();

    SERVICE_TABLE_ENTRY st[] =

    {

        { szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain },

        { NULL, NULL }

};

 

    if (stricmp(lpCmdLine, "/install") == 0)

    {

           Install();

    }

    else if (stricmp(lpCmdLine, "/uninstall") == 0)

    {

           Uninstall();

    }

    else

    {

           if (!::StartServiceCtrlDispatcher(st))

           {

                  LogEvent(_T("Register Service Main Function Error!"));

           }

    }

    return 0;

}

 

八、总结。其实做一个服务程序并不难,主要是懂得程序的执行体放于哪里?和注册程序的主函数和注册控制函数,如果这两个没有注册的话,你的程序就不知道如何去控制了。status.dwControlsAccepted = SERVICE_ACCEPT_STOP;这个也重要,如果你没有设置的话,那么服务就不会受你控制了。

 

 源码下載:http://d.download.csdn.net/down/158720/niying

 

我的例子:

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
#include "windows.h"  
#include <iostream>

using namespace std;
#if defined(_WIN32)


SERVICE_STATUS          gSvcStatus;  //服务状态  
SERVICE_STATUS_HANDLE   gSvcStatusHandle; //服务状态句柄  
HANDLE                  ghSvcStopEvent = NULL;//服务停止句柄  
#define SERVER_NAME    TEXT("LincTestServer") //服务名  
VOID WINAPI Server_main( DWORD, LPTSTR *); //服务入口点  
void ServerReportEvent(LPTSTR szName,LPTSTR szFunction); //写Windows日志  
VOID ReportSvcStatus( DWORD, DWORD, DWORD ); //服务状态和SCM交互  
VOID WINAPI SvcCtrlHandler( DWORD );  //SCM回调函数  
VOID ServerProgram(DWORD, LPTSTR *); //服务主程序  

void main()  
{   
	SERVICE_TABLE_ENTRY DispatchTable[] =  
	{  
		{ SERVER_NAME, (LPSERVICE_MAIN_FUNCTION)Server_main },  
		{ NULL, NULL }  
	};  

	if (!StartServiceCtrlDispatcher(DispatchTable))  
	{  
		ServerReportEvent(SERVER_NAME,TEXT("StartServiceCtrlDispatcher"));  
	}  
}  
static VOID WINAPI Server_main(DWORD dwArgc, LPTSTR *lpszArgv )  
{  
	// Register the handler function for the service  
	gSvcStatusHandle = RegisterServiceCtrlHandler(   
		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; //只有一个单独的服务  
	gSvcStatus.dwServiceSpecificExitCode = 0;      

	// Report initial status to the SCM  
	ReportSvcStatus( SERVICE_START_PENDING, NO_ERROR, 3000 );  

	// Perform service-specific initialization and work.  
	ghSvcStopEvent = CreateEvent(  
		NULL,    // default security attributes  
		TRUE,    // manual reset event  
		FALSE,   // not signaled  
		NULL);   // no name  

	if ( ghSvcStopEvent == NULL)  
	{  
		ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );  
		return;  
	}  

	// Report running status when initialization is complete.  
	ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );  

	// TO_DO: Perform work until service stops.   
	ServerProgram(dwArgc, lpszArgv); //你需要的操作在此添加代码  

	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];  

	hEventSource = RegisterEventSource(NULL, szName);  

	if( NULL != hEventSource )  
	{  
		//StringCchPrintf(Buffer, 80, TEXT("%s failed with %d"), szFunction, GetLastError());  
		strcpy((char*)Buffer,(char*)szFunction);  
		lpszStrings[0] = szName;  
		lpszStrings[1] = Buffer;  

		ReportEvent(hEventSource,        // event log handle  
			EVENTLOG_ERROR_TYPE, // event type  
			0,                   // event category  
			0,           // event identifier  
			NULL,                // no security identifier  
			2,                   // size of lpszStrings array  
			0,                   // no binary data  
			lpszStrings,         // array of strings  
			NULL);               // no binary data      
		DeregisterEventSource(hEventSource);  
	}  
}  
VOID ReportSvcStatus( DWORD dwCurrentState,  
	DWORD dwWin32ExitCode,  
	DWORD dwWaitHint)  
{  
	static DWORD dwCheckPoint = 1;  

	// Fill in the SERVICE_STATUS structure.  

	gSvcStatus.dwCurrentState = dwCurrentState;  
	gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;  
	gSvcStatus.dwWaitHint = dwWaitHint;  

	if (dwCurrentState == SERVICE_START_PENDING)  
		gSvcStatus.dwControlsAccepted = 0;  
	else gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;  

	if ( (dwCurrentState == SERVICE_RUNNING) ||  
		(dwCurrentState == SERVICE_STOPPED) )  
		gSvcStatus.dwCheckPoint = 0;  
	else gSvcStatus.dwCheckPoint = dwCheckPoint++;  

	// Report the status of the service to the SCM.  
	SetServiceStatus( gSvcStatusHandle, &gSvcStatus );  
}  
VOID WINAPI SvcCtrlHandler( DWORD dwCtrl )  
{  
	// Handle the requested control code.   
	switch(dwCtrl)   
	{    
	case SERVICE_CONTROL_STOP:   
		ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);  
		// Signal the service to stop.  
		SetEvent(ghSvcStopEvent);  
		return;  

	case SERVICE_CONTROL_INTERROGATE:   
		// Fall through to send current status.  
		break;   

	default:   
		break;  
	}   
	ReportSvcStatus(gSvcStatus.dwCurrentState, NO_ERROR, 0);  
}  

VOID ServerProgram(DWORD, LPTSTR *)
{
	try
	{
		std::cout << "server program!!!" << std::endl;

	}
	catch (std::exception& e)
	{
		std::cerr << "exception: " << e.what() << "\n";
	}
}
#endif


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值