文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Windows编程: 写一个系统服务

Windows编程: 写一个系统服务

时间:2010-10-24  来源:kgisme170

         有两种计算机思想。
         一个是unix,一切都是文本;一种是windows,一切都是2进制对象。
         所以对于unix来说,要增加一个系统服务是个很简单的事情。如ubuntu linux要增加一个系统服务的话:
1.在/etc/rcX.d下面增加一个SXXX脚本,指向/etc/rc.d下面的真的启动脚本
2.建立这个真的启动脚本,这个脚本要包括start,stop等选项
3.系统可以调用service start/stop [该服务的名字]来启动和关闭。
         但是,windows不是这样基于脚本的,如何增加系统服务? 如何有一个启动入口来指定,start/stop的时候执行什么功能呢? 那就全都要写到一个启动程序里面去,这个程序包括了:
1.main函数,因为这个模块应该是exe。
2.注册一个回调函数,扮演了/etc/rcX.d当中SXXX脚本的角色,增加一个系统能够服务的入口,名字必须是ServiceMain
3.完成Install/Uninstall的接口,相当于start/stop的脚本功能。


#include "stdafx.h"
#include<stdio.h>
#include <Windows.h>
#include <WinDef.h>
#include <Winsvc.h>
#include <time.h>

SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
BOOL bRunning=true;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
BOOL InstallService();
BOOL DeleteService();
int main(int argc, char* argv[]){
  if(argc>1) {
    if(strcmp(argv[1],"-i")==0) {
      if(InstallService())
        printf("\n\nService Installed Sucessfully\n");
      else
        printf("\n\nError Installing Service\n");
    }
    if(strcmp(argv[1],"-d")==0) {
      if(DeleteService())
        printf("\n\nService UnInstalled Sucessfully\n");
      else
        printf("\n\nError UnInstalling Service\n");
    }else{
      printf("\n\nUnknown Switch Usage\n\nFor Install\
           use Srv1 -i\n\nFor UnInstall use Srv1 -d\n");
     return 1;
    }
  }else{
    SERVICE_TABLE_ENTRY DispatchTable[]=
                {{"Service1",ServiceMain},{NULL,NULL}};
    StartServiceCtrlDispatcher(DispatchTable);
  }
  return 0;
}

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv){
  m_ServiceStatus.dwServiceType = SERVICE_WIN32;
  m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  m_ServiceStatus.dwWin32ExitCode = 0;
  m_ServiceStatus.dwServiceSpecificExitCode = 0;
  m_ServiceStatus.dwCheckPoint = 0;
  m_ServiceStatus.dwWaitHint = 0;

  m_ServiceStatusHandle = RegisterServiceCtrlHandler("Service1",ServiceCtrlHandler);
  if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
    return;
  m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
    return;
  bRunning=true;
  while(bRunning) {
    Sleep(3000);
    //这里是我们要做的事情....

  }
  return;
}

void WINAPI ServiceCtrlHandler(DWORD Opcode){
  switch(Opcode)
  {
    case SERVICE_CONTROL_PAUSE:
      m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
      break;
    case SERVICE_CONTROL_CONTINUE:
      m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
      break;
    case SERVICE_CONTROL_STOP:
      m_ServiceStatus.dwWin32ExitCode = 0;
      m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
      m_ServiceStatus.dwCheckPoint = 0;
      m_ServiceStatus.dwWaitHint = 0;

      SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
      bRunning=false;
      break;
    case SERVICE_CONTROL_INTERROGATE:
      break;
  }
  return;
}

BOOL InstallService(){
  char strDir[1024];
  SC_HANDLE schSCManager,schService;
  GetCurrentDirectory(1024,strDir);
  strncat_s(strDir,"\\Srv1.exe",10);
  schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  if (schSCManager == NULL)
    return false;
  LPCTSTR lpszBinaryPathName=strDir;
  schService = CreateService(schSCManager,"Service1",
        "The Display Name Needed", // service name to display
     SERVICE_ALL_ACCESS, // desired access
     SERVICE_WIN32_OWN_PROCESS, // service type
     SERVICE_DEMAND_START, // start type
     SERVICE_ERROR_NORMAL, // error control type
     lpszBinaryPathName, // service's binary
     NULL, // no load ordering group
     NULL, // no tag identifier
     NULL, // no dependencies
     NULL, // LocalSystem account
     NULL); // no password
  if (schService == NULL)
    return false;
  CloseServiceHandle(schService);
  return true;
}

BOOL DeleteService(){
  SC_HANDLE schSCManager,hService;
  schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
  if (schSCManager == NULL)
    return false;
  hService=OpenService(schSCManager,"Service1",SERVICE_ALL_ACCESS);
  if (hService == NULL)
    return false;
  if(DeleteService(hService)==0)
    return false;
  if(CloseServiceHandle(hService)==0)
    return false;
  return true;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载