系统服务的处理(C++版)

int  __fastcall  Server_Servers::QueryServerStats(AnsiString SERV,AnsiString Service)
{
    SERVICE_STATUS ssStatus;
    DWORD dwOldCheckPoint;
    SC_HANDLE schService , schSCManager ;
    schSCManager 
= OpenSCManager(
                SERV.c_str(),  
// pointer to machine name string
                NULL,  // pointer to database name string
                SC_MANAGER_ALL_ACCESS  // type of access
            );

    schService 
= OpenService(
                schSCManager,                      
//   SCM   database
                Service.c_str(),          //   service   name Topsky_ServerService
                SERVICE_ALL_ACCESS);
    
if (schService == NULL)
    
{
        PMSGDLG
->ShowErrorMsg("服务不存在!");
        
return -1;
    }


    
if (!QueryServiceStatus(
                schService,        
//   handle   to   service
                &ssStatus) )     //   address   of   status   information
    {
        PMSGDLG
->ShowErrorMsg("查询服务状态失败!");
        
return -1;
    }

    
else
        
return  ssStatus.dwCurrentState;


}

// 启动服务
void  __fastcall Server_Servers::StartServerServers(AnsiString SERV,AnsiString Service)
{
    SERVICE_STATUS ssStatus;
    DWORD dwOldCheckPoint;
    SC_HANDLE schService , schSCManager ;
    schSCManager 
= OpenSCManager(
                SERV.c_str(),  
// pointer to machine name string
                NULL,  // pointer to database name string
                SC_MANAGER_ALL_ACCESS  // type of access
            );

    schService 
= OpenService(
                schSCManager,                      
//   SCM   database
                Service.c_str(),          //   service   name Topsky_ServerService
                SERVICE_ALL_ACCESS);

    
if (schService == NULL)
    
{
        PMSGDLG
->ShowErrorMsg("服务不存在!");
        
return ;
    }


    
if (!StartService(schService, 0, NULL)) //
    {
        PMSGDLG
->ShowErrorMsg("无法启动服务!");
        
return ;
    }

    
else
        ;
// PMSGDLG->ShowErrorMsg("服务正在启动...");

    
//   Check   the   status   until   the   service   is   no   longer   start   pending.

    
if (!QueryServiceStatus(
                schService,        
//   handle   to   service
                &ssStatus) )     //   address   of   status   information
    {
        PMSGDLG
->ShowErrorMsg("查询服务状态失败!");
        
return ;
    }


    
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
    
{
        
//   Save   the   current   checkpoint.

        dwOldCheckPoint 
= ssStatus.dwCheckPoint;

        
//   Wait   for   the   specified   interval.

        Sleep(ssStatus.dwWaitHint);

        
//   Check   the   status   again.

        
if (!QueryServiceStatus(
                    schService,        
//   handle   to   service
                    &ssStatus) )     //   address   of   status   information
            break;

        
//   Break   if   the   checkpoint   has   not   been   incremented.

        
if (dwOldCheckPoint >= ssStatus.dwCheckPoint)
            
break;
    }


    
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
        ;
//  PMSGDLG->ShowErrorMsg("启动服务成功!");
    else
    
{
        PMSGDLG
->ShowErrorMsg("CurrentState:   " + AnsiString(ssStatus.dwCurrentState));
        PMSGDLG
->ShowErrorMsg("Win32ExitCode:   " + AnsiString(ssStatus.dwWin32ExitCode));
        PMSGDLG
->ShowErrorMsg("ServiceSpecificExitCode:   " + AnsiString(ssStatus.dwServiceSpecificExitCode));
        PMSGDLG
->ShowErrorMsg("CheckPoint:   " + AnsiString( ssStatus.dwCheckPoint));
        PMSGDLG
->ShowErrorMsg("WaitHint:   " + AnsiString( ssStatus.dwWaitHint));
    }

    CloseServiceHandle(schService);
}

