[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Programs Path

Posted on 2011-02-20
7
Medium Priority
?
875 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 2000 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

Technology Partners: 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!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

656 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