作者:绿色风
分类:
时间:2022-08-17
浏览:100
楼主 xmyjk |
Q:如何通过窗口标题获得窗口所属应用程序的路径?
A:- 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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
- Private Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
- Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
- Sub 获取应用程序路径(name As String)
- Dim lpid As Long, stxt As String, Hnd As Long, hwnd As Long
-
- hwnd = FindWindow(vbNullString, name) '通过名称获取窗口句柄
- GetWindowThreadProcessId hwnd, lpid '获取窗口的进程ID
- stxt = Space(255) '缓存文本
- Hnd = OpenProcess(PROCESS_ALL_ACCESS, 0, lpid) '通过ID访问进程
- GetModuleFileNameEx Hnd, 0, stxt, 255& '获取进程文件路径
- CloseHandle Hnd '关闭访问
- Debug.Print Trim(stxt) '输出路径名
- End Sub
使用方法,类似,获取应用程序路径("1.txt - 记事本") 窗口路径.zip |
2楼 い卋玑┾宝珼 |
向妃妃学习 |
3楼 老糊涂 |
学习 |
免责声明
有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素,
经与ExcelTip.Net站长Apolloh商议并征得其同意,
现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示,
供有需要的人士查询使用,也慰缅曾经的论坛时代。
所示各个帖子的原作者如对版权有异议,
可与本人沟通提出,或于本站点留言,我们会尽快处理。
在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一