+ Reply to Thread
Results 1 to 2 of 2

Thread: How to make a windows service enable/disable programatically?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    38

    How to make a windows service enable/disable programatically?

    How to make a windows service enable/disable programatically?

    I know to start and stop the service but not enable or disable it.

    Any idea?

  2. #2
    Following code example can be helpful to you:

    Code:
    BOOL EnableOrDisableService(CString strServiceName, BOOL bIsEnable)
    {
    	BOOL bResult = FALSE ;
    
        SC_HANDLE hServiceControlManager ;
    	SC_HANDLE hService ;
    
        hServiceControlManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS) ;
    
        if (NULL != hServiceControlManager)
        {
    		hService = OpenService(hServiceControlManager, strServiceName.GetBuffer(0), SERVICE_CHANGE_CONFIG) ;
    		
    		if (hService != NULL)
    		{
    			bResult = ChangeServiceConfig(hService, SERVICE_NO_CHANGE,
    										bIsEnable ? SERVICE_AUTO_START : SERVICE_DISABLED,
    										SERVICE_NO_CHANGE,
    										NULL, NULL, NULL, NULL, NULL, NULL, NULL) ;
    			
    			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