| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 341 人关注过本帖
标题:C程序如何做服务程序 几步搞定
只看楼主 加入收藏
bowenpro
Rank: 2
等 级:论坛游民
帖 子:7
专家分:16
注 册:2011-2-23
收藏
 问题点数:0 回复次数:0 
C程序如何做服务程序 几步搞定
程序代码:
-
#include <windows.h>
#include <stdio.h>

#define SLEEP_TIME 5000
#define LOGFILE "C:\\MyServices\\memstatus.txt"

////////////////////////////////////////////////////////////
// Declare several global variables to share
// their values across multiple functions of your program.
////////////////////////////////////////////////////////////
SERVICE_STATUS          ServiceStatus;
SERVICE_STATUS_HANDLE   hStatus;

////////////////////////////////////////////////////////////
// Make the forward definitions of functions prototypes.
//
////////////////////////////////////////////////////////////
void ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request);
int InitService();

int WriteToLog(char* str)
{
   FILE* log;
   log = fopen(LOGFILE, "a+");
   if (log == NULL){
    OutputDebugString("Log file open failed.");
      return -1;
   }
   fprintf(log, "%s\n", str);
   fclose(log);
   return 0;
}

// Service initialization
int InitService()
{
OutputDebugString("Monitoring started.");
int result;
result = WriteToLog("Monitoring started.");
return(result);
}

// Control Handler
void ControlHandler(DWORD request)
{
   switch(request)
   {
      case SERVICE_CONTROL_STOP:
   OutputDebugString("Monitoring stopped.");
         WriteToLog("Monitoring stopped.");

         ServiceStatus.dwWin32ExitCode = 0;
         ServiceStatus.dwCurrentState = SERVICE_STOPPED;
         SetServiceStatus (hStatus, &ServiceStatus);
         return; 

      case SERVICE_CONTROL_SHUTDOWN:
   OutputDebugString("Monitoring stopped.");
         WriteToLog("Monitoring stopped.");

         ServiceStatus.dwWin32ExitCode = 0;
         ServiceStatus.dwCurrentState = SERVICE_STOPPED;
         SetServiceStatus (hStatus, &ServiceStatus);
         return;
       
      default:
         break;
    } 

    // Report current status
    SetServiceStatus (hStatus, &ServiceStatus);

    return;
}

void ServiceMain(int argc, char** argv)
{
   int error; 

   ServiceStatus.dwServiceType =
      SERVICE_WIN32;
   ServiceStatus.dwCurrentState =
      SERVICE_START_PENDING;
   ServiceStatus.dwControlsAccepted   =
      SERVICE_ACCEPT_STOP |
      SERVICE_ACCEPT_SHUTDOWN;
   ServiceStatus.dwWin32ExitCode = 0;
   ServiceStatus.dwServiceSpecificExitCode = 0;
   ServiceStatus.dwCheckPoint = 0;
   ServiceStatus.dwWaitHint = 0; 

   hStatus = RegisterServiceCtrlHandler(
      "MemoryStatus",
      (LPHANDLER_FUNCTION)ControlHandler);
   if (hStatus == (SERVICE_STATUS_HANDLE)0)
   {
      // Registering Control Handler failed
      return;
   } 

   // Initialize Service
   error = InitService();
   if (error)
   {
      // Initialization failed
      ServiceStatus.dwCurrentState =
         SERVICE_STOPPED;
      ServiceStatus.dwWin32ExitCode = -1;
      SetServiceStatus(hStatus, &ServiceStatus);
      return;
   }
   // We report the running status to SCM.
   ServiceStatus.dwCurrentState =
      SERVICE_RUNNING;
   SetServiceStatus (hStatus, &ServiceStatus);

   MEMORYSTATUS memory;
   // The worker loop of a service
   while (ServiceStatus.dwCurrentState ==
          SERVICE_RUNNING)
   {
      char buffer[16];
      GlobalMemoryStatus(&memory);
      sprintf(buffer, "%d", memory.dwAvailPhys);

      OutputDebugString(buffer);
      int result = WriteToLog(buffer);
      if (result)
      {
         ServiceStatus.dwCurrentState =
            SERVICE_STOPPED;
         ServiceStatus.dwWin32ExitCode      = -1;
         SetServiceStatus(hStatus,
                          &ServiceStatus);
         return;
      }
      Sleep(SLEEP_TIME);
   }
   return;
}

void main(int argc, char* argv[])
{
   SERVICE_TABLE_ENTRY ServiceTable[2];
   ServiceTable[0].lpServiceName = "MemoryStatus";
   ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;

   ServiceTable[1].lpServiceName = NULL;
   ServiceTable[1].lpServiceProc = NULL;
   // Start the control dispatcher thread for our service
   StartServiceCtrlDispatcher(ServiceTable);
}

搜索更多相关主题的帖子: 如何 several values their 
2011-02-23 15:44
快速回复:C程序如何做服务程序 几步搞定
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023803 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved