• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 878
  • Last Modified:

Programs Path

Are they a way to get a programs file name and path from it's hWnd or Class name
Phil Chapman
Phil Chapman
  • 3
  • 2
  • 2
1 Solution
Robberbaron (robr)Commented:
See App.Path function
Robberbaron (robr)Commented:
See App.Path function
Phil ChapmanAuthor Commented:
No I mean another application that you have the hWnd of.
If you know the hWnd of Notepad how can you get the exe name and path from it hWnd.

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

Yes.. Many different ways but which operating system does your application target
Phil ChapmanAuthor Commented:
It will be Windows but could be most any version.  I would think they might be a API call
The reason I asked is because there are some that don't work correctly on certain operating systems. If you were using Vista or later QueryFullProcessImage() would be the best option.
Option Explicit

Private Const ProcessImageFileName As Long = 27
Private Const STATUS_INFO_LENGTH_MISMATCH As Long = &HC0000004
Private Const PROCESS_QUERY_INFORMATION As Long = &H400&
Private Const PROCESS_VM_READ As Long = &H10&
Private Const HEAP_ZERO_MEMORY As Long = &H8&

Length As Integer
MaximumLength As Integer
Buffer As Long
End Type

Private Declare Function GetProcessHeap Lib "Kernel32.dll" () As Long
Private Declare Function HeapAlloc Lib "Kernel32.dll" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function HeapFree Lib "Kernel32.dll" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal lpMem 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 CloseHandle Lib "Kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "User32.dll" (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
Private Declare Function NtQueryInformationProcess Lib "Ntdll.dll" (ByVal ProcessHandle As Long, ByVal ProcessInformationClass As Long, ByVal ProcessInformation As Long, ByVal ProcessInformationLength As Long, ByRef ReturnLength As Long) As Long
Private Declare Sub RtlMoveMemory Lib "Kernel32.dll" (ByVal lpDest As Long, ByVal lpSource As Long, ByVal cbCopy As Long)

Public Function GetProcessNameByHandle(ByVal hWnd As Long) As String

  Dim uni             As UNICODE_STRING
  Dim BufferFile()    As Byte   '// file name buffer
  Dim Buffer          As Long   '// heap buffer
  Dim hProcess        As Long   '// process handle
  Dim cbLength        As Long   '// return length
  Dim tid             As Long   '// threadId
  Dim pid             As Long   '// processId
  GetProcessNameByHandle = vbNullString
  tid = GetWindowThreadProcessId(hWnd, pid)
  If hProcess = 0 Then
    Debug.Print "OpenProcess failed"; Err.LastDllError
    Exit Function
  End If
  If NtQueryInformationProcess(hProcess, ProcessImageFileName, VarPtr(uni), 8, cbLength) = STATUS_INFO_LENGTH_MISMATCH Then

    Buffer = HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, cbLength)
    If NtQueryInformationProcess(hProcess, ProcessImageFileName, Buffer, cbLength, cbLength) = 0 Then
      RtlMoveMemory VarPtr(uni), ByVal Buffer, LenB(uni)
      ReDim BufferFile(uni.Length) As Byte
      RtlMoveMemory VarPtr(BufferFile(0)), ByVal uni.Buffer, uni.Length
      GetProcessNameByHandle = BufferFile
    End If
    If (Buffer <> 0) Then
        HeapFree GetProcessHeap, 0, Buffer
        Buffer = 0
    End If
  End If
  CloseHandle hProcess
End Function

Open in new window

Debug.Print GetProcessNameByHandle(&H1170878)

Open in new window

Phil ChapmanAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now