Solved

Programs Path

Posted on 2011-02-20
7
859 Views
Last Modified: 2012-05-11
Are they a way to get a programs file name and path from it's hWnd or Class name
0
Comment
Question by:PhilChapmanJr
  • 3
  • 2
  • 2
7 Comments
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 34939569
See App.Path function
0
 
LVL 32

Expert Comment

by:Robberbaron (robr)
ID: 34939570
See App.Path function
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 34939649
No I mean another application that you have the hWnd of.
Example:
If you know the hWnd of Notepad how can you get the exe name and path from it hWnd.

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34939741
Yes.. Many different ways but which operating system does your application target
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 34939777
It will be Windows but could be most any version.  I would think they might be a API call
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 34939843
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&

Private Type UNICODE_STRING
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)
  
  hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, 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

0
 
LVL 2

Author Closing Comment

by:PhilChapmanJr
ID: 34940169
Thanks
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question