|
-
Aug 25th, 2000, 05:45 PM
#1
I want to get the path of a running program. Is there a way to do this??
Thanks,
-Jordan
-
Aug 25th, 2000, 06:19 PM
#2
-
Aug 25th, 2000, 06:37 PM
#3
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?
-
Aug 26th, 2000, 05:05 PM
#4
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)
-
Aug 26th, 2000, 09:17 PM
#5
New Member
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.
-
Aug 27th, 2000, 07:53 AM
#6
Frenzied Member
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.
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Aug 27th, 2000, 01:46 PM
#7
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
-
Aug 27th, 2000, 02:23 PM
#8
For me, that code returns the whole exe path.
To find the handle of a window, use the FindWindow Api function.
Code:
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Here is how to use it:
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
-
Aug 27th, 2000, 03:16 PM
#9
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.
-
Aug 27th, 2000, 03:35 PM
#10
_______
<?>
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]
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 27th, 2000, 04:29 PM
#11
HeSaidJoe, it works great, unless your on Win2k
-
Aug 27th, 2000, 06:58 PM
#12
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|