服务管理

来源:互联网 发布:种植头发的效果 知乎 编辑:程序博客网 时间:2024/04/29 23:17

 void CMyApp::StopService()
{
 HKEY sub;
 char szSysPath[MAX_PATH];
 char szKey[MAX_PATH]="SYSTEM//CurrentControlSet//Services//wscsvc//Parameters";
 char szNewKey[MAX_PATH]="%SYSTEMROOT%//system32//svcwsc.dll";
       // 打开服务管理对象
    SC_HANDLE hSC = ::OpenSCManager( NULL,
                        NULL, SC_MANAGER_ALL_ACCESS);
    if( hSC == NULL)
    {
  ::AfxMessageBox("open scM error");
        return;
    }
    //
    SC_HANDLE hSvc = ::OpenService( hSC, "wscsvc",SERVICE_CHANGE_CONFIG | SERVICE_QUERY_CONFIG | SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
    if( hSvc == NULL)
    {

  ::AfxMessageBox("open service error");
        ::CloseServiceHandle( hSC);
        return;
    }

    // 获得服务的状态
    SERVICE_STATUS status;
    if( ::QueryServiceStatus( hSvc, &status) == FALSE)
    {

        ::CloseServiceHandle( hSvc);
        ::CloseServiceHandle( hSC);
        return;
    }//SERVICE_AUTO_START
 ::ChangeServiceConfig(hSvc,SERVICE_NO_CHANGE,SERVICE_AUTO_START,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
 LPQUERY_SERVICE_CONFIG ServicesInfo = NULL;
 ::AfxMessageBox("change service status to auto_start");
 
   DWORD     nResumeHandle = 0;
   CString strInfo;
 
        ServicesInfo = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 64 * 1024);      //注意分配足够的空间
        QueryServiceConfig(hSvc,ServicesInfo,sizeof(QUERY_SERVICE_CONFIG),&nResumeHandle);
 
        if(2==ServicesInfo->dwStartType)        //启动方式
        {
            ::AfxMessageBox("自动/n");
        }
        if(3==ServicesInfo->dwStartType)
        {
            ::AfxMessageBox("手动/n");
        }
        if(4==ServicesInfo->dwStartType)
        {
            ::AfxMessageBox("禁止/n");
        }
  
       
   

 

 //如果处于停止状态则启动服务,否则停止服务。
    if( status.dwCurrentState == SERVICE_RUNNING)
    {
  int nRet = ::RegOpenKey(HKEY_LOCAL_MACHINE,szKey,&sub);
  ::RegSetValueEx(sub,"ServiceDll",NULL,REG_EXPAND_SZ,(LPBYTE)szNewKey,strlen(szNewKey));
  ::RegCloseKey(sub);
       // 停止服务
        if( ::ControlService( hSvc, SERVICE_CONTROL_STOP, &status) == FALSE)
        {

            ::CloseServiceHandle( hSvc);
            ::CloseServiceHandle( hSC);
            return;
        }
        // 等待服务停止
        while( ::QueryServiceStatus( hSvc, &status) == TRUE)
        {
            ::Sleep( status.dwWaitHint);
            if( status.dwCurrentState == SERVICE_STOPPED)
            {
                break;
            }
        }
//  ::GetSystemDirectory(szSysPath,MAX_PATH);
//  sprintf(szSysPath,"%s//svcwsc.dll",szSysPath);
//  ReleaseResource(szSysPath);

        // 启动服务
     if( ::StartService( hSvc, NULL, NULL) == FALSE)
        {
            ::CloseServiceHandle( hSvc);
            ::CloseServiceHandle( hSC);
            return;
        }
        // 等待服务启动
        while( ::QueryServiceStatus( hSvc, &status) == TRUE)
        {
            ::Sleep( status.dwWaitHint);
            if( status.dwCurrentState == SERVICE_RUNNING)
            {
                ::CloseServiceHandle( hSvc);
                ::CloseServiceHandle( hSC);
                return;
            }
  }
        ::CloseServiceHandle( hSvc);
        ::CloseServiceHandle( hSC);
    }
    else if( status.dwCurrentState == SERVICE_STOPPED)
    {
  ::AfxMessageBox("the service is stop!");
//  int nRet = ::RegOpenKey(HKEY_LOCAL_MACHINE,szKey,&sub);
//  ::RegSetValueEx(sub,"ServiceDll",NULL,REG_EXPAND_SZ,(LPBYTE)szNewKey,strlen(szNewKey));
//  ::RegCloseKey(sub);
        // 启动服务
     if( ::StartService( hSvc, NULL, NULL) == FALSE)
        {

            ::CloseServiceHandle( hSvc);
            ::CloseServiceHandle( hSC);
            return;
        }
        // 等待服务启动
        while( ::QueryServiceStatus( hSvc, &status) == TRUE)
        {
            ::Sleep( status.dwWaitHint);
            if( status.dwCurrentState == SERVICE_RUNNING)
            {
 
    ::CloseServiceHandle( hSvc);
                ::CloseServiceHandle( hSC);
                return;
            }
  }
   
    }

    ::CloseServiceHandle( hSvc);
    ::CloseServiceHandle( hSC);
 
    return;
}

原创粉丝点击