QueryFullProcessImageName总是返回0,无法检索进程路径,下面的代码,需要你的帮助,谢谢。
Public Declare Function QueryFullProcessImageName Lib "Kernel32.dll" Alias "QueryFullProcessImageNameA" (hProcess As Long, ByVal dwFlags As Long, ByVal lpExeName As String, lpdwSize As Long) As Long
Private Function GetProcessImageName(ByVal lProcessID As Long) As String
Dim hProcess As Long
Dim sBuf As String
Dim sChar As Long
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION Or PROCESS_QUERY_INFORMATION, 0, lProcessID)
If hProcess Then
sBuf = String$(MAX_PATH, Chr$(0))
QueryFullProcessImageName hProcess, 0, sBuf, MAX_PATH
sBuf = Left$(sBuf, sChar)
End If
GetProcessImageName = sBuf
CloseHandle hProcess
End Function
答案 0 :(得分:0)
如果您使用GetLastError功能,您将能够获得有关其返回0的原因的更多信息。
答案 1 :(得分:0)
代码中有几个问题。
首先,sChar从未初始化。
此外,QueryFullProcessImageName的第一个参数是“In”参数,因此应该传递ByVal。这可能是无效句柄错误6的原因。
这个线程有一些有用的信息,关于如何在调用QueryFullProcessImageName时获取长度:http://www.vbforums.com/showthread.php?805537-RESOLVED-Excel-Crash-when-call-API-function-QueryFullProcessImageName-in-excel-VBA-of-win7。
最后,根据https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880(v=vs.85).aspx,“具有PROCESS_QUERY_INFORMATION访问权限的句柄会自动被授予PROCESS_QUERY_LIMITED_INFORMATION”,因此请删除不需要的访问权限。
有了这些信息,请尝试这样的事情(未经测试):
Public Declare Function QueryFullProcessImageName Lib "Kernel32.dll" Alias "QueryFullProcessImageNameA" _
(ByVal hProcess As Long, ByVal dwFlags As Long, ByVal lpExeName As String, ByRef lpdwSize As Long) As Long
Private Function GetProcessImageName(ByVal lProcessID As Long) As String
Dim hProcess As Long
Dim sBuf As String
Dim sChar As Long
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, lProcessID)
If hProcess Then
sBuf = String$(MAX_PATH, Chr$(0))
sChar = MAX_PATH
QueryFullProcessImageName hProcess, 0, sBuf, sChar
sBuf = Left$(sBuf, sChar)
End If
GetProcessImageName = sBuf
CloseHandle hProcess
End Function