【问题标题】:How can a program tell if another process is running as a service?程序如何判断另一个进程是否作为服务运行?
【发布时间】:2010-05-01 14:38:39
【问题描述】:

我有一个 Win32 程序,我可以指导它来监视另一个 Win32 进程。

我想为监控程序找到一种方法来确定被监控的进程是否作为 Win32 服务运行。

并非所有服务都作为 SYSTEM 运行,也并非所有服务都将 services.exe 作为直接父级,因此我认为这些明显的技术不够强大。

要清楚,我正在寻找的是一种编写函数的方法:

bool isService(HANDLE aProcessHandle) { ... }

【问题讨论】:

    标签: windows-services service win32-process


    【解决方案1】:

    您可以使用 WMI 轻松完成此操作。我知道您没有指定 C#,但 WMI api 以非常相似的方式在所有平台上可用。

    首先我们需要一个形状像Win32_Service的对象

    public class Win32_Service
    {
        public Win32_Service(ManagementBaseObject obj)
        {
    
            AcceptPause = (bool)(obj["AcceptPause"] ?? false);
            AcceptStop = (bool)(obj["AcceptStop"] ?? false);
            Caption = (string)(obj["Caption"] ?? "");
            CheckPoint = (UInt32)(obj["CheckPoint"] ?? 0);
            CreationClassName = (string)(obj["CreationClassName"] ?? "");
            Description = (string)(obj["Description"] ?? "");
            DesktopInteract = (bool)(obj["DesktopInteract"] ?? false);
            DisplayName = (string)(obj["DisplayName"] ?? "");
            ErrorControl = (string)(obj["ErrorControl"] ?? "");
            ExitCode = (UInt32)(obj["ExitCode"] ?? 0);
            InstallDate = (DateTime)(obj["InstallDate"] ?? DateTime.MinValue);
            Name = (string)(obj["Name"] ?? "");
            PathName = (string)(obj["PathName"] ?? "");
            ProcessId = (UInt32)(obj["ProcessId"] ?? 0);
            ServiceSpecificExitCode = (UInt32)(obj["ServiceSpecificExitCode"] ?? 0);
            ServiceType = (string)(obj["ServiceType"] ?? "");
            Started = (bool)(obj["Started"] ?? false);
            StartMode = (string)(obj["StartMode"] ?? "");
            StartName = (string)(obj["StartName"] ?? "");
            State = (string)(obj["State"] ?? "");
            Status = (string)(obj["Status"] ?? "");
            SystemCreationClassName = (string)(obj["SystemCreationClassName"] ?? "");
            SystemName = (string)(obj["SystemName"] ?? "");
            TagId = (UInt32)(obj["TagId"] ?? 0);
            WaitHint = (UInt32)(obj["WaitHint"] ?? 0);
        }
        bool AcceptPause;
        bool AcceptStop;
        string Caption;
        UInt32 CheckPoint;
        string CreationClassName;
        string Description;
        bool DesktopInteract;
        string DisplayName;
        string ErrorControl;
        UInt32 ExitCode;
        DateTime InstallDate;
        string Name;
        string PathName;
        UInt32 ProcessId;
        UInt32 ServiceSpecificExitCode;
        string ServiceType;
        bool Started;
        string StartMode;
        string StartName;
        string State;
        string Status;
        string SystemCreationClassName;
        string SystemName;
        UInt32 TagId;
        UInt32 WaitHint;
    };
    

    现在我们向 WMI 查询服务。在这里,我只是拉取所有服务。如果您有更具体的条件,只需修改查询"Select * from Win32_Service"

    var services = new System.Collections.Generic.List<Win32_Service>();
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_Service"))
    {
        using (ManagementObjectCollection results = searcher.Get())
        {
            foreach (ManagementObject obj in results)
            {
                services.Add(new Win32_Service(obj));
            }
        }
    }
    

    现在可以使用 Linq 查询services

    参考:http://msdn.microsoft.com/en-us/library/ms974579.aspx

    【讨论】:

    • 我不确定如何使用这些信息来编写:bool isService(HANDLE aProcessHandle) { ... } 你能详细说明一下吗?
    • @quiet - 您询问如何确定特定进程是否作为服务运行。我提供了一个查询 WMI 以获取服务的示例,在本例中是所有服务,您可以查找您的进程名称。或者,您可能在查询中更具体,并明确搜索您的目标。如果返回,则它作为服务运行。您如何处理这些信息取决于您自己。 ;-) 我建议使用它作为在您的平台上编写一些简单的 WMI 代码的参考,c++?。使用您的HANDLE 获取processId 并与WMI 结果进行比较?
    • 好的。感谢您的指导。我可以从:technet.microsoft.com/en-us/library/ee198746.aspx 获得更多线索
    • 调用 WMI 的 C++ 示例可从此处获得:msdn.microsoft.com/en-us/library/aa390423%28v=VS.85%29.aspx
    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 2021-10-15
    • 2016-10-04
    • 1970-01-01
    • 2010-10-02
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多