16,551
社区成员
发帖
与我相关
我的任务
分享
bool StartServer( LPCSTR ServerName )
{
printf( "start %s service", ServerName );
// 打开服务管理对象
SC_HANDLE hSC = ::OpenSCManager( NULL, NULL, GENERIC_EXECUTE );
if( hSC == NULL)
{
printf( "\nOpen SCManager error.\n");
return false;
}
// 打开服务
SC_HANDLE hSvc = ::OpenService( hSC, ServerName, SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP );
if( hSvc == NULL)
{
printf( "\nOpen %s error.\n", ServerName );
::CloseServiceHandle( hSC);
return false;
}
// 获得服务的状态
SERVICE_STATUS status;
if( ::QueryServiceStatus( hSvc, &status) == FALSE)
{
printf( "\nGet Service state error.\n");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return false;
}
// 如果处于停止状态则启动服务
if( status.dwCurrentState == SERVICE_STOPPED)
{
// 启动服务
if( ::StartService( hSvc, NULL, NULL) == FALSE)
{
printf( "\nstart service error.\n");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return false;
}
// 等待服务启动
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
printf( "." );
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_RUNNING)
{
printf( "\tstart success.\n");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return false;
}
}
printf( "\tstart %s error.\n", ServerName );
}
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return true;
}
bool StopServer( LPCSTR ServerName )
{
printf( "\nstop %s service", ServerName );
// 打开服务管理对象
SC_HANDLE hSC = ::OpenSCManager( NULL, NULL, GENERIC_EXECUTE );
if( hSC == NULL)
{
printf( "\nOpen SCManager error.\n");
return false;
}
// 打开服务
SC_HANDLE hSvc = ::OpenService( hSC, ServerName, SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP );
if( hSvc == NULL)
{
printf( "\nOpen %s error.\n", ServerName );
::CloseServiceHandle( hSC);
return false;
}
// 获得服务的状态
SERVICE_STATUS status;
if( ::QueryServiceStatus( hSvc, &status) == FALSE)
{
printf( "\nGet Service state error.\n");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return false;
}
//如果处于运行状态则停止服务
if( status.dwCurrentState == SERVICE_RUNNING)
{
// 停止服务
if( ::ControlService( hSvc, SERVICE_CONTROL_STOP, &status) == FALSE)
{
printf( "\nstop service error.\n");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return false;
}
// 等待服务停止
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
printf( "." );
::Sleep( 1000);
if( status.dwCurrentState == SERVICE_STOPPED)
{
printf( "\tstop success.");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return true;
}
}
printf( "\tstop %s error.", ServerName );
}
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return true;
}
bool StartServer( LPCSTR ServerName )
{
char szCmd[MAX_PATH] = "net start ";
strcat( szCmd, ServerName );
// 打开服务管理对象
SC_HANDLE hSC = ::OpenSCManager( NULL, NULL, GENERIC_EXECUTE );
if( hSC == NULL)
{
printf( "Open SCManager error.\n");
return false;
}
// 打开服务
SC_HANDLE hSvc = ::OpenService( hSC, ServerName, SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP );
if( hSvc == NULL)
{
printf( "Open %s error.\n", ServerName );
::CloseServiceHandle( hSC);
return false;
}
// 获得服务的状态
SERVICE_STATUS status;
if( ::QueryServiceStatus( hSvc, &status) == FALSE)
{
printf( "Get Service state error.\n");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return false;
}
if( status.dwCurrentState == SERVICE_RUNNING )
{
printf( "Service %s is running already.\n", ServerName );
return true;
}
// 如果处于停止状态则启动服务
else if( status.dwCurrentState == SERVICE_STOPPED)
{
// 启动服务
system( szCmd );
// 等待服务启动
if( ::QueryServiceStatus( hSvc, &status ) == TRUE )
{
if( status.dwCurrentState == SERVICE_RUNNING)
{
printf( "Start %s success.\n", ServerName );
::CloseServiceHandle( hSvc );
::CloseServiceHandle( hSC );
return true;
}
}
printf( "Start %s failed.\n", ServerName );
}
::CloseServiceHandle( hSvc );
::CloseServiceHandle( hSC );
return false;
}
bool StopServer( LPCSTR ServerName )
{
char szCmd[MAX_PATH] = "net stop ";
strcat( szCmd, ServerName );
printf( "Stop %s service", ServerName );
// 打开服务管理对象
SC_HANDLE hSC = ::OpenSCManager( NULL, NULL, GENERIC_EXECUTE );
if( hSC == NULL)
{
printf( "Open SCManager error.\n");
return false;
}
// 打开服务
SC_HANDLE hSvc = ::OpenService( hSC, ServerName, SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP );
if( hSvc == NULL)
{
printf( "Open %s error.\n", ServerName );
::CloseServiceHandle( hSC );
return false;
}
// 获得服务的状态
SERVICE_STATUS status;
if( ::QueryServiceStatus( hSvc, &status) == FALSE)
{
printf( "Get Service state error.\n");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return false;
}
//如果处于运行状态则停止服务
if( status.dwCurrentState == SERVICE_STOPPED )
{
printf( "Service %s has stopped already.\n", ServerName );
return true;
}
if( status.dwCurrentState == SERVICE_RUNNING)
{
// 停止服务
system( szCmd );
// 等待服务停止
if( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
if( status.dwCurrentState == SERVICE_STOPPED)
{
printf( "Stop service %s success.\n", ServerName );
::CloseServiceHandle( hSvc );
::CloseServiceHandle( hSC );
return true;
}
}
printf( "Stop Service %s failed.\n", ServerName );
}
::CloseServiceHandle( hSvc );
::CloseServiceHandle( hSC );
return false;
}