Results 1 to 2 of 2

Thread: Filename of the active program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    Yugoslavia
    Posts
    16

    Filename of the active program

    How to get the filename of the active program, if all I have is the handle (hWnd) of the active program?

  2. #2
    Matthew Gates
    Guest
    Try this:


    VB Code:
    1. 'Author: Sam Huggill
    2. 'Author's email: [url]http://www.programmerz.com/vb/[/url]
    3. 'Date Submitted: 2/6/1999
    4. 'Compatibility: VB 6,VB 5
    5.  
    6. 'Task: Returns the .exe name from the given handle.
    7.  
    8. 'Declarations
    9. Public Const TH32CS_SNAPPROCESS As Long = 2&
    10. Public Const MAX_PATH As Long = 260
    11.  
    12. Public Type PROCESSENTRY32
    13. dwSize As Long
    14. cntUsage As Long
    15. th32ProcessID As Long
    16. th32DefaultHeapID As Long
    17. th32ModuleID As Long
    18. cntThreads As Long
    19. th32ParentProcessID As Long
    20. pcPriClassBase As Long
    21. dwflags As Long
    22. szexeFile As String * MAX_PATH
    23. End Type
    24.  
    25. Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd
    26. As Long, lpdwProcessId As Long) As Long
    27.  
    28. Public Declare Function CreateToolhelpSnapshot Lib "Kernel32" Alias
    29. "CreateToolhelp32Snapshot" (ByVal lFlgas As Long, ByVal lProcessID As
    30. Long) As Long
    31.  
    32. Public Declare Function ProcessFirst Lib "Kernel32" Alias "Process32First"
    33. (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    34.  
    35. Public Declare Function ProcessNext Lib "Kernel32" Alias "Process32Next"
    36. (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    37.  
    38. Public Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
    39.  
    40. Public Function GetExeFromHandle(hWnd As Long) As String
    41. Dim threadID As Long, processID As Long, hSnapshot As Long
    42. Dim uProcess As PROCESSENTRY32, rProcessFound As Long
    43. Dim i As Integer, szExename As String
    44. ' Get ID for window thread
    45. threadID = GetWindowThreadProcessId(hWnd, processID)
    46. ' Check if valid
    47. If threadID = 0 Or processID = 0 Then Exit Function
    48. ' Create snapshot of current processes
    49. hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    50. ' Check if snapshot is valid
    51. If hSnapshot = -1 Then Exit Function
    52. 'Initialize uProcess with correct size
    53. uProcess.dwSize = Len(uProcess)
    54. 'Start looping through processes
    55. rProcessFound = ProcessFirst(hSnapshot, uProcess)
    56. Do While rProcessFound
    57. If uProcess.th32ProcessID = processID Then
    58. 'Found it, now get name of exefile
    59. i = InStr(1, uProcess.szexeFile, Chr(0))
    60. If i > 0 Then szExename = Left$(uProcess.szexeFile, i - 1)
    61. Exit Do
    62. Else
    63. 'Wrong ID, so continue looping
    64. rProcessFound = ProcessNext(hSnapshot, uProcess)
    65. End If
    66. Loop
    67. Call CloseHandle(hSnapshot)
    68. GetExeFromHandle = szExename
    69. End Function
    70.  
    71.  
    72. Private Sub Command1_Click()
    73.     MsgBox GetExeFromHandle(Me.hWnd)
    74. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width