gpt4 book ai didi

VBA获取正在运行的进程的程序名称和任务ID

转载 作者:行者123 更新时间:2023-12-01 18:58:08 27 4
gpt4 key购买 nike

如何获取正在运行的进程的程序名称和任务ID。 shell() 返回启动进程的任务 ID。类似地,我想获取已经运行且不是由宏创建的进程的任务 ID 和名称。我发现代码返回程序名称,但其输出缺少任务 ID 信息:

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

最佳答案

您可以将 SQL 更改为读取Select Name, ProcessID FROM Win32_Process

然后在 For 循环中,要获取名称,请使用 Process.Properties_("Name").valueProcess.Properties_("ProcessID").value 其中需要。

关于VBA获取正在运行的进程的程序名称和任务ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26277214/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com