Results 1 to 12 of 12

Thread: APP PATH of RUNNING PROGRAM???

  1. #1
    Guest
    I want to get the path of a running program. Is there a way to do this??

    Thanks,

    -Jordan

  2. #2
    Guest
    your program?

    Code:
    MsgBox App.Path

  3. #3
    Guest
    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?

  4. #4
    Guest
    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)

  5. #5
    New Member
    Join Date
    Jul 2000
    Posts
    11
    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.
    Still Learning

  6. #6
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    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.

  7. #7
    Guest
    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

  8. #8
    Guest
    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

  9. #9
    Guest
    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.

  10. #10
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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

  11. #11
    Guest
    HeSaidJoe, it works great, unless your on Win2k

  12. #12
    Guest
    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
  •  



Click Here to Expand Forum to Full Width