// 停止服务
void  __fastcall Server_Servers::StopServerServers(AnsiString SERV,AnsiString Service)
{
    SC_HANDLE schSCManager , schService ;
    SERVICE_STATUS ssStatus;
    DWORD dwOldCheckPoint;
    schSCManager 
= OpenSCManager(SERV.c_str(), NULL, SC_MANAGER_ALL_ACCESS);
    
if (schSCManager != NULL)
    
{
        schService 
= OpenService(schSCManager, Service.c_str(), SERVICE_STOP | SERVICE_QUERY_STATUS);
        
if (schService != NULL)
        
{
            ControlService(schService, SERVICE_CONTROL_STOP, 
&ssStatus);
            Sleep(
2000);
            
if (!QueryServiceStatus(
                        schService,        
//   handle   to   service
                        &ssStatus) )     //   address   of   status   information
            {
                PMSGDLG
->ShowErrorMsg("查询服务状态失败!");
                
return ;
            }

            
while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
            
{
                
//   Save   the   current   checkpoint.
                dwOldCheckPoint = ssStatus.dwCheckPoint;
                
//   Wait   for   the   specified   interval.
                Sleep(ssStatus.dwWaitHint);
                
//   Check   the   status   again.
                if (!QueryServiceStatus(
                            schService,        
//   handle   to   service
                            &ssStatus) )     //   address   of   status   information
                    break;

                
//   Break   if   the   checkpoint   has   not   been   incremented.
                if (dwOldCheckPoint >= ssStatus.dwCheckPoint)
                    
break;
            }


            
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
                ;
//  PMSGDLG->ShowErrorMsg("停止服务成功!");
            else
            
{
                PMSGDLG
->ShowErrorMsg("CurrentState:   " + AnsiString(ssStatus.dwCurrentState));
                PMSGDLG
->ShowErrorMsg("Win32ExitCode:   " + AnsiString(ssStatus.dwWin32ExitCode));
                PMSGDLG
->ShowErrorMsg("ServiceSpecificExitCode:   " + AnsiString(ssStatus.dwServiceSpecificExitCode));
                PMSGDLG
->ShowErrorMsg("CheckPoint:   " + AnsiString( ssStatus.dwCheckPoint));
                PMSGDLG
->ShowErrorMsg("WaitHint:   " + AnsiString( ssStatus.dwWaitHint));
            }

            CloseServiceHandle(schService);

        }

        CloseServiceHandle(schSCManager);
    }

}

// 暂停服务
void  __fastcall Server_Servers::PauseServerServers(AnsiString SERV,AnsiString Service)
{
    SC_HANDLE schSCManager , schService ;
    SERVICE_STATUS ssStatus;
    DWORD dwOldCheckPoint;
    schSCManager 
= OpenSCManager(SERV.c_str(), NULL, SC_MANAGER_ALL_ACCESS);
    
if (schSCManager != NULL)
    
{
        schService 
= OpenService(schSCManager, Service.c_str(), SERVICE_PAUSE_CONTINUE);
        
if (schService != NULL)
        
{
            ControlService(schService, SERVICE_CONTROL_PAUSE, 
&ssStatus);
            Sleep(
3000);
            CloseServiceHandle(schService);
        }

        CloseServiceHandle(schSCManager);
    }

}

// 继续服务
void  __fastcall Server_Servers::ContinueServerServers(AnsiString SERV,AnsiString Service)
{
    SC_HANDLE schSCManager , schService ;
    SERVICE_STATUS ServiceStatus;
    schSCManager 
= OpenSCManager(SERV.c_str(), NULL, SC_MANAGER_ALL_ACCESS);
    
if (schSCManager != NULL)
    
{
        schService 
= OpenService(schSCManager, Service.c_str(), SERVICE_PAUSE_CONTINUE);
        
if (schService != NULL)
        
{
            QueryServiceStatus(schService, 
&ServiceStatus);
            ControlService(schService, SERVICE_CONTROL_CONTINUE, 
&ServiceStatus);
            Sleep(
3000);
            CloseServiceHandle(schService);
        }

        CloseServiceHandle(schSCManager);
    }

}

// 取得网上邻居
bool  __fastcall Server_Servers::EnumHosts(NETRESOURCE *  pNetRes)
{
    HANDLE hEnum 
= NULL;
    DWORD dwRet
= WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, pNetRes, &hEnum);
    
if (dwRet != NO_ERROR)
        
return false;

    NETRESOURCE
* pResBuf = new NETRESOURCE[8];
    DWORD cCount, dwBuf 
= 8*sizeof( NETRESOURCE);
    DWORD i;
    
do
    
{
        cCount 
= (DWORD)-1;
        dwRet 
= WNetEnumResource(hEnum, &cCount, (LPVOID)pResBuf, &dwBuf);
        
for ( i = 0; i < cCount; ++i)
        
{
            ;
//  AddHostToVector(&pResBuf[i]);
        }

        
if(pResBuf[i].dwUsage & RESOURCEUSAGE_CONTAINER)
            EnumHosts(
&pResBuf[i]);

        
if (dwRet == ERROR_NO_MORE_ITEMS)
        
//Enum finished.
            dwRet = NO_ERROR;
            
break;
        }

        
else if (dwRet != NO_ERROR)
            
break;
    }

    
while (dwRet == NO_ERROR);

    delete pResBuf;
    
return (dwRet == NO_ERROR);
}

 
  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

52vc

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值