有没有C语言的API可以用来检查文件索引是否开启或关闭?如果有代码就更好了。
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();
}
}
在C++中,WMI很麻烦,但本地的Service 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
}