The following code is a function to return the Executable name from any given Window Handle.

In a Standard Module:
VB Code:
  1. '------------------------------------------------------------------
  2. ' GetHwndEXE()
  3. '
  4. ' Written by Aaron Young, 08/10/2000
  5. '
  6. ' Extract the EXE Name and Path from a Given Window Handle
  7. '
  8. '------------------------------------------------------------------
  9. '
  10. 'In a Module...
  11. Private Type PROCESSENTRY32
  12.     dwSize As Long
  13.     cntUsage As Long
  14.     th32ProcessID As Long
  15.     th32DefaultHeapID As Long
  16.     th32ModuleID As Long
  17.     cntThreads As Long
  18.     th32ParentProcessID As Long
  19.     pcPriClassBase As Long
  20.     dwFlags As Long
  21.     szExeFile As String * 260
  22. End Type
  23.  
  24. Private Type OSVERSIONINFO
  25.     dwOSVersionInfoSize As Long
  26.     dwMajorVersion As Long
  27.     dwMinorVersion As Long
  28.     dwBuildNumber As Long
  29.     dwPlatformId As Long
  30.     szCSDVersion As String * 128
  31. End Type
  32.  
  33. Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
  34. Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
  35. Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
  36. Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
  37. 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
  38. 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
  39. Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
  40. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
  41. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
  42.  
  43. Private Const VER_PLATFORM_WIN32_WINDOWS = 1
  44. Private Const PROCESS_QUERY_INFORMATION = 1024
  45. Private Const PROCESS_VM_READ = 16
  46. Private Const TH32CS_SNAPPROCESS = &H2
  47.  
  48. Private Function CheckVersion() As Long
  49.     Dim tOS As OSVERSIONINFO
  50.     tOS.dwOSVersionInfoSize = Len(tOS)
  51.     Call GetVersionEx(tOS)
  52.     CheckVersion = tOS.dwPlatformId
  53. End Function
  54.  
  55. Public Function GetHwndEXE(ByVal hWnd As Long) As String
  56.     Dim lProcessID As Long, lThread As Long
  57.     Dim lProcessHandle As Long
  58.     Dim sName As String, lModule As Long
  59.     Dim bMore As Boolean, tPROCESS As PROCESSENTRY32
  60.     Dim lSnapShot As Long
  61.    
  62.     lThread = GetWindowThreadProcessId(hWnd, lProcessID)
  63.    
  64.     If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
  65.         'Windows 9x
  66.         'Create a SnapShot of the Currently Running Processes
  67.         lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  68.         If lSnapShot < 0 Then Exit Function
  69.        
  70.         tPROCESS.dwSize = Len(tPROCESS)
  71.        
  72.         'Enumerate those processes until we find a match
  73.         bMore = Process32First(lSnapShot, tPROCESS)
  74.         While bMore And tPROCESS.th32ProcessID <> lProcessID
  75.             bMore = Process32Next(lSnapShot, tPROCESS)
  76.         Wend
  77.        
  78.         'If a match was found, get the EXE Path and Filename
  79.         If tPROCESS.th32ProcessID = lProcessID Then
  80.             sName = Left$(tPROCESS.szExeFile, InStr(tPROCESS.szExeFile, Chr(0)) - 1)
  81.             GetHwndEXE = sName
  82.         End If
  83.    
  84.     Else
  85.         'Win NT
  86.         'Create an Instance of the Process
  87.         lProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0&, lProcessID)
  88.        
  89.         'If the Process was successfully created, get the EXE
  90.         If lProcessHandle Then
  91.             'Just get the First Module, all we need is the Handle to get the Filename..
  92.             If EnumProcessModules(lProcessHandle, lModule, 4, 0&) Then
  93.                 sName = Space(260)
  94.                 Call GetModuleFileNameExA(lProcessHandle, lModule, sName, Len(sName))
  95.                 GetHwndEXE = sName
  96.             End If
  97.             'Close the Process Handle
  98.             Call CloseHandle(lProcessHandle)
  99.         End If
  100.     End If
  101.  
  102. End Function
Example Usage:
VB Code:
  1. sEXE = GetHwndEXE(Me.hWnd)