+ Reply to Thread
Results 1 to 2 of 2

Thread: How to find if windows service is started or not by its name?

  1. #1

    How to find if windows service is started or not by its name?

    I want to check whether my service is running or not.

    How can I do this in MFC/WIN32 application?

    Please help.

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    46
    Use following code to test whether service is running or not.

    Code:
    BOOL IsServiceRunning(CString strServiceName)
    {
    	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_QUERY_STATUS) ;
    		
    		if (hService != NULL)
    		{
    			SERVICE_STATUS_PROCESS serviceStatusProcess ;
    			DWORD dwBytesNeeded = 0 ;
    			if (QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE) &serviceStatusProcess, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded))
    			{
    				if (serviceStatusProcess.dwCurrentState == SERVICE_RUNNING)
    				{
    					bResult = TRUE ;
    				}
    			}
    
        		CloseServiceHandle(hService) ;
    		}
    		
    		CloseServiceHandle(hServiceControlManager) ;
        }
    
    	return bResult ;
    }
    Enjoy!

+ 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