Click to See Complete Forum and Search --> : wmiService not stopping with ControlService()


MisterNeutral
April 26th, 2010, 09:34 AM
A custom application for my office requires stopping WMI in order to remove some folders in the Windows Repository folder. The application is running as a service, which is trying to tell WMI to stop.

I'm having difficult getting the darn thing to stop in code. There could be a lot of reasons, some of which are that:

1. WMI has two child dependent services
2. WMI may have some sort of protection on it.
3. The child services aren't stopping
4. im doin et rong

The code I'm using is below. As you can see, it's just a simple service control manager and some control service calls. Any insight is really appreciated.

Note: WMI depends on ICS|Firewall & Windows Security Center.


//Try to open an SCManager
if (!(schSCManager = OpenSCManager(NULL, NULL, DELETE)))
{
logger<< "Error occured creating service manager for WMI. Error code: " << GetLastError() <<endl;
return 1;
}


//Open Service to stop Windows Security Center
if(!(wmiService = OpenService(schSCManager, L"wscsvc", SERVICE_ALL_ACCESS)))
{
logger << "Could not open the Windows Security Center service. Error code: " << GetLastError() << endl;
CloseServiceHandle(schSCManager);
return 2;
}

//Stop Windows Security Center service.
if(!(ControlService(wmiService, SERVICE_CONTROL_STOP, &wmiServiceStatus)))
{
logger << "Failed to stop the Windows Security Center Service." <<GetLastError() <<endl;
CloseServiceHandle(schSCManager);
return 3;
}

//Null out WMI service handle;
wmiService = NULL;

Sleep(10000);

//Open Service to stop ICS/Firewall
if(!(wmiService = OpenService(schSCManager, L"SharedAccess", SERVICE_ALL_ACCESS)))
{
logger << "Could not open the ICS service. Error code: " << GetLastError() << endl;
CloseServiceHandle(schSCManager);
return 2;
}

//Stop ISC/Firewall.
if(!(ControlService(wmiService, SERVICE_CONTROL_STOP, &wmiServiceStatus)))
{
logger << "Failed to stop the ICS Service." <<GetLastError() <<endl;
CloseServiceHandle(schSCManager);
return 3;
}



//Null out WMI service handle;
wmiService = NULL;



Sleep(10000);

//Open WMI to stop the service temporarily
if(!(wmiService = OpenService(schSCManager, L"winmgmt", SERVICE_ALL_ACCESS)))
{
logger << "Could not open the WMI service. Error code: " << GetLastError() << endl;
CloseServiceHandle(schSCManager);
return 2;
}

//Stop WMI service.
if(!(ControlService(wmiService, SERVICE_CONTROL_STOP, &wmiServiceStatus)))
{
logger << "Failed to stop WMI Service." <<GetLastError() <<endl;
CloseServiceHandle(schSCManager);
return 3;
}

//Close SCManager to get new SCmanager.
if (!(CloseServiceHandle(schSCManager)))
{
logger << "Could not close Service Manager after stopping WMI";
return 4;
}

ovidiucucu
April 27th, 2010, 04:03 PM
First, try to step into procedural programming and avoid three-in-one functions like you have posted here.