Results 1 to 12 of 12

Thread: [RESOLVED] Get the File Name of the Running VB6 Application.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Resolved [RESOLVED] Get the File Name of the Running VB6 Application.

    Hey all,

    I have an application I've built and I want to increment the revision numbers on it just by changing the File Name of the compiled executable.
    My hopes were to have my program look at the compiled executable (which is itself) then take the name and put it into my main form's caption.
    This would allow the revision number to be displayed on the form's caption (without needed to update that part as well). Yay Automation!

    So, I'd like to ask, is there a way where I can retrieve the File Name of the application that is running?

    Thanks much, your help is greatly appreciated!
    God Bless,
    -Nick

  2. #2

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Re: Get the File Name of the Running VB6 Application.

    Although App.EXEName looks good, this isn't what I'm looking for. App.EXEName is referencing the executable's title (defined in the projects properties), and not it's file name.

    Even though when compiling I could use the title to do the version incrementing, I would really like to use the file name. (As this can be done outside of VB6 IDE)

    Thanks though!

  4. #4
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Get the File Name of the Running VB6 Application.

    You could get all files of your App.Path and match the .exe matching your application's name then simply put that .exe file's name for you form's caption.

    I don't see why just do it the original way, updating it in your Project properties, would be the simplest way.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: Get the File Name of the Running VB6 Application.

    There is an api getmodulename i think, but you would need the process-id of your app
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Get the File Name of the Running VB6 Application.

    In the event that your exe's filename has Unicode characters in it, the following function will help you deal with VB's mostly ANSI-only limitation:

    Code:
    Private Declare Function GetModuleFileNameW Lib "kernel32.dll" (ByVal hModule As Long, ByVal lpFilename As Long, ByVal nSize As Long) As Long
    
    Private Function GetEXEName() As String
        Const MAX_PATH = 260&
    
        GetEXEName = Space$(MAX_PATH - 1&)
        GetEXEName = Left$(GetEXEName, GetModuleFileNameW(0&, StrPtr(GetEXEName), MAX_PATH))
        GetEXEName = Right$(GetEXEName, Len(GetEXEName) - InStrRev(GetEXEName, "\"))
    End Function
    
    Private Sub Form_Load()
        Caption = GetEXEName
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Get the File Name of the Running VB6 Application.

    Quote Originally Posted by Millerni456 View Post
    Although App.EXEName looks good, this isn't what I'm looking for. App.EXEName is referencing the executable's title (defined in the projects properties), and not it's file name.

    Even though when compiling I could use the title to do the version incrementing, I would really like to use the file name. (As this can be done outside of VB6 IDE)

    Thanks though!
    That is not actually the case. I just tested it here. I created a project and used the following code
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Unload Me
    End Sub
    
    Private Sub Form_Load()
    Me.Caption = App.EXEName
    
    End Sub
    The project properties are set to project1

    I compiled as MyTest.Exe
    I run the program and the caption reads MyTest.Exe
    I renamed the exe to MyTest2.Exe
    I run the program and the caption reads MyTest2.Exe
    I renamed it to BogusProgram.Exe and again it reads BogusProgram.Exe

    So it would appear that this does indeed give you the actual exe name of the running program and not what is set in the properties.

    You should have tried it before you dismissed it.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Re: Get the File Name of the Running VB6 Application.

    Quote Originally Posted by Max187Boucher View Post
    You could get all files of your App.Path and match the .exe matching your application's name then simply put that .exe file's name for you form's caption.

    I don't see why just do it the original way, updating it in your Project properties, would be the simplest way.
    When listing all of the files and searching for an executable, are there ways of confirming that the .exe being looked at matches the app?
    Suppose I had two .exe's in the directory where my program was ran; I'd get two possible file names that could be used. How can I distinguish which is the one I'm interested in?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Re: Get the File Name of the Running VB6 Application.

    Quote Originally Posted by DataMiser View Post
    So it would appear that this does indeed give you the actual exe name of the running program and not what is set in the properties.

    You should have tried it before you dismissed it.
    Surprisingly enough, I attempted that with no success. After reading your post I tried again and it works! :P
    To be honest, I think I tricked myself since I had modified the exe title to what I wanted it to be (containing the version#), and I had my file named the same way. Thus, I was unable to draw out where the reference came from.

    Thanks for re-iterating this solution!


    God Bless,
    -Nick

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] Get the File Name of the Running VB6 Application.

    No prob

    Now if only I could figure out why my emulator is not working

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Get the File Name of the Running VB6 Application.

    Quote Originally Posted by Millerni456 View Post
    Although App.EXEName looks good, this isn't what I'm looking for. App.EXEName is referencing the executable's title (defined in the projects properties), and not it's file name.
    Not a true statement - App.EXEName returns executable file name. This is easy enough to test:

    - create new project and keep all default names (form1, Project1, etc)
    - add that sample code I posted
    - save project (keep the default names)
    - compile it and name executable say "test"
    - run the exe - you should see "test" in the form's title bar (caption)

    Open the project and check its properties - you will still see the Project1...

  12. #12
    PowerPoster
    Join Date
    Jun 2012
    Posts
    2,388

    Re: Get the File Name of the Running VB6 Application.

    Quote Originally Posted by Bonnie West View Post
    In the event that your exe's filename has Unicode characters in it, the following function will help you deal with VB's mostly ANSI-only limitation:

    Code:
    Private Declare Function GetModuleFileNameW Lib "kernel32.dll" (ByVal hModule As Long, ByVal lpFilename As Long, ByVal nSize As Long) As Long
    
    Private Function GetEXEName() As String
        Const MAX_PATH = 260&
    
        GetEXEName = Space$(MAX_PATH - 1&)
        GetEXEName = Left$(GetEXEName, GetModuleFileNameW(0&, StrPtr(GetEXEName), MAX_PATH))
        GetEXEName = Right$(GetEXEName, Len(GetEXEName) - InStrRev(GetEXEName, "\"))
    End Function
    
    Private Sub Form_Load()
        Caption = GetEXEName
    End Sub
    Just as an addition to above code from Bonnie West:
    I would also exlude the file extension as the App.EXEName does it. Also check if running in the IDE or not as App.EXEName returns the project name in the IDE and the above code would return "vb6" in that case which is not very meaningful.
    Would then be like this:
    Code:
    Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameW" (ByVal hModule As Long, ByVal lpFileName As Long, ByVal nSize As Long) As Long
    
    Public Function GetEXEName() As String
    If InIDE() = False Then
        Const MAX_PATH As Long = 260
        Dim Buffer As String
        Buffer = String(MAX_PATH, vbNullChar)
        Buffer = Left$(Buffer, GetModuleFileName(0, StrPtr(Buffer), MAX_PATH + 1))
        Buffer = Right$(Buffer, Len(Buffer) - InStrRev(Buffer, "\"))
        GetEXEName = Left$(Buffer, InStrRev(Buffer, ".") - 1)
    Else
        GetEXEName = App.EXEName
    End If
    End Function
    
    Public Function InIDE(Optional ByRef B As Boolean = True) As Boolean
    If B = True Then Debug.Assert Not InIDE(InIDE) Else B = True
    End Function
    Last edited by Krool; Jan 13th, 2014 at 05:17 PM.

Tags for this Thread

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