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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10226718/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:52:48  来源:igfitidea点击:

How to get the filename of the process that is currently runing in vb6

vbavb6

提问by user1153193

Is there a way to find the currently open file from an Excel or Word process etc.? I want to get the list of all running processes in Windows and which files they currently have open.

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

回答by Fionnuala

How about a list of running processes using VBA

如何使用 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

Modified from : http://www.windowsadminscripts.com/coding/networking/processes/

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

Perhaps a list of open windows might be more useful:

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

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

Use it like so:

像这样使用它:

ListWins "*.doc*"

This will list all Word windows with a title containing .doc

这将列出标题包含.doc 的所有 Word 窗口

回答by chris6523

Is there a reason, why you use vb6?

有什么原因,你为什么使用vb6?

Edit: I don't know, if that will help you, but here is a link, with some examples how to get a process list in VB6: http://wiki.robotz.com/index.php/Process_List_and_Locate_VB6

编辑:我不知道,如果这对您有帮助,但这里有一个链接,其中包含一些如何在 VB6 中获取进程列表的示例:http: //wiki.robotz.com/index.php/Process_List_and_Locate_VB6