Search code examples
cwinapiindexing-service

How to check whether windows file indexing is ON or OFF


Is there an API in C that I can use to check whether file indexing is on or off? Code is appreciated.


Solution

  • WMI is a pain in C++, but the native Service API is pretty clean.

    SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
    if(hSCManager)
    {
        SC_HANDLE hService = OpenService(hSCManager, _T("ServiceNameGoesHere"), SERVICE_QUERY_STATUS);
        if(hService)
        {
            // service is installed
            SERVICE_STATUS ServiceStatus;
            if(ServiceQueryStatus(hService, &ServiceStatus))
            {
                // service is running
                // get current state from ServiceStatus.dwCurrentState
            }
            else if(GetLastError() == ERROR_SERVICE_NOT_ACTIVE)
            {
                // service is not running
            }
            else
            {
                // error
            }
            CloseServiceHandle(hService);
            hService = NULL;
        }
        else if(GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
        {
            // service is not installed
        }
        else
        {
            // error
        }
        CloseServiceHandle(hSCManager);
        hSCManager = NULL;
    }
    else
    {
        // error
    }