Windows XP下Service的编程入门[1]

来源:互联网 发布:易酷cms 编辑:程序博客网 时间:2024/06/11 18:04

Windows XP下Service的编程入门[1]

// _dse.cpp : Defines the entry point for the console application.
// 作者: 秋镇菜
// E-mail: cyin8@msn.com

#include "stdafx.h"
#include "windows.h"

int main(int argc, char* argv[])
{
 LPCTSTR lpServiceName = "Themes"; // 本例为关闭 Themes 服务

 SC_HANDLE scman = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
/* =============================================================================
SC_HANDLE OpenSCManager(LPCTSTR lpszMachineName,
                        LPCTSTR lpszDatabaseName,
                        DWORD fdwDesiredAccess);
== Open SCManager 函数打开指定计算机上的service control manager database。
== 其中参数lpszMachineName指定计算机名,若为空则指定为本机。
== 参数lpszDatabaseName指定要打开的service control manager database,默认为空。
== 参数fdwDesiredAccess指定操作的权限,可以为下面取值之一
SC_MANAGER_ALL_ACCESS        //所有权限
SC_MANAGER_CONNECT           //允许连接service control manager
SC_MANAGER_CREATE_SERVICE    //允许创建服务对象并把它加入service control manager database
SC_MANAGER_ENUMERATE_SERVICE //允许枚举service control manager database中的服务
SC_MANAGER_LOCK              //允许锁住service control manager database
SC_MANAGER_QUERY_LOCK_STATUS //允许获取servicecontrolmanagerdatabase的封锁信息
== 函数返回值:函数执行成功则返回一个指向service control manager database的句柄,失败则返回NULL。
============================================================================== */
 if (scman)
 {
  SC_HANDLE sh = ::OpenService(scman, lpServiceName, SERVICE_STOP);
/* ========================================================================
SC_HANDLE OpenService(SC_HANDLE schSCManager,
                      LPCTSTR lpszServiceName,
                      DWORD fdwDesiredAccess);
== OpenService函数打开指定的Service。
== 其中参数schSCManager是指向service control manager database的句柄,
由OpenSCManager函数返回。
== 参数lpszServiceName要打开的服务的名字,注意大小写。
== 参数fdwDesiredAccess指定操作的权限,可以为下面取值之一
SERVICE_ALL_ACCESS            //所有权限
SERVICE_CHANGE_CONFIG         //允许更改服务的配置
SERVICE_ENUMERATE_DEPENDENTS  //允许获取依赖于该服务的其他服务
SERVICE_INTERROGATE            //允许立即获取服务状态
SERVICE_PAUSE_CONTINUE        //允许暂停和唤醒服务
SERVICE_QUERY_CONFIG           //允许获取服务配置
SERVICE_QUERY_STATU            //允许通过访问service control manager获取服务状态
SERVICE_START                  //允许启动服务
SERVICE_STOP                   //允许停止服务
SERVICE_USER_DEFINE_CONTROL    //允许用户指定特殊的服务控制码
== 函数返回值:函数执行成功则返回指向某项服务的句柄,失败则返回NULL。
========================================================================== */
  if (sh)
  {
   BOOL ctrl;
   SERVICE_STATUS ServiceStatus;
   ctrl = ::ControlService(sh, SERVICE_CONTROL_STOP, &ServiceStatus);
/* ==========================================================================
BOOL ControlService(SC_HANDLE hService,
     DWORD dwControl,
     LPSERVICE_STATUS lpServiceStatus);
== ControlService函数向Win32service发送控制码。
== 其中参数hService是指向某项服务的句柄,由OpenService函数返回。
== 参数dwControl为控制码,常用的有
    SERVICE_CONTROL_STOP     //停止服务
    SERVICE_CONTROL_PAUSE    //暂停服务
    SERVICE_CONTROL_CONTINUE    //唤醒暂停的服务
    SERVICE_CONTROL_INTERROGATE//刷新某服务的状态
== 参数lpServiceStatus指向SERVICE_STATUS结构,用于存放该服务最新的状态信息。
== 函数返回值:函数执行成功则返回True,失败则返回False。
============================================================================= */
   DWORD dwControl;
   if (ctrl)
   {
    printf("success to stop the service /"%s/"/n",lpServiceName);
   }
   else
   {
    dwControl = ::GetLastError();
    switch(dwControl){
     case ERROR_ACCESS_DENIED:
      printf("The specified handle was not opened with the necessary access./n");
      break;
     case ERROR_SERVICE_NOT_ACTIVE:
      printf("The service has not been started./n");
      break;
     case ERROR_DEPENDENT_SERVICES_RUNNING:
      printf("The service cannot be stopped because other running services are dependent on it./n");
      break;
     case ERROR_INVALID_SERVICE_CONTROL:
      printf("The requested control code is not valid, or it is unacceptable to the service./n");
      break;
     case ERROR_SERVICE_CANNOT_ACCEPT_CTRL:
      printf("The requested control code cannot be sent to the service because the state of the service is SERVICE_STOPPED, SERVICE_START_PENDING, or SERVICE_STOP_PENDING./n");
      break;
     case ERROR_SERVICE_REQUEST_TIMEOUT:
      printf("The service did not respond to the start request in a timely fashion./n");
      break;
    }
   }
  }
  CloseServiceHandle(sh); // 关闭该Service
 }
 CloseServiceHandle(scman); // 关闭该Service
 printf("Hello World!/n");
 return 0;
}