如何检查 Windows 文件索引是否打开或关闭

发布于 2024-08-21 09:54:15 字数 45 浏览 9 评论 0原文

C 中是否有一个 API 可用于检查文件索引是否打开或关闭? 代码受到赞赏。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染火枫林 2024-08-28 09:54:15

WMI 在 C++ 中是一个痛苦,但本机服务 API 非常干净。

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 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
}
不回头走下去 2024-08-28 09:54:15

WMI 可以提供此功能,使用 Win32_Service 类。在“C”中执行此操作很麻烦,SDK 仅提供 C++ 示例。这是等效的 C# 代码:

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();
    }
}

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();
    }
}
薯片软お妹 2024-08-28 09:54:15

迂腐地说,C 编程语言不了解 Windows 文件索引或其他特定于平台的功能。 ISO C 标准指定了一组严格的 API,例如字符串处理、文件处理(打开、关闭等)、算术运算等,并指定和定义了它们所作用的原语。这些操作与底层平台无关。所有这些 API 都由语言规范本身非常严格地定义(有关当前参考,请参阅 ISO C99 标准)。

您必须依赖外部(对于语言)库来获取您想要的 API(用于查明文件索引是否打开或关闭的 API)。因此,您想要了解的是 a) 这个库是什么 b) 从这个库使用什么 API 来从您的 C 程序调用以及 c) 如何将此库链接到您的应用程序等等。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文