Thread: Stopping a service

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Stopping a service

    I'm trying to stop a sertvice, but the OpenService function fails, saying that the service doesn't exist...

    The GetErr function is just a function which returns system error info as text.

    Code:
    int stopSvc(LPTSTR achSvc){
      SERVICE_STATUS_PROCESS sp;
      SERVICE_STATUS ss;
      SC_HANDLE hScM, hSvc;
      DWORD dwBytesNeeded;
    
      if((hScM=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS))){
        if((hSvc=OpenService(hScM, achSvc, SERVICE_ALL_ACCESS))){
          if(!QueryServiceStatusEx(hSvc, SC_STATUS_PROCESS_INFO, (LPBYTE)&sp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded)){
            GetErr("QueryServiceStatusEx");
            return(0);
          }else if(sp.dwCurrentState == SERVICE_STOPPED) 
            return(1);
          if(ControlService(hSvc, SERVICE_CONTROL_STOP, &ss)>0){
            CloseServiceHandle(hSvc);
            CloseServiceHandle(hScM);
            return(1);
          }else
            GetErr("ControlService");
        }else
          GetErr("OpenService");
        CloseServiceHandle(hSvc);
      }else
        GetErr("OpenSCManager");
      CloseServiceHandle(hScM);
      return(0);
    }
    The service is running on the local machine, and I'm logged on as admin. Running XP SP2/VC.Net.
    Any clues?

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Found the error. I'm trying to stop the service by it's display name.. I'll have to find the actual name of the service instead..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. running my program as service
    By daher in forum Windows Programming
    Replies: 5
    Last Post: 09-05-2008, 12:30 PM
  2. Windows service status checking
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-01-2008, 08:19 AM
  3. eek a virus
    By MisterSako in forum Tech Board
    Replies: 17
    Last Post: 06-08-2005, 03:29 PM
  4. Replies: 1
    Last Post: 06-03-2005, 01:03 AM
  5. Another windows service question... starting/stopping
    By BrianK in forum Windows Programming
    Replies: 1
    Last Post: 03-20-2003, 12:22 AM