Results 1 to 12 of 12

Thread: [RESOLVED] Obtain file location from Window caption

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    448

    Resolved [RESOLVED] Obtain file location from Window caption

    Hi,

    I'm wondering how to get the file location when I give the program a certain caption.

    So for example, I can create test.txt at:
    C:\test.txt

    I'll open the txt document, and the caption will give me

    test.txt - Notepad

    My question is, how would I go backwards from the "test.txt - Notepad" caption to obtain that window's actual location, which in this case is C:\test.txt?

    I did manage to find an example in Delphi, but no luck finding one in vb. In this example, you give the window's handle:

    http://www.delphitricks.com/source-c...ow_handle.html
    Last edited by k0zz; May 1st, 2008 at 08:43 PM.
    "Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Obtain file location from Window caption

    Store the files full path when you open it.. then just show what you want:
    Code:
    Dim strFullFilePath As String
    strFullFilePath = OpenFile("C:\test.txt.")
    Me.Caption = GetFileName(strFullFilePath)
    
    ' do something with strFullFilePath
    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    448

    Re: Obtain file location from Window caption

    The problem is that I don't have the given file location to use your code with.

    I don't know where the file is stored, thats why I'm trying to find it based off the window's caption.
    "Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Obtain file location from Window caption

    So.. you want to search a hard-drive for files named like the caption?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    448

    Re: Obtain file location from Window caption

    No,

    Lets say I have any EXE open. Lets say the caption of it is "The program"

    Is it possible to take that caption (I can retrieve the window handle) and get that file's actual location?

    I would like to get a file's path from the window's handle, basically.

    Thanks
    "Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Obtain file location from Window caption

    Window handles and their paths have no correlation. Unless you have physical access to the programs code.. there is no way to get its path.

    You can however, just search the harddrive for, lets say, "Notepad". But then, it won't always work, because you have things like "MSN Messenger" that are called "msnmsgr.exe".

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    448

    Re: Obtain file location from Window caption

    I've actually found the code for it in the codebank:

    http://www.vbforums.com/showthread.php?t=241198

    but one small problem. Lets say I want to find the location of a textfile, named C:\file.txt. The problem is, when I use that open text file's hWND, I will get returned notepad.exe's location instead of C:\file.txt.

    Any way around this?
    "Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."

  8. #8
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Obtain file location from Window caption

    Quote Originally Posted by k0zz
    I've actually found the code for it in the codebank:

    http://www.vbforums.com/showthread.php?t=241198

    but one small problem. Lets say I want to find the location of a textfile, named C:\file.txt. The problem is, when I use that open text file's hWND, I will get returned notepad.exe's location instead of C:\file.txt.

    Any way around this?
    That code uses a process's information to get what it needs. A file handle isn't a process, therefore, in theory, its not possible.

    There would be a way to do it.. but nothing springs to mind that would be easy/worth doing.

    Why is it you need this?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    448

    Re: Obtain file location from Window caption

    Well, im making this for a user that has some annoying program on his computer that he can only recognize by its caption. I can have my program find the caption then end it, but it has a nasty habit of coming back up at some later time. I wanted to be able to find the the EXE's filepath so I can delete it. It also seems to be creating random log files and other kinds of txt documents, and shelling them, and ending them, at random. I wanted to see where it was storing them.

    So, its actually possible to get its value? In theory, I guess I could just take the meaningful part of the .txt document's caption and do a search for it on the harddrive, but is there any easier way of doing it?
    "Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."

  10. #10
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Obtain file location from Window caption

    Found this, might be a start? I don't know much about this sort of code.

    Code:
    Private Sub Command1_Click()
        Dim objServices, objProcessSet, Process
        Dim results
         
        Set objServices = GetObject("winmgmts:")
        Set objProcessSet = objServices.ExecQuery("Select * from Win32_Process")
        For Each Process In objProcessSet
            If (UCase("Notepad.exe") = UCase(Process.Caption)) Then
                results = Split(Process.commandline, """ ", , vbTextCompare)
                Debug.Print Trim(results(1))
            End If
        Next
    End Sub

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    448

    Re: Obtain file location from Window caption

    Woah, Edgemeal your code works perfectly.
    "Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."

  12. #12
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Obtain file location from Window caption

    I just found the code somewhere and just changed it to notepad.exe,

    Probably want to add this before the end sub,
    Set objServices = Nothing
    Set objProcessSet = Nothing

    Though I'm not at all knowledgeable in the "winmgmts:" dept. ;P

    Good luck!

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