Question

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

Was it helpful?

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
}

OTHER TIPS

WMI can provide this, use the Win32_Service class. Doing this in 'C' is fugly, the SDK only provides C++ samples. This is the equivalent C# code:

using System;
using System.Management;   // Add reference!!

class Program {
    public static void Main() {
        var searcher = new ManagementObjectSearcher("root\\CIMV2",
            "SELECT * FROM Win32_Service WHERE Name='wsearch'");

        foreach (ManagementObject queryObj in searcher.Get()) {
            Console.WriteLine("State = {0}", queryObj["State"]);
        }
        Console.ReadLine();
    }
}

To be pedantic, the C programming language does not have any knowledge of Windows file indexing or for that matter other platform-specific features. The ISO C standard specifies a strict set of API like for string handling, file handling (open, close etc.), arithmetic operations etc. and specifies and defines the primitive they act upon. These operations are agnostic of the underlying platform. All of these API are defined very strictly by the language specification itself (see ISO C99 standard for a current reference).

You would have to rely on an external (to the language) library to get the API you desire (API to find out whether the file indexing is on or off). So what you want to find out is a) what this library is b) what API to use from this library to call from your C program and c) how to link this library to your application among other things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top