[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
Re: Get the File Name of the Running VB6 Application.
Code:
Option Explicit
Private Sub Form_Load()
Me.Caption = App.EXEName
End Sub
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!
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.
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
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
Re: Get the File Name of the Running VB6 Application.
Quote:
Originally Posted by
Millerni456
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.
Re: Get the File Name of the Running VB6 Application.
Quote:
Originally Posted by
Max187Boucher
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?
Re: Get the File Name of the Running VB6 Application.
Quote:
Originally Posted by
DataMiser
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
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 :(
Re: Get the File Name of the Running VB6 Application.
Quote:
Originally Posted by
Millerni456
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...
Re: Get the File Name of the Running VB6 Application.
Quote:
Originally Posted by
Bonnie West
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