启动和停止服务的控制程序, 网上找的, 稍加改动
2007-09-13 10:53
489 查看
void CNet_service_controlDlg::OnButton1()
{
// 打开服务管理对象
SC_HANDLE hSC = ::OpenSCManager( NULL,
NULL, GENERIC_EXECUTE);
if( hSC == NULL)
{
TRACE( "open SCManager error");
return;
}
// 打开www服务。
SC_HANDLE hSvc = ::OpenService( hSC, "Apache2",
SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
if( hSvc == NULL)
{
TRACE( "Open www error。");
SaveToLog("---打开Apache2服务错误---");
::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)
{
CTime t=CTime::GetCurrentTime();
int hou=t.GetHour();//还要判断一下时间 if(hou >= 22)
{
SaveToLog("---停止Apache2服务时时间不对,退出---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 停止服务
if( ::ControlService( hSvc,
SERVICE_CONTROL_STOP, &status) == FALSE)
{
TRACE( "stop service error。");
SaveToLog("---停止Apache2服务错误---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务停止
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_STOPPED)
{
AfxMessageBox( "stop success。");//不要提示,直接写入日志
SaveToLog("---停止Apache2服务成功---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
OnOK();return;
}
}
}
else if( status.dwCurrentState == SERVICE_STOPPED)
{
// 启动服务
if( ::StartService( hSvc, NULL, NULL) == FALSE)
{
TRACE( "start service error。");
SaveToLog("---启动Apache2服务错误---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务启动
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_RUNNING)
{
AfxMessageBox( "start success。");//不要提示,直接写入日志
SaveToLog("---启动Apache2服务成功---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
OnOK();return;
}
}
}
TRACE( "start error。");
SaveToLog("---控制Apache2服务时发生未知错误---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
OnOK();
return;
}
{
// 打开服务管理对象
SC_HANDLE hSC = ::OpenSCManager( NULL,
NULL, GENERIC_EXECUTE);
if( hSC == NULL)
{
TRACE( "open SCManager error");
return;
}
// 打开www服务。
SC_HANDLE hSvc = ::OpenService( hSC, "Apache2",
SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
if( hSvc == NULL)
{
TRACE( "Open www error。");
SaveToLog("---打开Apache2服务错误---");
::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)
{
CTime t=CTime::GetCurrentTime();
int hou=t.GetHour();//还要判断一下时间 if(hou >= 22)
{
SaveToLog("---停止Apache2服务时时间不对,退出---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 停止服务
if( ::ControlService( hSvc,
SERVICE_CONTROL_STOP, &status) == FALSE)
{
TRACE( "stop service error。");
SaveToLog("---停止Apache2服务错误---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务停止
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_STOPPED)
{
AfxMessageBox( "stop success。");//不要提示,直接写入日志
SaveToLog("---停止Apache2服务成功---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
OnOK();return;
}
}
}
else if( status.dwCurrentState == SERVICE_STOPPED)
{
// 启动服务
if( ::StartService( hSvc, NULL, NULL) == FALSE)
{
TRACE( "start service error。");
SaveToLog("---启动Apache2服务错误---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务启动
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_RUNNING)
{
AfxMessageBox( "start success。");//不要提示,直接写入日志
SaveToLog("---启动Apache2服务成功---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
OnOK();return;
}
}
}
TRACE( "start error。");
SaveToLog("---控制Apache2服务时发生未知错误---");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
OnOK();
return;
}
相关文章推荐
- IIS网站已经停止,提示服务器没有及时响应启动或控制请求!本地计算机法启动 World Wide Web Publishing 服务。提示 “错误127: 找不到指定的程序”或者“错误1721,资源不足,无法启动”
- IIS网站已经停止,提示服务器没有及时响应启动或控制请求!本地计算机法启动 World Wide Web Publishing 服务。提示 “错误127: 找不到指定的程序”或者“错误1721,资源不足,无法启动
- 对NT服务型程序的控制代码。如安装服务、启动、停止服务、取服务状态等。
- 对NT服务型程序的控制代码。如安装服务、启动、停止服务、取服务状态等。
- mysql5启动报错:本地计算机 上的 MySQL 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
- 进程服务编写,与启动停止控制
- 如何使用命令行来控制IIS服务的启动和停止
- 使用shell脚本批处理控制大数据环境服务启动停止
- asp.net控制Sql Server服务的启动与停止
- 本地计算机 上的 OracleOraDb10g_home1TNSListener 服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
- Oracleoradb12home1tnslistener服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止。
- 使用shell脚本批处理控制大数据环境服务启动停止
- OracleOraDb11g_home1TNSListener服务启动后停止,某些服务在未由其他服务或程序使用时将自己主动停止
- windows NT 服务程序的创建,删除,启动,停止
- 本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止?
- 本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止?
- 本地计算机上的XXX服务启动后停止。某些服务在未由其它服务或程序使用时将自动停止
- windows 10安装mysql 5.7后,启动服务提示“本地计算机上的mysql服务启动后停止,某些服务在未由其他服务或程序使用时将自动停止。
- 本地计算机上的XXX服务启动后停止。某些服务在未由其它服务或程序使用时将自动停止。咋整?
- OracleOraDb11g_home1TNSListener服务启动后停止,某些服务在未由其它服务或程序使用时将自动停止