将 .Net ManagementBaseObject 转换为 ManagementObject

发布于 2024-07-25 08:42:49 字数 1392 浏览 1 评论 0原文

我试图使用以下代码来写出计算机上启动的所有进程。 我的问题是,EventArrived 方法传递了一个 EventArrivedEventArgs,它具有 ManagementBaseObject 类型的 NewEvent 属性。 它没有 InvokeMethod 方法。 我可以通过某种方式将 NewEvent 属性转换为 ManagementObject,还是需要重新查询并创建 ManagementObject? 下面的代码效果很好,但不想重新查询。

Imports System.Management

Public NotInheritable Class EntryPoint

    Public Shared Sub Main(ByVal args() As String)

        Dim scope As New ManagementScope("\\.\root\cimV2")
        Dim query As New WqlEventQuery("__InstanceCreationEvent", TimeSpan.FromSeconds(1), "TargetInstance isa ""Win32_Process""")

        Using watcher As New ManagementEventWatcher(scope, query)
            AddHandler watcher.EventArrived, AddressOf EventArrived
            watcher.Start()
            Console.WriteLine("Waiting for processes to start...")
            Console.ReadLine()
            watcher.Stop()
        End Using

    End Sub

    Private Shared Sub EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)

        Dim targetInstance As ManagementBaseObject = e.NewEvent("TargetInstance")
        Dim process As New ManagementObject("win32_process.handle=" + targetInstance("ProcessId").ToString())
        Dim output(1) As String
        process.InvokeMethod("GetOwner", output)
        Console.WriteLine("Process {0} started by {2}\{1}", targetInstance("Name"), output(0), output(1))

    End Sub

End Class

I am trying to use the following code to write out all processes started on a computer. My problem is that the EventArrived method is passed a EventArrivedEventArgs which has a NewEvent property of type ManagementBaseObject. This does not have a InvokeMethod method. Can I convert the NewEvent property to a ManagementObject some how, or do I need to requery and create a ManagementObject? The code below works great, but would like to not requery.

Imports System.Management

Public NotInheritable Class EntryPoint

    Public Shared Sub Main(ByVal args() As String)

        Dim scope As New ManagementScope("\\.\root\cimV2")
        Dim query As New WqlEventQuery("__InstanceCreationEvent", TimeSpan.FromSeconds(1), "TargetInstance isa ""Win32_Process""")

        Using watcher As New ManagementEventWatcher(scope, query)
            AddHandler watcher.EventArrived, AddressOf EventArrived
            watcher.Start()
            Console.WriteLine("Waiting for processes to start...")
            Console.ReadLine()
            watcher.Stop()
        End Using

    End Sub

    Private Shared Sub EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)

        Dim targetInstance As ManagementBaseObject = e.NewEvent("TargetInstance")
        Dim process As New ManagementObject("win32_process.handle=" + targetInstance("ProcessId").ToString())
        Dim output(1) As String
        process.InvokeMethod("GetOwner", output)
        Console.WriteLine("Process {0} started by {2}\{1}", targetInstance("Name"), output(0), output(1))

    End Sub

End Class

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

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

发布评论

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

评论(1

青瓷清茶倾城歌 2024-08-01 08:42:49

该对象是 ManagementObject 实例吗? 索引器可以将返回值作为 Base 传递,因为它是通用属性。 尝试这个:

Private Shared Sub EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
    Dim targetInstance As ManagementBaseObject = e.NewEvent("TargetInstance")

    If TypeOf targetInstance Is ManagementObject Then
        Dim mo as ManagementObject = DirectCast(targetInstance, ManagementObject)
        Dim output(1) as String

        mo.InvokeMethod("GetOwner", output)

        Console.WriteLine("Process {0} started by {2}\{1}", targetInstance("Name"), output(0), output(1))
    End If
End Sub

IS the object a ManagementObject instance? The indexer may pass the return value as a Base because it's a general-purpose property. Try this:

Private Shared Sub EventArrived(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
    Dim targetInstance As ManagementBaseObject = e.NewEvent("TargetInstance")

    If TypeOf targetInstance Is ManagementObject Then
        Dim mo as ManagementObject = DirectCast(targetInstance, ManagementObject)
        Dim output(1) as String

        mo.InvokeMethod("GetOwner", output)

        Console.WriteLine("Process {0} started by {2}\{1}", targetInstance("Name"), output(0), output(1))
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文