C语言写windows服务例子

转载地址:http://hi.baidu.com/hegongheng/blog/item/d4a321b55efd6cc636d3cadd.html

//这个windows服务例子是监控内存情况,并写到C:\\MyServices\\memstatus.txt的记事本上,创建MemoryStatus.cpp,代码如下:

#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);
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值