vb根据窗口标题获取应用程序完整路径
有些窗口不能获取
比如nx4 的窗口 能获得完整路径
但是nx8 的窗口 不能获得完整路径
【建使 根据进程 来获得程序完整路径 比较妥当 都能获得】
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.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID 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
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Function GetProcessPathByProcessID(pid As Long) As String
On Error GoTo ErrLine
Dim cbNeeded As Long
Dim szBuf(1 To 250) As Long
Dim Ret As Long
Dim szPathName As String
Dim nSize As Long
Dim hProcess As Long
hProcess = OpenProcess(&H400 Or &H10, 0, pid)
If hProcess <> 0 Then
Ret = EnumProcessModules(hProcess, szBuf(1), 250, cbNeeded)
If Ret <> 0 Then
szPathName = Space(260)
nSize = 500
Ret = GetModuleFileNameExA(hProcess, szBuf(1), szPathName, nSize)
GetProcessPathByProcessID = Left(szPathName, Ret)
End If
End If
Ret = CloseHandle(hProcess)
If GetProcessPathByProcessID = "" Then
GetProcessPathByProcessID = "can't not catch"
End If
ErrLine:
End Function
Private Sub Form_Load()
'根据窗口标题获取应用程序完整路径
On Error Resume Next
Dim notepad_hwnd As Long, i As Long, pid As Long
Dim notepad_path As String
notepad_hwnd& = FindWindow(vbNullString, "1.txt - 记事本") '获得窗口并句
i = GetWindowThreadProcessId(notepad_hwnd, pid) '获得pid
notepad_path = GetProcessPathByProcessID(pid) '获得路径
If notepad_path <> "can't not catch" Then
MsgBox notepad_path
Else
MsgBox "无发获得 应用程序完整路径"
End If
End Sub
_____________________________________________________________________________ _
中磊UG二次开发教程 梅雷著 qq1821117007
学UG就上UG网 http://www.9sug.com/ |