Solved

Programs Path

Posted on 2011-02-20
7
853 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now