Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > Win32服务控制类
【标  题】:Win32服务控制类
【关键字】:Win32
【来  源】:http://www.cppblog.com/lovelypig/archive/2006/04/01/4871.html

Win32服务控制类

Your Ad Here

/**
W32Server.h???
http://www.cppblog.com/lovelypig? 橙子
*/
#ifndef? _CW32SERVER_H
#define? _CW32SERVER_H
#include <windows.h>
#include <stdio.h>

?

class CW32Server
{
?TCHAR??m_szServerName[MAX_PATH];
?SC_HANDLE?? m_hService;
?SC_HANDLE?? m_hScm;
?SERVICE_STATUS ServiceStatus;
?BYTE??m_cfgBuf[4096];
public:

?CW32Server(void);
?CW32Server(const TCHAR *strServerName);?
?BOOL??? Open(const TCHAR *strServerName);??

?/**? 返回状态:
??SERVICE_CONTINUE_PENDING The service continue is pending.
??SERVICE_PAUSE_PENDING The service pause is pending.
??SERVICE_PAUSED The service is paused.
??SERVICE_RUNNING The service is running.
??SERVICE_START_PENDING The service is starting.
??SERVICE_STOP_PENDING The service is stopping.
??SERVICE_STOPPED The service is not running.
?*/
?DWORD?GetState();?????
?BOOL??? SetState(DWORD state);
?BOOL??? Start();
?BOOL??? Stop();

?/**
??SERVICE_AUTO_START??A service started automatically by the service control manager during system startup.
??SERVICE_BOOT_START??A device driver started by the system loader. This value is valid only for driver services.
??SERVICE_DEMAND_START?A service started by the service control manager when a process calls the StartService function.
??SERVICE_DISABLED??A service that cannot be started. Attempts to start the service result in the error code ERROR_SERVICE_DISABLED.
??SERVICE_SYSTEM_START
?*/
?BOOL??? GetConfig();???//如果返回真,配置状态填充在 m_config? 中
?SC_HANDLE??? GetHandle();??//需要禁用、启动服务,使用 ChangeServiceConfig(GetHandle(),...)?? 查看MSDN
?~CW32Server(void);

?QUERY_SERVICE_CONFIG *m_config;
};


#endif


/**
W32Server.cpp
http://www.cppblog.com/lovelypig? 橙子
*/
#include ".\w32server.h"
#include <assert.h>

CW32Server::CW32Server(void)
{
?memset((char*)&m_szServerName,0,sizeof(TCHAR)*MAX_PATH);
?memset((char*)&m_config,0,sizeof(m_config));
?m_hService = 0;
?m_hScm?? = 0;
?m_config = (QUERY_SERVICE_CONFIG*)m_cfgBuf;
}

CW32Server::CW32Server(const TCHAR *strServerName)
{?
?assert(strServerName);

?CW32Server();
?_tcscpy(m_szServerName,strServerName);?

?m_hScm=OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
?if(!m_hScm)
?{
??return ;
?}
?m_hService=OpenService(m_hScm,strServerName,SERVICE_ALL_ACCESS);
?if(!m_hService)
?{
??CloseServiceHandle(m_hScm);
??m_hScm = NULL;
??? }
}
CW32Server::~CW32Server(void)
{
?if( m_hScm )
?{
??CloseServiceHandle(m_hScm);
??m_hScm = NULL;
?}
?if( m_hService )
?{
??CloseServiceHandle(m_hService);
??m_hService = NULL;
?}
}

BOOL?? CW32Server:: Open(const TCHAR *strServerName)
{
?assert(strServerName);

?if( m_hScm )
?{
??CloseServiceHandle(m_hScm);
??m_hScm = NULL;
?}
?if( m_hService )
?{
??CloseServiceHandle(m_hService);
??m_hService = NULL;
?}

?_tcscpy(m_szServerName,strServerName);?
?m_hScm=OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
?if(!m_hScm)
?{
??return FALSE;
?}
?m_hService=OpenService(m_hScm,strServerName,SERVICE_ALL_ACCESS);
?if(!m_hService)
?{
??CloseServiceHandle(m_hScm);
??m_hScm = NULL;
??return FALSE;
?}
?return TRUE;
}

DWORD?CW32Server::GetState()
{?
?assert(m_hService);

??? if(QueryServiceStatus(m_hService,&ServiceStatus))
?{
??return ServiceStatus.dwCurrentState;
?}
?else
?{
??return 0xffffffff;
?}
}

BOOL??? CW32Server::SetState(DWORD state)
{
?assert(m_hService);

?return ControlService(m_hService,state,&ServiceStatus);
}

BOOL??? CW32Server::Start()
{
?assert(m_hService);

?return StartService(m_hService,0,NULL);

}

BOOL??? CW32Server::Stop()
{
?assert(m_hService);

?return ControlService(m_hService,SERVICE_CONTROL_STOP,&ServiceStatus);
}

BOOL??? CW32Server::GetConfig()
{
?assert(m_hService);
?
?DWORD cbBufSize = 4096;
?DWORD pcbBytesNeeded = 4096;
?return QueryServiceConfig(m_hService,m_config,cbBufSize, &pcbBytesNeeded);
}

SC_HANDLE? CW32Server::GetHandle()
{
?assert(m_hService);
?return m_hService;
}

关于DOF:【上一篇】
网卡类:【下一篇】
【相关文章】
  • Win32的时间类型
  • 遇到Trojan-PSW.Win32.Agent.i(第2版)
  • [2006-03-23]手工查杀QQ尾巴yuuikkj.exe(Kaspersky报为-Trojan-Proxy.Win32.Agent.iu,瑞星报为Trojan.QQ....
  • README.WIN32:Snort在win32环境下的安装
  • 命令行参数解析器 -- win32版getopt
  • 利用X-win32 SSH远程登录Unix系统访问图形界面
  • [翻译]Win32中安全的子类化
  • [翻译]Win32中安全的子类化 (2)
  • [翻译]Win32中安全的子类化 (1)
  • 使用PostThreadMessage在Win32线程间传递消息
  • 【随机文章】
  • 解密深圳IT人士的当前薪情
  • Berkeley_DB: 我的公益站点 www.berkeleydb.net 开张了。
  • SQL2000卸载了后重新安装时不能安装
  • [教学] HLTV
  • 揭开ASP神秘面纱(4)
  • Windows 2000 虚拟专用网络
  • 在Ubuntu中设置samba共享可读写文件夹
  • 不能再次装载程序
  • 關於spamassassin處理中文及中文特殊字符的問題
  • AS400存储空间管理经验谈
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.