VB6 - 如何列出与用户名相关的PID

时间:2016-07-04 05:08:33

标签: list process vb6

我还是VB6和VB Studio的新手。

我想获得所有进程ID的列表以及与这些ID相关联的用户名

我没有使用表单,因为不需要GUI,但出于测试目的,我想将列表输出到msgbox或记事本文件。

这是我到目前为止所做的,但它不能在VB Studio中运行。它确实作为VBS工作,但不会以错误“无效的外部过程”

保存为exe
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strList

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
strList = strList & vbCr & _
objProcess.Name
Next

WSCript.Echo strList
WScript.Quit

感谢任何帮助

1 个答案:

答案 0 :(得分:1)

将模块添加到项目中并使用以下代码。它将数据写入临时文件夹中的文本文件。从项目>属性菜单中确保"启动对象"是Sub Main

    Option Explicit

    Dim objWMIService As Object
    Dim objProcess As Object
    Dim colProcess As Object
    Dim strComputer As String
    Dim strUserName
    Dim strUserDomain

    Sub Main()

        On Error GoTo eh

        strComputer = "."

        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & strComputer & "\root\cimv2")

        Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process")

        Open "c:\temp\test.txt" For Output As #1
        For Each objProcess In colProcess
            objProcess.GetOwner strUserName, strUserDomain
            Print #1, objProcess.Name & vbTab & strUserName
        Next
        Close #1

        MsgBox ("Done")

        Exit Sub

    eh:
        MsgBox (Error$)

    End Sub