Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Sub Main()
Dim strFileName As String
Dim hWnd As Long, pId As Long, hProcess As Long
strFileName = String(128, Chr(0))
hWnd = FindWindow(vbNullString, "无标题 - 记事本") '以记事本为例
GetWindowThreadProcessId hWnd, pId
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pId)
strFileName = Left(strFileName, GetModuleFileNameExA(hProcess, 0, strFileName, Len(strFileName)))
CloseHandle hProcess
MsgBox strFileName
End Sub
评论