I want to get the path of a running program. Is there a way to do this??
Thanks,
-Jordan
Printable View
I want to get the path of a running program. Is there a way to do this??
Thanks,
-Jordan
your program?
Code:MsgBox App.Path
Not mine... another one. There must be a way to get the handle of a window or something and get the path of it.
Any Ideas?
This code will return the path of a running program from the given handle.
Code:Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH As Long = 260
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwflags As Long
szexeFile As String * MAX_PATH
End Type
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd
As Long, lpdwProcessId As Long) As Long
Public Declare Function CreateToolhelpSnapshot Lib "Kernel32" Alias
"CreateToolhelp32Snapshot" (ByVal lFlgas As Long, ByVal lProcessID As
Long) As Long
Public Declare Function ProcessFirst Lib "Kernel32" Alias "Process32First"
(ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function ProcessNext Lib "Kernel32" Alias "Process32Next"
(ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
Public Function GetExeFromHandle(hWnd As Long) As String
Dim threadID As Long, processID As Long, hSnapshot As Long
Dim uProcess As PROCESSENTRY32, rProcessFound As Long
Dim i As Integer, szExename As String
' Get ID for window thread
threadID = GetWindowThreadProcessId(hWnd, processID)
' Check if valid
If threadID = 0 Or processID = 0 Then Exit Function
' Create snapshot of current processes
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
' Check if snapshot is valid
If hSnapshot = -1 Then Exit Function
'Initialize uProcess with correct size
uProcess.dwSize = Len(uProcess)
'Start looping through processes
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
If uProcess.th32ProcessID = processID Then
'Found it, now get name of exefile
i = InStr(1, uProcess.szexeFile, Chr(0))
If i > 0 Then szExename = Left$(uProcess.szexeFile, i - 1)
Exit Do
Else
'Wrong ID, so continue looping
rProcessFound = ProcessNext(hSnapshot, uProcess)
End If
Loop
Call CloseHandle(hSnapshot)
GetExeFromHandle = szExename
End Function
Usage:
MsgBox GetExeFromHandle(hWnd)
A very easy way to get the app path is to
add a DirListBox to your form and add this code:
Dim AppPath as string
'put this in forms load event
Dir1.Visible=false
AppPath=Dir1.Path
That's it,
AppPath holds the data you want.
Hehe Johnewad, he meant from another prog, not his own.
and hey, ever heard of App.Path (think so, Dennis mentioned it before in this post)?
that will be much better, no OCX. no hassle.
Thanks Matthew thats 98% of what I've been looking for. The only problem with the above code is that it only returns the EXE name, not the entire path. Is there a way to get this?
Thanks Again,
Jordan
For me, that code returns the whole exe path.
To find the handle of a window, use the FindWindow Api function.
Here is how to use it:Code:Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Code:'Find the Calculator
handl = FindWindow(vbNullString, "Calculator")
If handl <> 0 Then
'If found then
MsgBox GetExeFromHandle("" & handl & "")
Else
'If not found then
MsgBox "Cannot extract exe's file path!", vbCritical
End If
Sorry about that... It only returns the exe name using Windows 2000, because when I switched over to Win98 it worked fine :) Good thing most people still use this crash-prone OS over anything else or I would be stuck with a lot of unhappy Win2k users :)
Anyway thanks a bunch for the code.
Matthew's code is sound..it returns the full path.
Another piece of good code for my DB...thanks..
[Edited by HeSaidJoe on 08-27-2000 at 04:39 PM]
HeSaidJoe, it works great, unless your on Win2k ;)
Sorry Jordan, Windows 98 and Windows 2000 are very different.
Windows 2000 and Windows NT are the same.
So the code that will work on Windows NT will probably work on Windows 2000, but much of the code on Windows 98 will not work on Windows 2000.
And your welcome HeSaidJoe.