SC_HANDLE hSC = ::OpenSCManager( NULL,
NULL, GENERIC_EXECUTE);
if( hSC == NULL)
{
return;
}
SC_HANDLE hSvc = ::OpenService( hSC, sSvcName.c_str(),
SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
if( hSvc == NULL)
{
::CloseServiceHandle( hSC);
return;
}
SERVICE_STATUS status;
if( ::QueryServiceStatus( hSvc, &status) == FALSE)
{
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
if( status.dwCurrentState == SERVICE_RUNNING)
{
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)
{
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
}
}
c++ 停止服务
最新推荐文章于 2022-10-29 19:20:31 发布