How to check if Windows file indexing is turned on or off

Is there an API in C that I can use to check if file indexing is turned on or off? The code is evaluated.

+3
source share
3 answers

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
}
+1
source

WMI can provide this, use the Win32_Service class. Running this in "C" is unsustainable, the SDK 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();
    }
}
+1
source

, C Windows , , , . ISO C API, , , (, ..), .., , . . API (. ISO C99 ).

You will need to rely on an external (language) library to get the required API (an API to find out whether file indexing is turned on or off). So, what do you want to know: a) what is a library b) which API to use from this library to call from your C program and c) how to link this library with your application by the way.

-1
source

Source: https://habr.com/ru/post/1732215/


All Articles