c++写windows服务程序
2016-05-01 15:47
302 查看
#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); }
相关文章推荐
- 如何重装TCP/IP协议
- Windows 8 官方高清壁纸欣赏与下载
- Seafile Server本地权限提升漏洞(CVE-2014-5443)
- 从USB安装Ubuntu Server 10.04.3 图文详解
- 谁是桌面王者?Win PK Linux三大镇山之宝
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- Windows Clang开发环境备忘
- 从Windows系统下访问Linux分区相关软件
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- Windows下搭建本地SVN服务器
- 使用Windows原生命令一键清空剪贴板
- 利用开源软件打造自己的全功能远程工具
- 使用zabbix监控Nginx活动状态--Part1
- 高效访问Internet-启用ISA Server的缓存
- Windows 8虚拟机不能全屏的解决方法
- Visual Studio 2012 示例代码浏览器 - 数以千计的开发示例近在手边,唾手可得
- Visual Studio 2012 示例代码浏览器 - 数以千计的开发示例近在手边,唾手可得
- 微软镜像下载