Tác giả: Sưu tầm
Mô tả: Lấy đường dẫn file exe từ tiêu đề của cửa sổ để biết được chương trình đang chạy có đường dẫn từ đâu.
Code: Select all
Option ExplicitConst PROCESS_QUERY_INFORMATION = 1024Const PROCESS_VM_READ = 16Const MAX_PATH As Integer = 260 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As LongPrivate Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As LongPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As LongPrivate Declare Function GetModuleFileNameEx Lib "PSAPI.DLL" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long Private Sub Command1_Click() Dim Ret As String Dim whwnd As Long Dim hMod As Long Dim theProcess As Long Dim lngCBSize2 As Long Dim lngModules(1 To 1) As Long Dim lngReturn As Long Dim strModuleName As String Dim lngSize As Long Dim strProcessName As String 'Lay tieu de cua so Ret = InputBox("Nhap tieu de cua so vao day:") If Ret = "" Then Exit Sub whwnd = FindWindow(vbNullString, Ret) If whwnd 0 Then hMod = GetWindowThreadProcessId(whwnd, theProcess) If hMod 0 Then 'Get a handle to the Process and Open Dim lngHwndProcess As Long lngHwndProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, theProcess) If lngHwndProcess 0 Then 'Get an array of the module handles for the specified process lngReturn = EnumProcessModules(lngHwndProcess, lngModules(1), 1, lngCBSize2) 'If the Module Array is retrieved, Get the ModuleFileName If lngReturn 0 Then 'Buffer with spaces first to allocate memory for byte array strModuleName = Space(MAX_PATH) 'Must be set prior to calling API lngSize = 500 'Get Process Name lngReturn = GetModuleFileNameEx(lngHwndProcess, lngModules(1), strModuleName, lngSize) If lngReturn > 0 Then 'Remove trailing spaces strProcessName = Left(strModuleName, lngReturn) End If End If End If 'Close the handle to this process lngReturn = CloseHandle(lngHwndProcess) End If 'process finded End If 'window finded If Len(strProcessName) > 0 Then 'Hien thi ket qua MsgBox "Found: " & strProcessName End IfEnd Sub