views:

137

answers:

3

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

+1  A: 

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();
    }
}
Hans Passant
Thanks, but I don't know C# nor I can use c#
Jessica
I gave you a point because you pointed me to the right direction
Jessica
A: 

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.

Gaurav Mathur
To be more pedantic here: if you have no answer to the question I asked please please refrain from giving an answer that is not connected to the question I asked. I clearly stated that i need an API in C. and the title and tags specifically states that it is for windows. so while I do like to learn, i really get annoyed by these kind of comments, which sadly seem to be the majority of the answers in ALL programming forums. You know the answer? write it down, if not please wait until someone does and then comments on it.
Jessica
+1  A: 

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
}
Luke
I'll give this a try. Do you know the service that is in charge of the indexing?Thanks for the answer
Jessica
On my XPSP3 machine, the Indexing Service is named "CiSvc" and the Windows Search service is named "WSearch".
Luke