Solved

Programs Path

Posted on 2011-02-20
7
868 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

726 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