【问题标题】:Get Process Path and Name by ProcessID - Inconsistent Results (VB6, VBA)通过 ProcessID 获取进程路径和名称 - 不一致的结果(VB6、VBA)
【发布时间】:2023-03-17 18:00:01
【问题描述】:

我正在使用下面的代码来获取给定进程的文件路径和名称。我只是将 ProcessID 传递给函数 ExePathFromProcID ,它应该返回完整路径。它将硬盘驱动器枚举为设备而不是使用驱动器号,但这不是我的抱怨。查看我的代码,然后在下面查看我的投诉。

Public Declare Function OpenProcess Lib "kernel32" ( _
    ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long

Public Declare Function GetProcessImageFileName Lib "psapi.dll" Alias "GetProcessImageFileNameA" _
    (ByVal hProcess As Long, _
    ByVal lpImageFileName As String, _
    ByVal nSize As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" ( _
    ByVal hObject As Long) As Long     

Private Function ExePathFromProcID(idProc As Long) As String
    Const MAX_PATH = 260
    Const PROCESS_QUERY_INFORMATION = &H400
    Const PROCESS_VM_READ = &H10

    Dim sBuf As String
    Dim sChar As Long, l As Long, hProcess As Long
    sBuf = String$(MAX_PATH, Chr$(0))
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, idProc)
    If hProcess Then
        sChar = GetProcessImageFileName(hProcess, sBuf, MAX_PATH)
        If sChar Then
            sBuf = Left$(sBuf, sChar)
            ExePathFromProcID = sBuf
            Debug.Print sBuf
        End If
        CloseHandle hProcess
    End If
End Function

我的抱怨是,只要首先检索到较长的路径,当返回较短的路径时,较长的流程路径的部分仍然会显示。这是一个例子:

第一次调用(正确结果):
\Device\HarddiskVolume2\Program Files\Portable Apps\Notepad++ Portable\App\Notepad++\notepad++.exe

第二次通话(意外结果):
\Device\HarddiskVolume2\Program Files\Microsoft Office\Office12\MSACCESS.EXE tepad++\notepad++.exe

注意到第二次调用结果末尾的“tepad++\notepad++.exe”了吗?不要被它落在第二行的事实所迷惑。它都是同一个字符串的一部分,并且在第二次调用这个函数时全部返回。

知道为什么我的函数会返回这个吗?看起来这是一个全局字符串变量没有被清除的问题,但我使用的代码几乎和我发布的一样。没有全局变量。

【问题讨论】:

  • 你在什么环境下测试?您是否遍历了返回字符串以检查将新信息与剩余旧信息分开的空字符?

标签: ms-access winapi vb6 vba


【解决方案1】:

如果您查看返回的缓冲区,正确的路径实际上是由 null 终止的,例如,如果我传递了我的 ultramon.exe 的 PID:

\  D  e  v  i  c  e  \  H  a  r  d  d  i  s  k  V  o  l  u  m  e  1  \  P  r  o  g  r  a  m     F  i  l  e  s  \  U  l  t  r  a  M  o  n  \  U  l  t  r  a  M  o  n  .  e  x  e     x  .  e  x  e     3  9     3  8     5  C     5  6     4  2     3  6     2  E     4  5     5  8     4  5     0     6  1     7  2     4  E     6  F     7  4     6  9     6  
5C 44 65 76 69 63 65 5C 48 61 72 64 64 69 73 6B 56 6F 6C 75 6D 65 31 5C 50 72 6F 67 72 61 6D 20 46 69 6C 65 73 5C 55 6C 74 72 61 4D 6F 6E 5C 55 6C 74 72 61 4D 6F 6E 2E 65 78 65 0 78 2E 65 78 65 0 33 39 20 33 38 20 35 43 20 35 36 20 34 32 20 33 36 20 32 45 20 34 35 20 35 38 20 34 35 20 30 20 36 31 20 37 32 20 34 45 20 36 46 20 37 34 20 36 39 20 36 

文档没有明确说返回值是路径的长度,只是复制缓冲区的长度,这似乎不是一回事; 如果函数成功,返回值指定复制到缓冲区的字符串长度。

这意味着您需要:

sBuf = Left$(sBuf, InStr(1, sBuf, ChrW$(0)))

【讨论】:

  • 感谢您的解决方案亚历克斯。虽然其他答案似乎与真正的问题有关,但我很感激你给了我一些解决问题的工作代码。
【解决方案2】:

我认为用 QueryFullProcessImageName 替换 GetProcessImageFileName 将解决第一个问题。

第二个问题只是 GetProcessImageFileName 在另一个之上写入一个以 null 结尾的字符串。我不是 VB 专家,但您可以在将缓冲区传递给 GetProcessImageFileName 之前清除缓冲区,或者在缓冲区返回后设置正确的长度。

【讨论】:

    【解决方案3】:

    关于返回值是什么,文档上有一条评论: http://msdn.microsoft.com/en-us/library/ms683217(v=vs.85).aspx#3

    即。返回的值不是路径的长度。所以你应该使用某种 strlen 函数。

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 2011-03-26
      • 2019-01-07
      • 2017-04-20
      • 1970-01-01
      相关资源
      最近更新 更多