开发者

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 appr开发者_运维百科eciated.


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
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