unsigned _stdcall ForbiddenServer(VOID* ServerName)//forbiddenServer
{
log_printf("%S ForbiddenServer start!!!",(TCHAR*)ServerName);
bool ret = FALSE;
SC_HANDLE hSC = ::OpenSCManager( NULL,NULL, SC_MANAGER_ALL_ACCESS/*GENERIC_EXECUTE*/);
if( hSC == NULL)
{
log_printf("OpenSCManager error = %d ,ServerName = %S",GetLastError(),ServerName);
return ret;
}
// 打开www服务。
SC_HANDLE hSvc = ::OpenService( hSC, (TCHAR*)ServerName, SC_MANAGER_ALL_ACCESS /*SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP*/);
if( hSvc == NULL)
{
log_error("OpenService faile = %d,ServerName = %S ",GetLastError(),ServerName);
::CloseServiceHandle( hSC);
return ret;
}
// 获得服务的状态
LPQUERY_SERVICE_CONFIG ServicesInfo = NULL;
DWORD nResumeHandle = 0;
SERVICE_STATUS status;
ServicesInfo = ServicesInfo=(LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR,8*1024); //注意分配足够的空间
if(!::QueryServiceConfig(hSvc,ServicesInfo,8*1024,&nResumeHandle))
{
log_printf("%S QueryServiceConfig error = %d",GetLastError(),(TCHAR*)ServerName);
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return ret; //枚举各个服务信息
}
else
{
if (ServicesInfo->dwStartType == SERVICE_DISABLED)//服务已经是禁用状态
{
log_printf("%S has been disabled.",(TCHAR*)ServerName);
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return ret = TRUE;
}
}
//服务未禁用
if( ::QueryServiceStatus( hSvc, &status) == FALSE)
{
log_printf("%S QueryServiceStatus error = %d",(TCHAR*)ServerName,GetLastError());
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return ret;
}
//如果处于停止状态则启动服务,否则停止服务。
if( status.dwCurrentState == SERVICE_RUNNING)
{
DWORD i;
DWORD dwBytesNeeded;
DWORD dwCount;
LPENUM_SERVICE_STATUS lpDependencies = NULL;
ENUM_SERVICE_STATUS ess;
SC_HANDLE hDepService;
// Pass a zero-length buffer to get the required buffer size
if ( EnumDependentServices( hSvc, SERVICE_ACTIVE,
lpDependencies, 0, &dwBytesNeeded, &dwCount ) )
{
// If the Enum call succeeds, then there are no dependent
// services so do nothing
log_printf("%S there are no dependent",(TCHAR*)ServerName);
}
else
{
if ( GetLastError() != ERROR_MORE_DATA )
return GetLastError(); // Unexpected error
// Allocate a buffer for the dependencies
lpDependencies = (LPENUM_SERVICE_STATUS) HeapAlloc(
GetProcessHeap(), HEAP_ZERO_MEMORY, dwBytesNeeded );
if ( !lpDependencies )
return GetLastError();
__try {
// Enumerate the dependencies
if ( !EnumDependentServices( hSvc, SERVICE_ACTIVE,
lpDependencies, dwBytesNeeded, &dwBytesNeeded,
&dwCount ) )
return GetLastError();
for ( i = 0; i < dwCount; i++ )
{
DWORD dwStartTime = GetTickCount();
ess = *(lpDependencies + i);
// Open the service
hDepService = OpenService( hSC, ess.lpServiceName,
SERVICE_STOP | SERVICE_QUERY_STATUS );
if ( !hDepService )
return GetLastError();
__try {
// Send a stop code
if ( !ControlService( hDepService, SERVICE_CONTROL_STOP,
&status ) )
return GetLastError();
// Wait for the service to stop
while ( status.dwCurrentState != SERVICE_STOPPED )
{
Sleep( status.dwWaitHint );
if ( !QueryServiceStatus( hDepService, &status ) )
{
log_error("%S QueryServiceStatus faile = %d",ess.lpServiceName, GetLastError());
break;
}
if ( status.dwCurrentState == SERVICE_STOPPED )
break;
if ( GetTickCount() - dwStartTime > 5000 )
{
log_error("%S SERVICE_CONTROL_STOP Timeout",ess.lpServiceName);
break;
}
}
}
__finally
{
// Always release the service handle
CloseServiceHandle( hDepService );
}
}
}
__finally
{
// Always free the enumeration buffer
HeapFree( GetProcessHeap(), 0, lpDependencies );
}
}
// 停止服务
if( ::ControlService( hSvc,SERVICE_CONTROL_STOP, &status) == FALSE)
{
log_printf("%S SERVICE_CONTROL_STOP error = %d",(TCHAR*)ServerName,GetLastError());
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return ret;
}
// 等待服务停止
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep(status.dwWaitHint);
if( status.dwCurrentState == SERVICE_STOPPED)
{
log_printf("%S SERVICE_CONTROL_STOP Success",(TCHAR*)ServerName);
if(!ChangeServiceConfig(hSvc,SERVICE_NO_CHANGE,SERVICE_DISABLED,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL))
{
int error = GetLastError();
log_printf("%S ChangeServiceConfig failed = %d",(TCHAR*)ServerName,error);
}
else
{
log_printf("%S ChangeServiceConfig success",(TCHAR*)ServerName);
}
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
log_printf("%S ForbiddenServer end!!!",(TCHAR*)ServerName);
return ret;
}
}
}
else if( status.dwCurrentState == SERVICE_STOPPED)
{
// 启动服务
// if( ::StartService( hSvc, NULL, NULL) == FALSE)
// {
//log_printf("SERVICE_CONTROL_START error");
// ::CloseServiceHandle( hSvc);
// ::CloseServiceHandle( hSC);
// return 0;
// }
// // 等待服务启动
// while( ::QueryServiceStatus( hSvc, &status) == TRUE)
// {
// ::Sleep( status.dwWaitHint);
// if( status.dwCurrentState == SERVICE_RUNNING)
// { log_printf("SERVICE_CONTROL_START Success");
// ::CloseServiceHandle( hSvc);
// ::CloseServiceHandle( hSC);
// return 0;
// }
// }
if(!ChangeServiceConfig(hSvc,SERVICE_NO_CHANGE,SERVICE_DISABLED,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL))
{
int error = GetLastError();
log_printf("%S ChangeServiceConfig failed = %d",(TCHAR*)ServerName,error);
}
else
{
log_printf("%S ChangeServiceConfig success",(TCHAR*)ServerName);
ret = TRUE;
}
}
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
log_printf("%S ForbiddenServer end!!!",(TCHAR*)ServerName);
return ret;
}
windows服务禁用
最新推荐文章于 2023-06-26 17:16:28 发布