c++写windows服务程序
来源:互联网 发布:java 制作图片 编辑:程序博客网 时间:2024/05/22 00:40
#include "windows.h"SERVICE_STATUS gSvcStatus; //服务状态SERVICE_STATUS_HANDLE gSvcStatusHandle; //服务状态句柄HANDLE ghSvcStopEvent = NULL;//服务停止句柄#define SERVER_NAME TEXT("my_server") //服务名VOID WINAPI ServerMain( 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(Buffer,szFunction); lpszStrings[0] = szName; lpszStrings[1] = Buffer; ReportEvent(hEventSource, // event log handle EVENTLOG_ERROR_TYPE, // event type 0, // event category SVC_ERROR, // 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);}
0 0
- c#--写windows 服务
- c#写windows服务程序
- c#写windows服务程序
- c#写windows服务程序
- c#写windows服务程序
- c++写windows服务程序
- C#写Windows Service(windows服务程序)
- c写windows窗体程序
- 写Windows服务程序的五个步骤
- 用BCB写windows服务程序
- [转载]C# 写Windows服务程序
- Python写Windows Service服务程序
- 关于C# 写Windows服务程序
- C语言写windows服务例子
- C语言编写Windows服务程序。
- 用C语言编写Windows服务程序
- C编写Windows服务程序详细介绍
- 用 C 语言编写 Windows 服务程序
- Android学习笔记3:使用日志工具
- 《剑指offer》01 赋值运算符函数
- Android基础:动画三:属性动画
- sicily 1047(数学)
- ZOJ - 3875 Lunch Time (模拟)水
- c++写windows服务程序
- Android学习笔记4:探究活动1
- Codeforces Round 335 (Div 2) D. Lazy Student【构造 脑洞 最小生成树】
- HDOJ1203
- HBase的javaApi一个应用(创建删除表等)
- UVa 524 Prime Ring Problem
- 启动报错com/opensymphony/xwork2/spring/SpringObjectFactory.java:245:-1
- ZOJ - 3876 May Day Holiday (打表&模拟)
- CSS3学习笔记