c++开发windows服务

来源:互联网 发布:软件项目立项报告 编辑:程序博客网 时间:2024/06/05 17:25

微软的项目模板能很好的支持WINDOWS服务开发,如C#的windows服务项目

C++开发的话,也不难,使用一些API就能实现,下附代码

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include "stdafx.h"  
  2. #include "Windows.h"  
  3.   
  4. #define SERVICE_NAME "srv_demo"  
  5.   
  6. SERVICE_STATUS ServiceStatus;  
  7. SERVICE_STATUS_HANDLE hServiceStatusHandle;  
  8. void WINAPI service_main(int argc, char** argv);   
  9. void WINAPI ServiceHandler(DWORD fdwControl);  
  10.   
  11. TCHAR szSvcName[80];  
  12. SC_HANDLE schSCManager;  
  13. SC_HANDLE schService;  
  14. int uaquit;  
  15. FILE* log;  
  16.   
  17. DWORD WINAPI srv_core_thread(LPVOID para)  
  18. {     
  19. int i = 0;  
  20. for(;;)  
  21. {  
  22.     if(uaquit)  
  23.     {  
  24.         break;   
  25.     }  
  26.     fprintf(log,"srv_core_thread run time count:%d\n",i++);  
  27.     Sleep(5000);   
  28. }      
  29.    return NULL;   
  30. }     
  31.   
  32.   
  33. void WINAPI ServiceHandler(DWORD fdwControl)  
  34. {  
  35. switch(fdwControl)   
  36. {  
  37.     case SERVICE_CONTROL_STOP:  
  38.     case SERVICE_CONTROL_SHUTDOWN:  
  39.     ServiceStatus.dwWin32ExitCode = 0;   
  40.     ServiceStatus.dwCurrentState  = SERVICE_STOPPED;   
  41.     ServiceStatus.dwCheckPoint    = 0;   
  42.     ServiceStatus.dwWaitHint      = 0;  
  43.     uaquit= 1;  
  44.     //add you quit code here  
  45.     if(log != NULL)  
  46.     fclose(log);  
  47.     break;   
  48.     default:  
  49.         return;   
  50.     };  
  51.     if (!SetServiceStatus(hServiceStatusHandle,  &ServiceStatus))   
  52.     {   
  53.         DWORD nError = GetLastError();  
  54.     }   
  55. }  
  56.   
  57.     
  58. void WINAPI service_main(int argc, char** argv)   
  59. {         
  60.     ServiceStatus.dwServiceType        = SERVICE_WIN32;   
  61.     ServiceStatus.dwCurrentState       = SERVICE_START_PENDING;   
  62.     ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;   
  63.     ServiceStatus.dwWin32ExitCode      = 0;   
  64.     ServiceStatus.dwServiceSpecificExitCode = 0;   
  65.     ServiceStatus.dwCheckPoint         = 0;   
  66.     ServiceStatus.dwWaitHint           = 0;    
  67.     hServiceStatusHandle = RegisterServiceCtrlHandler(_T(SERVICE_NAME), ServiceHandler);   
  68.     if (hServiceStatusHandle==0)   
  69.     {  
  70.         DWORD nError = GetLastError();  
  71.     }    
  72.     //add your init code here  
  73.     log = fopen("c:\\test.txt","w");  
  74.     //add your service thread here  
  75.     HANDLE task_handle = CreateThread(NULL,NULL,srv_core_thread,NULL,NULL,NULL);  
  76.     if(task_handle == NULL)  
  77.     {  
  78.         fprintf(log,"create srv_core_thread failed\n");  
  79.     }  
  80.       
  81.     // Initialization complete - report running status   
  82.     ServiceStatus.dwCurrentState       = SERVICE_RUNNING;   
  83.     ServiceStatus.dwCheckPoint         = 0;   
  84.     ServiceStatus.dwWaitHint           = 9000;    
  85.     if(!SetServiceStatus(hServiceStatusHandle, &ServiceStatus))   
  86.     {   
  87.         DWORD nError = GetLastError();  
  88.     }   
  89.    
  90. }   
  91. //do not change main function  
  92. int main (int argc, const char *argv[])  
  93. {  
  94.     SERVICE_TABLE_ENTRY ServiceTable[2];  
  95.       
  96.     ServiceTable[0].lpServiceName = _T(SERVICE_NAME);  
  97.     ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)service_main;  
  98.       
  99.     ServiceTable[1].lpServiceName = NULL;  
  100.     ServiceTable[1].lpServiceProc = NULL;  
  101.     // 启动服务的控制分派机线程  
  102.     StartServiceCtrlDispatcher(ServiceTable);   
  103.     return 0;  
  104. }  


以上是一个别人写的简单服务,需要注意的是,win32控制台的应用程序,作为服务不能直接打开,因此也不能调试。

开启服务步骤如下:

1.编译程序;
2.成功后找到win32srvdemo.exe(注意:不是直接运行此程序),在debug或release目录中,
复制下路径,如..win32srvdemo\debug\win32srvdemo.exe;
3.开始->运行->cmd->回车 输入sc create test binPath= 上面的路径
4.开始->运行->services.msc->回车 找到test并启动->OK了
5.成功后应该可以看到d:\test.txt文件
0 0
原创粉丝点击