谢谢

解决方案 »

  1.   

    1。判断Service的状态(运行,停止......),用ControlService();
    2。启动一个Service,使用StarService
      

  2.   

    BOOL StopService(SC_HANDLE schService)
    {
    SERVICE_STATUS ssStatus;
        DWORD dwOldCheckPoint; 
        DWORD dwStartTickCount;
        DWORD dwWaitTime; if (!ControlService( 
                schService,   // handle to service 
                SERVICE_CONTROL_STOP,   // control value to send 
                &ssStatus) )  // address of status info 
        {
             AfxMessageBox("停止服务 MSSQLSERVER 时出现错误!"); 
     return FALSE;
        }
    if (!QueryServiceStatus( 
                schService,   // handle to service 
                &ssStatus) )  // address of status information structure
        {
            AfxMessageBox("查询服务状态的时候出现错误!"); 
    return FALSE;
        }
     
        // Save the tick count and initial checkpoint.    dwStartTickCount = GetTickCount();
        dwOldCheckPoint = ssStatus.dwCheckPoint;    while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) 
        { 
            // Do not wait longer than the wait hint. A good interval is 
            // one tenth the wait hint, but no less than 1 second and no 
            // more than 10 seconds. 
     
            dwWaitTime = ssStatus.dwWaitHint / 10;        if( dwWaitTime < 1000 )
                dwWaitTime = 1000;
            else if ( dwWaitTime > 10000 )
                dwWaitTime = 10000;        Sleep( dwWaitTime );        // Check the status again. 
     
            if (!QueryServiceStatus( 
                    schService,   // handle to service 
                    &ssStatus) )  // address of structure
                break; 
     
            if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
            {
                // The service is making progress.            dwStartTickCount = GetTickCount();
                dwOldCheckPoint = ssStatus.dwCheckPoint;
            }
            else
            {
                if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
                {
                    // No progress made within the wait hint
                    break;
                }
            }
        }     if (ssStatus.dwCurrentState != SERVICE_STOPPED) 
        {
    AfxMessageBox("停止服务 MSSQLSERVER 时出现错误!");
            return FALSE;
        }
    return TRUE;
    }BOOL StartService(SC_HANDLE schService) 

        SERVICE_STATUS ssStatus; 
        DWORD dwOldCheckPoint; 
        DWORD dwStartTickCount;
        DWORD dwWaitTime;
        if (!StartService(
                schService,  // handle to service 
                0,           // number of arguments 
                NULL) )      // no arguments 
        {
            AfxMessageBox("启动服务 MSSQLSERVER 时出现错误!"); 
    return FALSE;
        } 
        // Check the status until the service is no longer start pending. 
     
        if (!QueryServiceStatus( 
                schService,   // handle to service 
                &ssStatus) )  // address of status information structure
        {
            AfxMessageBox("查询服务状态的时候出现错误!"); 
    return FALSE;
        }
     
        // Save the tick count and initial checkpoint.    dwStartTickCount = GetTickCount();
        dwOldCheckPoint = ssStatus.dwCheckPoint;    while (ssStatus.dwCurrentState == SERVICE_START_PENDING) 
        { 
            // Do not wait longer than the wait hint. A good interval is 
            // one tenth the wait hint, but no less than 1 second and no 
            // more than 10 seconds. 
     
            dwWaitTime = ssStatus.dwWaitHint / 10;        if( dwWaitTime < 1000 )
                dwWaitTime = 1000;
            else if ( dwWaitTime > 10000 )
                dwWaitTime = 10000;        Sleep( dwWaitTime );        // Check the status again. 
     
            if (!QueryServiceStatus( 
                    schService,   // handle to service 
                    &ssStatus) )  // address of structure
                break; 
     
            if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
            {
                // The service is making progress.            dwStartTickCount = GetTickCount();
                dwOldCheckPoint = ssStatus.dwCheckPoint;
            }
            else
            {
                if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
                {
                    // No progress made within the wait hint
                    break;
                }
            }
        }     if (ssStatus.dwCurrentState != SERVICE_RUNNING) 
        {
    AfxMessageBox("启动服务 MSSQLSERVER 时出现错误!"); 
    return FALSE;
        }
     
        return TRUE;
    } // CattachdbApp 初始化BOOL CattachdbApp::InitInstance()
    {
    CWinApp::InitInstance(); //打开服务管理器
    SC_HANDLE schSCManager = OpenSCManager( 
        NULL,                    // local machine 
        NULL,                    // ServicesActive database 
        SC_MANAGER_ALL_ACCESS);  // full access rights 
     
    if (schSCManager == NULL) 
    {
    AfxMessageBox("打开服务管理器的时候出现错误!"); 
    return FALSE;
    }
    SC_HANDLE schService = OpenService( 
            schSCManager,          // SCM database 
            SERVICENAME,          // service name
            SERVICE_ALL_ACCESS); 
     
        if (schService == NULL) 
        { 
    CloseServiceHandle(schSCManager);
            AfxMessageBox("打开服务 MSSQLSERVER 时出现错误"); 
    return FALSE;
        }
    //先停止服务
    if(!StopService(schService))
    {
    CloseServiceHandle(schService);
    CloseServiceHandle(schSCManager);
    return FALSE;
    }
    //启动服务
    if(!StartService(schService))
    {
    CloseServiceHandle(schService);
    CloseServiceHandle(schSCManager);
    return FALSE;
    } CloseServiceHandle(schService);
    CloseServiceHandle(schSCManager); return FALSE;
    }
      

  3.   

    //查询服务当前状态的处理函数
    BOOL QueryServiceStatus(CString strServiceName)
    {
    BOOL bCurrentStatus = FALSE; SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SERVICE_STATE_ALL);
     
    if (hSCM == NULL)
    {
    return bCurrentStatus;
    } SC_HANDLE hService =NULL;
    hService=::OpenService(hSCM, strServiceName, GENERIC_READ);
     
    if (hService == NULL)
    {
    ::CloseServiceHandle(hSCM);
    return bCurrentStatus;
    }  SERVICE_STATUS status;
    if(::QueryServiceStatus(hService,&status) == 0)
    {
    bCurrentStatus  = FALSE;  
    }
    else if(!(status.dwCurrentState & SERVICE_RUNNING))
    {
    bCurrentStatus  = TRUE;  
    }
     
    ::CloseServiceHandle(hService);
    ::CloseServiceHandle(hSCM); return bCurrentStatus;
    }
    与上述函数一样,StartService过程利用以下语句得到hService调用StartService即可; 
    BOOL StartService(CString strServiceName)
    {
    //...
    hService=::OpenService(hSCM, strServiceName, GENERIC_READ);
    //...
    StartService(hService,0,NULL));
    //...
    }