1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

My Project:Service Manager

Discussion in 'Win32' started by alok, Sep 27, 2004.


Do u like The Application

  1. Yes

  2. NO

    0 vote(s)
  3. SomeWhat

Multiple votes are allowed.
  1. alok

    alok New Member

    Hello Guys, Back again with new Article. this time I am going to explain Enumeration of Windows service, it’s Starting & Stopping , Changing Startup Type between Automatic, Manual And Disable, And Most Important One Deleting the Service(Please handle this Option With Care, if this action is executed once ,there is no chance of rollback).

    I think every body knows what are Windows services, if not, let me explain it in brief,
    A service application conforms to the interface rules of the Service Control Manager (SCM). It can be started automatically at system boot, by a user through the Services control panel applet, or by an application that uses the service functions. Services can execute even when no user is logged on to the system.

    A driver service conforms to the device driver protocols. It is similar to a service application, but it does not interact with the SCM. For simplicity, the term service refers to a service application in this overview. ​


    Now Let Me Explain Two Main Api's Which Deal With Opening of Service Manager (which is handle whole service database ) and then Opening OF particular Service.
    SC_HANDLE OpenSCManager(  LPCTSTR lpMachineName, LPCTSTR lpDatabaseName, DWORD dwDesiredAccess);
    Here, if you want to access the local machine service database pass NULL in first two argument of openSCManager API to notify windows that we want to open local database of services.

    For accessing the remote machine services database, pass the target computer name must be prefixed by "\\".

    Important Note: You should have ADMIN Access to Operate SCManger other Wise you can only enumerate the Services but you can't alter them
    SC_HANDLE OpenService(
      SC_HANDLE hSCManager,
      LPCTSTR lpServiceName,
      DWORD dwDesiredAccess
    Here, the first Parameter is handle, that is Supplied by successful call Of OpenSCManager() API

    Second the Name OF SERVICE(Service name),which you want to Open

    Note: basically there are two type name One Display Name which we see when we Operate the Service Manager take look at next snapshot to get clear look.

    And third parameter is Some access flag for accessing services
    And Service name you got when you enumerate the service or you can see the properties of particular service get it real name, path etc look at next Screenshot for better understanding of this.

    I think you got the clear view of what are Service Name and Display name I according to Services Theory. :d

    Now this is the code for Enumerating all the services of local system, I will explain using à symbol in given code
    void CServiceMgrDlg::UpDateListCtrl()
    	//delete all item in listctrl
    	SC_HANDLE ScManager;-> Handle to service
    	ENUM_SERVICE_STATUS EnService[512];
    	DWORD   cbBufSize=512*sizeof(ENUM_SERVICE_STATUS);
    	// size of status buffer
    	DWORD lpServicesReturned;
    	DWORD   pcbBytesNeeded;           // buffer size needed
    	DWORD   lpResumeHandle=0;           // next entry
    	CString str,csTemp;
    ->here I am opening the service manager using OpenScManager API call

    check that whether we able to connect with local computer service databse or not
    	MessageBox("Error Opening Service Mgr");
    * Now call the EnumServicesstatus api call to enum all the service present in the computer
    * Also check that we are successful in retrieving the services or not
    * First Parameter is the handle return from successful call of OpenScManger
    * Second parameter indicate ,that we need only WIN32 based services
    * Third parameter return all the service whether it’s running or stop
    * Fourth parameter is a buffer of ENUM_SERVICE_STATUS in which all the services name will be return
    * Fifth parameter is buffer size of fourth parameter
    * Sixth parameter return with total byte of memory need to retrieve all the services
    * Seventh parameter return with total number of service return
    * And eight parameter indicate Pointer to a variable that, on input, specifies the starting point of enumeration. You must set this value to zero the first time this function is called. On output, this value is zero if the function succeeds. However, if the function returns zero and the GetLastError function returns ERROR_MORE_DATA, this value is used to indicate the next service entry to be read when the function is called to retrieve the additional data.

    if(::EnumServicesStatus(ScManager,SERVICE_WIN32,SERVICE_STATE_ALL,EnService,cbBufSize,&pcbBytesNeeded,&lpServicesReturned,&lpResumeHandle )==0)
    	MessageBox("Error Querrying Service Mgr");
    -> now check the state of each service
    for(int i=0;i<int(lpServicesReturned);i++)
    ->the ENUM_SERVICE_STATUS third parameter contain the status of each service
    	break; // The service is paused.
    	break;// The service is running.
    	break;// The service is stopped
    	break;// The service is starting
    	break;// The service is stopped
    	break;// The service is running.
    //block for start type
    	SC_HANDLE ScService;     
    	QUERY_SERVICE_CONFIG ServiceConfig;
    	DWORD CbBufSize, pcbBytesNeeded;
    	CString SerState;
    ->Now opening each service to get it Startup type,eg wheather it Automatic or Manual or disable.
    	"Problem in opening service";
    This api call will query the local database about the state of service.

    ->Here first parameter is hadle that we retrieve from successful call of OpenService

    -> second parameter is variable of QUERY_SERVICE_CONFIG which will receive the status
    			case SERVICE_AUTO_START:this->m_lstServices.SetItemText(i,4,"Automatic"); break;
    			case SERVICE_DEMAND_START:this->m_lstServices.SetItemText(i,4,"Manual"); break;
    			case SERVICE_DISABLED :this->m_lstServices.SetItemText(i,4,"Disable"); break;
    Here I have taken Access as SC_MANAGER_ENUMERATE_SERVICE| GENERIC_READ as this access guarantee to enumerate Services for each user because we are only enumerating them

    Working of above function is like this

    1. First we open the scmanager with 'SC_MANAGER_ENUMERATE_SERVICE | GENERIC_READ' access Using the OpenSCManager Api
    2. the using the handle Given by above Successful Call of SCManager, Call Api EnumServicesStatus to enumerate the service present in local system.the important parameter in this function is struct Buff ENUM_SERVICE_STATUS ,which will bring back all the service Present in the computer.
    3. I think rest of code can be easily understood by a beginner.
    Now let understand how to Stop & Start the services

    To use Every Function of SCManger, you must get handle from the openSCManager to operate on Services.

    NOTE: You should have Admin right to perform RUN and Stop the services on local machine

    First Get Handle from OpenScManger like this

    SC_HANDLE ScManager,ScService;//Declaring the Some USeful Variable

    I think till now you have understand use of OpenSCmanager API
    	//preparing Error Variable
    	MessageBox("Error Opening System Manager");
    * Now get the handle OF service Using Api Open Service
    * Here first parameter is handle receive from successful call of OpenSCManager Api
    * Second parameter is the name of service to open
    * Third determine the access type to open service, here I have taken access as SERVICE_ALL_ACCESS .
    	//preparing Error Variable
    	MessageBox("Error Opening Service");
    Now you run you service using This Little Piece of Code.

    now using StartService api we can Run the previously stop service.

    here first parameter is handle which we receive from successful call of OpenService api

    the second parameter indicate any command line argument pass to service

    the third parameter according to MSDN

    Pointer to an array of pointers to null-terminated argument strings passed to a service. Driver services do not receive these arguments. If no arguments are passed to the service being started, this parameter can be NULL.
    	//preparing Error Variable
    	MessageBox("Error Starting Service");
    And You Can Stop Your service using this Little Piece Of Code. let me explain it to you
    -> First parameter is common as of above
    -> Second parameter specifies the requested control code
    -> Third parameter according to MSDN

    Pointer to a SERVICE_STATUS structure that receives the latest service status information. The information returned reflects the most recent status that the service reported to the service control manager.
    	//preparing Error Variable
    	MessageBox("Error: Unable to Stop Service");
    And For Restarting there is no direct Api so you have to play the following trick for restarting the service “ First Stop the service And then run the service again “: d.

    Now Most important and Dangerous One (Deleting the service)

    Caution: please use this code with utmost care, because once it delete the service, this action can't be rollback. Unless you know how to install that service again

    Note: You need Admin right to perform this Action otherwise the action failed

    Here the Little Piece of code same way get the handle of both SCManager and Service

    SC_HANDLE ScManager,ScService;//Declaring the Some Useful Variable
    	//preparing Error Variable
    	MessageBox("Error Opening System Manager");
    	//preparing Error Variable
    	MessageBox("Error Opening Service");
    ->for deleting the service there is very simple api named

    deleteService and only parameter it take is the handle of particular service
    	//preparing Error Variable
    	MessageBox("Error Deleting Service");
    	MessageBox("Service Marked For Deletion\nDeleted After Computer Restart");
    The service after deletion was marked by SCmanger for Deletion and it get deleted after the computer is rebooted.

    I think I have explain each and very line of code, if you still face problem you can mail me in this regards


    Attached Files:

  2. parvez.yu

    parvez.yu New Member

    lengthy explanation
  3. chomchom

    chomchom New Member

    thank u
  4. rajeev_rsd

    rajeev_rsd New Member

    One Beginner Question:

    As the Code above is written using Win32 API, is it compatible with all Windows Versions (like XP, VISTA, 7 both x86 and x64 editions)?

    By the way, lengthy but perfect explanation.

    - Rajeev
  5. naimish

    naimish New Member

    Yes 100.00%
    NO 0%
    What 9.09%

    Total 109.09% ?????

Share This Page