标题
    
    
        [windows/vc6/sdk]启动和停止服务 [含注册/删除一个服务项的代码]
    
 
    
 	 
       
    
    clq
    
    
    
		
        浏览(0) + 
        2008-07-17 09:10:37 发表
        
编辑
         
        
        
        
        关键字: 
        
 
        
        
        
        
                
        启动和停止服务 
from  http://blog.vckbase.com/star/archive/2005/04/30/5085.html
启动和停止服务
包含头文件:#include 
以下以web服务为例:
#include 
void CStartServiceDlg::OnBnClickedButton1()
{
       // 打开服务管理对象
    SC_HANDLE hSC = ::OpenSCManager( NULL, 
                        NULL, GENERIC_EXECUTE);
    if( hSC == NULL)
    {
        TRACE( "open SCManager error");
        return;
    }
    // 打开www服务。
    SC_HANDLE hSvc = ::OpenService( hSC, "W3SVC",
        SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
    if( hSvc == NULL)
    {
        TRACE( "Open www erron。");
        ::CloseServiceHandle( hSC);
        return;
    }
    // 获得服务的状态
    SERVICE_STATUS status;
    if( ::QueryServiceStatus( hSvc, &status) == FALSE)
    {
        TRACE( "Get Service state error。");
        ::CloseServiceHandle( hSvc);
        ::CloseServiceHandle( hSC);
        return;
    }
    //如果处于停止状态则启动服务,否则停止服务。
    if( status.dwCurrentState == SERVICE_RUNNING)
    {
        // 停止服务
        if( ::ControlService( hSvc, 
          SERVICE_CONTROL_STOP, &status) == FALSE)
        {
            TRACE( "stop service error。");
            ::CloseServiceHandle( hSvc);
            ::CloseServiceHandle( hSC);
            return;
        }
        // 等待服务停止
        while( ::QueryServiceStatus( hSvc, &status) == TRUE)
        {
            ::Sleep( status.dwWaitHint);
            if( status.dwCurrentState == SERVICE_STOPPED)
            {
                AfxMessageBox( "stop success。");
                ::CloseServiceHandle( hSvc);
                ::CloseServiceHandle( hSC);
                return;
            }
        }
    }
    else if( status.dwCurrentState == SERVICE_STOPPED)
    {
        // 启动服务
        if( ::StartService( hSvc, NULL, NULL) == FALSE)
        {
            TRACE( "start service error。");
            ::CloseServiceHandle( hSvc);
            ::CloseServiceHandle( hSC);
            return;
        }
        // 等待服务启动
        while( ::QueryServiceStatus( hSvc, &status) == TRUE)
        {
            ::Sleep( status.dwWaitHint);
            if( status.dwCurrentState == SERVICE_RUNNING)
            {
                AfxMessageBox( "start success。");
                ::CloseServiceHandle( hSvc);
                ::CloseServiceHandle( hSC);
                return;
            }
      }
    }
    TRACE( "start error。");
    ::CloseServiceHandle( hSvc);
    ::CloseServiceHandle( hSC);
    return;
}
        
        
        
        		
		        
                
                
     
    
 
	
	
     
    
       
    
    clq
    
    
    
    
    
    		    
    
          
              
    	下面是我自己分解的一些实用函数:
//#include 
//查询一个服务的状态
std::string GetScStateString(const std::string sername)
{
	char * sta[] = {"关闭", "启动", "暂停", "正在转换状态"};
	int i = GetScState(sername);
	if ((i >= 0)&&(i <= 3))
	{
		return sta[i] ;
	}
	return "查询失败";
}
//查询一个服务的状态// -1 -- 失败, 0 -- 关闭, 1 -- 启动, 2 -- 暂停, 3 -- 正在转换状态
int GetScState(const std::string sername)
{
	//打开服务管理对象
	SC_HANDLE hSC = ::OpenSCManager( NULL, 
	NULL, GENERIC_EXECUTE);
	if( hSC == NULL)
	{
		return -1;
	}
    //打开www服务
    //SC_HANDLE hSvc = ::OpenService( hSC, "W3SVC", SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
	SC_HANDLE hSvc = ::OpenService( hSC, sername.c_str(), SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
    if( hSvc == NULL)
    {
        ::CloseServiceHandle(hSC);
        return -1;
    }
    // 获得服务的状态
    SERVICE_STATUS status;
    if( ::QueryServiceStatus( hSvc, &status) == FALSE)
    {
        //TRACE( "Get Service state error。");
        ::CloseServiceHandle( hSvc);
        ::CloseServiceHandle( hSC);
        return -1;
    }
	int r = -1;
    if( status.dwCurrentState == SERVICE_RUNNING)//运行
    {
		r = 1;
	}
	else
    if( status.dwCurrentState == SERVICE_STOPPED)//停止
    {
		r = 0;
	}
	else
    if( status.dwCurrentState == SERVICE_PAUSED)//暂停
    {
		r = 2;
	}
	else
	{
		r = 3;//等待转换为其他状态
	}
    ::CloseServiceHandle( hSvc);
    ::CloseServiceHandle( hSC);
	return r;
}
//停止一个服务
bool StopSc(const std::string sername)
{
	//打开服务管理对象
	SC_HANDLE hSC = ::OpenSCManager( NULL, NULL, GENERIC_EXECUTE);
	if( hSC == NULL)
	{
		return false;
	}
    //打开www服务
    //SC_HANDLE hSvc = ::OpenService( hSC, "W3SVC", SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
	SC_HANDLE hSvc = ::OpenService( hSC, sername.c_str(), SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
    if( hSvc == NULL)
    {
        ::CloseServiceHandle(hSC);
        return false;
    }
    // 获得服务的状态
    SERVICE_STATUS status;
    if( ::QueryServiceStatus( hSvc, &status) == FALSE)
    {
        //TRACE( "Get Service state error。");
        ::CloseServiceHandle( hSvc);
        ::CloseServiceHandle( hSC);
        return false;
    }
    
    //停止服务
    if( ::ControlService(hSvc, SERVICE_CONTROL_STOP, &status) == FALSE)
    {
        //TRACE( "stop service error。");
        ::CloseServiceHandle( hSvc);
        ::CloseServiceHandle( hSC);
        return false;
    }
    //等待服务停止
    while( ::QueryServiceStatus( hSvc, &status) == TRUE)
    {
        //::Sleep( status.dwWaitHint);
		::Sleep( 200 );
        if( status.dwCurrentState == SERVICE_STOPPED)
        {
            //AfxMessageBox( "stop success。");
            ::CloseServiceHandle( hSvc);
            ::CloseServiceHandle( hSC);
            return true;
        }
    }
    //TRACE( "start error。");
    ::CloseServiceHandle( hSvc);
    ::CloseServiceHandle( hSC);
    return false;
}
//启动一个服务
bool StartSc(const std::string sername)
{
	//打开服务管理对象
	SC_HANDLE hSC = ::OpenSCManager( NULL, 
	NULL, GENERIC_EXECUTE);
	if( hSC == NULL)
	{
		return false;
	}
    //打开www服务
    //SC_HANDLE hSvc = ::OpenService( hSC, "W3SVC", SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
	SC_HANDLE hSvc = ::OpenService( hSC, sername.c_str(), SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
    if( hSvc == NULL)
    {
        ::CloseServiceHandle(hSC);
        return false;
    }
    // 获得服务的状态
    SERVICE_STATUS status;
    if( ::QueryServiceStatus( hSvc, &status) == FALSE)
    {
        //TRACE( "Get Service state error。");
        ::CloseServiceHandle( hSvc);
        ::CloseServiceHandle( hSC);
        return false;
    }
    // 启动服务
    if( ::StartService( hSvc, NULL, NULL) == FALSE)
    {
        //TRACE( "start service error。");
        ::CloseServiceHandle( hSvc);
        ::CloseServiceHandle( hSC);
        return false;
    }
    // 等待服务启动
    while( ::QueryServiceStatus( hSvc, &status) == TRUE)
    {
        //::Sleep( status.dwWaitHint);
		::Sleep( 200 );
        if( status.dwCurrentState == SERVICE_RUNNING)
        {
            //AfxMessageBox( "start success。");
            ::CloseServiceHandle( hSvc);
            ::CloseServiceHandle( hSC);
            return true;
        }
    }
    ::CloseServiceHandle( hSvc);
    ::CloseServiceHandle( hSC);
    return false;
}
//判断服务是否注册
bool ScIsInstalled(const std::string sername)
{
    bool bResult = false;
	//打开服务控制管理器
    SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (hSCM != NULL)
    {
		//打开服务
        SC_HANDLE hService = ::OpenService(hSCM, sername.c_str(), SERVICE_QUERY_CONFIG);
        if (hService != NULL)
        {
            bResult = true;
            ::CloseServiceHandle(hService);
        }
        ::CloseServiceHandle(hSCM);
    }
    return bResult;
}
//注册一个服务
bool ScInstall(const std::string sername, const std::string path)
{
    if (ScIsInstalled(sername) == true)
        return true;
	//打开服务控制管理器
    SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (hSCM == NULL)
    {
        //MessageBox(NULL, _T("Couldn't open service manager"), szServiceName, MB_OK);
        return false;
    }
    // Get the executable file path
    //TCHAR szFilePath[MAX_PATH];
    //::GetModuleFileName(NULL, szFilePath, MAX_PATH);
	//创建服务
    SC_HANDLE hService = ::CreateService(
        hSCM, sername.c_str(), sername.c_str(),
        SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
        //SERVICE_DEMAND_START, //手工启动
		SERVICE_AUTO_START,//自动启动
		SERVICE_ERROR_NORMAL,
        //szFilePath, NULL, NULL, _T(""), NULL, NULL);
		path.c_str(), NULL, NULL, (""), NULL, NULL);
    if (hService == NULL)
    {
        ::CloseServiceHandle(hSCM);
        //MessageBox(NULL, _T("Couldn't create service"), szServiceName, MB_OK);
        return false;
    }
    ::CloseServiceHandle(hService);
    ::CloseServiceHandle(hSCM);
    return true;
}
//删除一个服务
bool ScUninstall(const std::string sername)
{
    if (ScIsInstalled(sername) == false)
        return true;
    SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if (hSCM == NULL)
    {
        //MessageBox(NULL, _T("Couldn't open service manager"), szServiceName, MB_OK);
        return false;
    }
    SC_HANDLE hService = ::OpenService(hSCM, sername.c_str(), SERVICE_STOP | DELETE);
    if (hService == NULL)
    {
        ::CloseServiceHandle(hSCM);
        //MessageBox(NULL, _T("Couldn't open service"), szServiceName, MB_OK);
        return false;
    }
    SERVICE_STATUS status;
    ::ControlService(hService, SERVICE_CONTROL_STOP, &status);
	//删除服务
    BOOL bDelete = ::DeleteService(hService);
    ::CloseServiceHandle(hService);
    ::CloseServiceHandle(hSCM);
    if (bDelete)
        return true;
    //LogEvent(_T("Service could not be deleted"));
    return FALSE;
}
    
    
     
 
	 
	
    NEWBT官方QQ群1: 276678893
    可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
    但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
    验证问题说明申请入群原因即可.