Results 1 to 4 of 4

Thread: Getting the Path a program was launched from

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    8

    Getting the Path a program was launched from

    I've been able to get the hwnd for the active window. I have also been able to get what the exe is of the program. Both of these I found out how to do from these forums.

    One thing I haven't found is if it is possible to find the path a program was launched from if you have the hwnd?

    Thanks for any help, and soz in advance if this has been answered before, but I couldn't fin dit

  2. #2
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    If you used something like GetWindowThreadProcessID() to get the PID of the process that made the window, and then get info about the exe that way, the path should be along with the exe name itself. I answered a question a while back for someone about this, and one of the things that eventually came out of it was the full path and name of the exe that the process is running, so you may want to look at it.

    http://www.vbforums.com/showthread.p...hreadid=198867 (go about halfway down and take a look at GetEXEName())

    If that isn't the path you're talking about, just ignore these babblings. I think you can get the actual command line an exe was launched with in some manner though, assuming the path in question was a parameter. If it was from the "Launch in:" part of a shortcut's properties, I'm not sure how to get that, or even if you can.
    Last edited by Kaverin; Oct 2nd, 2002 at 10:34 PM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    8
    Thanks for the reply, one of my mates got it going.

    This is the code from the module he made if anyone is interested

    Code:
    Public Const TH32CS_SNAPMODULE As Long = 8&
    Public Const MAX_PATH As Long = 260
    
    Public Type MODULEENTRY32
        dwSize As Long
        th32ModuleID As Long
        th32ProcessID As Long
        GlblcntUsage As Long
        ProccntUsage As Long
        modBaseAddr As Long
        modBaseSize As Long
        hModule As Long
        szModule As String * 256
        szExePath As String * 260
    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 Module32First Lib "kernel32" (ByVal hSnapshot As _
    Long, lppe As Any) 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 MODULEENTRY32, rProcessFound As Long
        Dim i As Integer, szExename As String
        szExename = ""
        
        ' 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_SNAPMODULE, processID)
        ' 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 = Module32First(hSnapshot, uProcess)
        
        If rProcessFound Then
                i = InStr(1, uProcess.szExePath, Chr(0))
                If i > 0 Then szExename = Left$(uProcess.szExePath, i - 1)
        End If
        
        Call CloseHandle(hSnapshot)
        GetExeFromHandle = szExename
    End Function

  4. #4
    Hyperactive Member Sacofjoea's Avatar
    Join Date
    May 2000
    Location
    Never Never Land
    Posts
    472
    good code - using
    That arranged can be

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