编写简单系统服务

一段简单的服务代码,在vc6,xpsp2上跑过去了。想了解如何写服务的朋友可以拿来看看,不明白可以查msdn。

it's al what i do today : code a very simple services on windows xp sp2

that's the code below:

///filename :svohost.c

#include  <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define SLEEP_TIME 10000

SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
char szFullPath[MAX_PATH];


void ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request);
int InitService();

int do_ur_things()
{
 //MessageBox(NULL,"service is running","",0);
 //do any thing u like...
return 0;
}


//
//
//
//

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(
      "svohost",
      (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);
 
  
   // The worker loop of a service
   while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
   {

      do_ur_things();

      Sleep(SLEEP_TIME);
   }
   return;

}


//
//
//
int InitService()
{
 //do anything?
    return 0;
}

 

//
//
//
void ControlHandler(DWORD request)
{
   switch(request)
   {
      case SERVICE_CONTROL_STOP:
        
         ServiceStatus.dwWin32ExitCode = 0;
         ServiceStatus.dwCurrentState = SERVICE_STOPPED;
         SetServiceStatus (hStatus, &ServiceStatus);
         return;
 
      case SERVICE_CONTROL_SHUTDOWN:

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


//
// add the service and start it
//

VOID install_service(char* pPath, char* pName)

 SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE);
 if (schSCManager==0)
 {
  long nError = GetLastError();
  printf("OpenSCManager failed, error code = %d", nError);
  
 }
 else
 {
  SC_HANDLE schService = CreateService
  (
   schSCManager, /* SCManager database      */
   pName,   /* name of service         */
   pName,   /* service name to display */
   SERVICE_ALL_ACCESS,        /* desired access          */
   SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS , /* service type            */
   SERVICE_AUTO_START,      /* start type              */
   SERVICE_ERROR_NORMAL,      /* error control type      */
   pPath,   /* service's binary        */
   NULL,                      /* no load ordering group  */
   NULL,                      /* no tag identifier       */
   NULL,                      /* no dependencies         */
   NULL,                      /* LocalSystem account     */
   NULL
  );                     /* no password             */
  if (schService==0)
  {
   long nError =  GetLastError();
   printf( "Failed to create service %s, error code = %d", pName, nError);
   
  }
  else
  {
   StartService(schService,0,0);
   //printf("Service %s installed", pName);

   CloseServiceHandle(schService);
  }
  CloseServiceHandle(schSCManager);
 } 
}



//
//win32 application
//

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
 
 
  
 SERVICE_TABLE_ENTRY ServiceTable[]=
 {
  {"svohost",(LPSERVICE_MAIN_FUNCTION)ServiceMain},
  {NULL,NULL}
 };
 
 GetModuleFileName(NULL,szFullPath,MAX_PATH);
 

    StartServiceCtrlDispatcher(ServiceTable);
  install_service(szFullPath,"svohost");
 
 return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值