将 .Net ManagementBaseObject 转换为 ManagementObject
我试图使用以下代码来写出计算机上启动的所有进程。 我的问题是,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该对象是
ManagementObject
实例吗? 索引器可以将返回值作为Base
传递,因为它是通用属性。 尝试这个:IS the object a
ManagementObject
instance? The indexer may pass the return value as aBase
because it's a general-purpose property. Try this: