VBA 获取正在运行的进程的程序名称和任务 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26277214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Getting program names and task ID of running processes
提问by Qbik
How to get program names and task IDs of running processes. shell()
returns task ID of initiated process. Similar, I would like to get the task ID and name of processes which are already running and is not created by macro. I've found code which returns programs names but its output lacks task IDs information :
如何获取正在运行的进程的程序名称和任务 ID。shell()
返回已启动进程的任务 ID。类似地,我想获取已经在运行且不是由宏创建的进程的任务 ID 和名称。我找到了返回程序名称但其输出缺少任务 ID 信息的代码:
http://www.vbaexpress.com/forum/archive/index.php/t-36677.html
http://www.vbaexpress.com/forum/archive/index.php/t-36677.html
Sub Test_AllRunningApps()
Dim apps() As Variant
apps() = AllRunningApps
Range("A1").Resize(UBound(apps), 1).Value2 = WorksheetFunction.Transpose(apps)
Range("A:A").Columns.AutoFit
End Sub
'Similar to: http://msdn.microsoft.com/en-us/library/aa393618%28VS.85%29.aspx
Public Function AllRunningApps() As Variant
Dim strComputer As String
Dim objServices As Object, objProcessSet As Object, Process As Object
Dim oDic As Object, a() As Variant
Set oDic = CreateObject("Scripting.Dictionary")
strComputer = "."
Set objServices = GetObject("winmgmts:\" _
& strComputer & "\root\CIMV2")
Set objProcessSet = objServices.ExecQuery _
("SELECT Name FROM Win32_Process", , 48)
For Each Process In objProcessSet
If Not oDic.exists(Process.Name) Then oDic.Add Process.Name, Process.Name
Next
a() = oDic.keys
Set objProcessSet = Nothing
Set oDic = Nothing
AllRunningApps = a()
End Function
采纳答案by JNevill
You can change the SQL to read Select Name, ProcessID FROM Win32_Process
您可以将 SQL 更改为读取 Select Name, ProcessID FROM Win32_Process
Then in your For Loop, to get the name use Process.Properties_("Name").value
and Process.Properties_("ProcessID").value
where needed.
然后在您的 For 循环中,获取名称的使用Process.Properties_("Name").value
和Process.Properties_("ProcessID").value
需要的位置。