CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2010
    Posts
    3

    wmiService not stopping with ControlService()

    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.

    Code:
    	//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;
    	 }

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: wmiService not stopping with ControlService()

    First, try to step into procedural programming and avoid three-in-one functions like you have posted here.
    Last edited by ovidiucucu; April 27th, 2010 at 04:15 PM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured