+ Reply to Thread
Results 1 to 2 of 2

Thread: How to find list of dependent services of a specified windows service?

  1. #1

    How to find list of dependent services of a specified windows service?

    I wan to stop a service.

    I found that this service depends on many other services.

    I need to stop all of them and then I will be able to stop main service.

    Now, I have to find the dependent service list and need to stop them programmatically.

    How can I enumerate dependent services list using C++/WIN32 APIs?

  2. #2
    Following code can be useful to you.

    Code:
    BOOL GetDependentServicesList(SC_HANDLE hServiceControlManager, SC_HANDLE hService, CArray<CString, CString>& strServiceList)
    {
        DWORD					nIndex ;
        DWORD					dwServiceCount ;
    	DWORD					dwBytesNeeded ;
        LPENUM_SERVICE_STATUS   lpDependencies = NULL ;
        ENUM_SERVICE_STATUS     enumServiceStatus ;
    
        if (!EnumDependentServices(hService, SERVICE_ACTIVE, lpDependencies, 0, &dwBytesNeeded, &dwServiceCount)) 
        {
            if (GetLastError() != ERROR_MORE_DATA)
    		{
                return FALSE ;
    		}
    
            lpDependencies = (LPENUM_SERVICE_STATUS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwBytesNeeded );
            if (lpDependencies != NULL)
    		{
    			if (EnumDependentServices(hService, SERVICE_ACTIVE, lpDependencies, dwBytesNeeded, &dwBytesNeeded, &dwServiceCount ) )
    			{
    				for (nIndex = 0; nIndex<dwServiceCount; nIndex++ ) 
    				{
    					enumServiceStatus = *(lpDependencies + nIndex) ;
    					strServiceList.Add(enumServiceStatus.lpServiceName) ;
    				}
    			}
    			HeapFree(GetProcessHeap(), 0, lpDependencies) ;
    		}
        } 
    
        return TRUE;
    }
    
    BOOL GetDependentServicesList(const CString& strServiceName)
    {
    	BOOL bResult = FALSE ;
    
    	SC_HANDLE hServiceControlManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS) ;
    	if (NULL != hServiceControlManager)
    	{
    		SC_HANDLE hService = OpenService(hServiceControlManager, (LPCTSTR) strServiceName, SERVICE_ALL_ACCESS) ;
    		if (hService != NULL)
    		{
    			CArray<CString, CString> strServiceList ;
    			GetDependentServicesList(hServiceControlManager, hService, strServiceList) ;
    			for (int nIndex=0; nIndex < strServiceList.GetSize(); nIndex++)
    			{
    				CString strServiceName = strServiceList[nIndex] ;
    			}
    			CloseServiceHandle(hService) ;
    		}
    
    		CloseServiceHandle(hServiceControlManager) ;
    	}
    
    	return bResult ;
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

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