2个回答
展开全部
正着做有点难,那么反着做。枚举窗口,获得pid,得到进程名,判断,ok
下面的代码是获得所有窗口名称的,因为一个程序不止有一个顶层窗口,所以你可能还需要根据窗口属性判断以下是不是需要的。对于一些程序不允许打开进程或者获取路径的,就没有办法了,在这里我直接忽略。在我这wps就获取不到路径。另外,GetProcessImageFileName得到的是系统内部的路径,不是像c:\a.txt这样的,是\Device\HarddiskVolume1\a.txt。不过不影响获得文件名
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const GW_HWNDNEXT = 2
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Const PROCESS_VM_READ = &H10
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetProcessImageFileName Lib "psapi" Alias "GetProcessImageFileNameA" (ByVal hProcess As Long, ByVal lpImageFileName As String, ByVal nSize 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
Private Sub Form_Load()
Dim hWindow As Long
hWindow = FindWindow(vbNullString, vbNullString)
Dim AllWindowCaption As String
Do Until hWindow = 0
Dim pid As Long
GetWindowThreadProcessId hWindow, pid
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION, 0, pid)
If hProcess Then
Dim FileName As String
FileName = String(255, 0)
FileName = LeftB(FileName, GetProcessImageFileName(hProcess, FileName, 255) + 1)
Dim SA() As String
SA() = Split(FileName, "\")
If UBound(SA()) > -1 Then
If StrComp(SA(UBound(SA())), "工程1.exe", vbTextCompare) = 0 Then
Dim WindowCaption As String
WindowCaption = String(255, 0)
WindowCaption = Left(WindowCaption, GetWindowText(hWindow, WindowCaption, 255))
AllWindowCaption = AllWindowCaption & WindowCaption & vbCrLf
End If
End If
End If
CloseHandle hProcess
hWindow = GetWindow(hWindow, GW_HWNDNEXT)
Loop
MsgBox AllWindowCaption
End
End Sub
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询