如何获取当前在vb6中运行的进程的文件名

时间:2012-04-19 11:06:48

标签: vba vb6

有没有办法从Excel或Word流程等中找到当前打开的文件?我想获取Windows中所有正在运行的进程的列表以及它们当前打开的文件。

2 个答案:

答案 0 :(得分:3)

使用VBA运行进程列表如何

Function getProcessInfo()
''On Error Resume Next
Dim objProcess, process, strNameOfUser
ComputerName = "."
Set objProcess = GetObject("winmgmts:{impersonationLevel=impersonate}\\" _
      & ComputerName & "\root\cimv2").ExecQuery("Select * From Win32_Process")
For Each process In objProcess
    If process.Name <> "System Idle Process" And process.Name <> "System" Then
        ''Debug.Print process.Name
        Debug.Print process.Name & "," & process.executablepath _
            & "," & process.Priority & "," & process.sessionid _
            & "," & strNameOfUser & "," & process.handlecount _
            & "," & process.ThreadCount
    End If
Next

Set objProcess = Nothing
End Function

修改自:http://www.windowsadminscripts.com/coding/networking/processes/

也许打开的窗口列表可能更有用:

Private Const GW_HWNDNEXT = 2
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Sub ListWins(Optional Title = "*", Optional Class = "*")
    Dim hWndThis As Long
    hWndThis = FindWindow(vbNullString, vbNullString)
    While hWndThis
        Dim sTitle As String, sClass As String
        sTitle = Space$(255)
        sTitle = Left$(sTitle, GetWindowText(hWndThis, sTitle, Len(sTitle)))
        sClass = Space$(255)
        sClass = Left$(sClass, GetClassName(hWndThis, sClass, Len(sClass)))
        If sTitle Like Title And sClass Like Class Then
            Debug.Print sTitle, sClass
        End If
        hWndThis = GetWindow(hWndThis, GW_HWNDNEXT)
    Wend
End Sub

像这样使用它:

ListWins "*.doc*"

这将列出所有包含 .doc

标题的Word窗口

答案 1 :(得分:1)

有没有理由,为什么要使用vb6?

编辑: 我不知道,如果这对你有所帮助,但这里有一个链接,有一些例子说明如何在VB6中获取进程列表:http://wiki.robotz.com/index.php/Process_List_and_Locate_VB6