Maybe someone know how to find "self" application extension or maybe even full path with extension?
Printable View
Maybe someone know how to find "self" application extension or maybe even full path with extension?
There is the App Object.
The App object is a global object accessed with the App keyword. It determines or specifies information about the application's title, version information, the path and name of its executable file and Help files, and whether or not a previous instance of the application is running.
i.e. to get path program is running from use, App.Path
Yes, I know that but App Object can't show extension.
Your right, I thought it did, sorry!
I can think of one way to do it using some APIs like, GetModuleFileNameEx
Maybe theres an easier way, but I came up with this real quick...
Code:Option Explicit
Private Const MAX_PATH As Long = 260
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
Private Const PROCESS_VM_READ As Long = (&H10)
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function OpenProcess Lib "KERNEL32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function CloseHandle Lib "KERNEL32.dll" (ByVal handle As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Sub Command1_Click()
' Show this executable file extension.
MsgBox GetFileExt(GetExePathFromPID(GetCurrentProcessId))
End Sub
Private Function GetExePathFromPID(PID As Long) As String
Dim S As String
Dim c As Long, hModule As Long, ProcHndl As Long
ProcHndl = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, PID)
If ProcHndl Then
S = String$(MAX_PATH, 0)
If EnumProcessModules(ProcHndl, hModule, 4, c) <> 0 Then c = GetModuleFileNameEx(ProcHndl, hModule, S, MAX_PATH)
If c Then GetExePathFromPID = Left$(S, c)
CloseHandle ProcHndl
End If
End Function
Private Function GetFileExt(sFile As String) As String
Dim i As Integer
i = InStrRev(sFile, ".")
If i Then GetFileExt = Mid$(sFile, i)
End Function
Just a bit easier...
However, when run in the vb ide it will return <path to where you installed vb\>vb6.exe as that is the process that your program is running in. (and you should check to make sure the value of "R" is > 0)Code:Option Explicit
Private Const MAX_PATH = 260
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" _
(ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Sub Form_Load()
Dim S As String, R As Long
S = Space(MAX_PATH)
R = GetModuleFileName(0, S, MAX_PATH)
MsgBox Left(S, R)
End Sub
Good Luck
I also have found other example how to do that:
This example works in same way as vb5prgrmr's example, but I still can't fully figure out how it works. :confused:Code:Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Sub Command1_Click()
Dim modName As String * 256
Dim i As Long
i = GetModuleFileName(App.hInstance, modName, Len(modName))
MsgBox Left$(modName, i)
End Sub
For example what is different between:
andCode:Private Const MAX_PATH = 260
Dim S As String
S = Space(MAX_PATH)
Also in hModule vb5prgrmr use "0" in example which I have found in hModule is used App.hInstance. :confused:Code:Dim modName As String * 256
Maybe someone could little bit explain about that?
P.S.
Don't blame me if I'm little bit obsessive, I just want to do that correctly.
The first way gives you a variable-length String, which contains just what you put in to it.
The second way gives you a fixed-length String which always contains the amount of characters specified - padded with spaces to the right of whatever you put in it. If you try to put more than that amount of characters into it, they are lost without warning.
With the examples as they are it doesn't make much difference, but if you change the MsgBox to put the value back into the variable instead (eg: modName = Left$(modName, i)), you would get lots of spaces when using the fixed-length version.
Take a look at the API documentation for the function (in this case GetModuleFileName) for explanations of what the values of the parameters mean.Quote:
Also in hModule vb5prgrmr use "0" in example which I have found in hModule is used App.hInstance. :confused:
That's the best way to be, as it means you actually learn what is going on, and can spot any issues with it - rather than having to ask people to write/fix it for you each time. :thumb:Quote:
Don't blame me if I'm little bit obsessive, I just want to do that correctly.
creates a fixed length string in VB.Code:Dim modName As String * 256
For more information on fixed length strings, refer here.
Basically fixed length strings put a cap on the amount of bytes you can store in a string.
:wave:
Thanks, mates!
Now it is clear for me. :afrog:
Just one question left, what fixed length for string should I choice? Because according to MSDN on this page:http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx max path lenght is 260 characters, but according to Wikipedia on page http://en.wikipedia.org/wiki/Filename max path lenght is 255 characters. Also I have read some other posts made by different peoples and almost all they have different opinions about max path lenght. So what is real max path lenght? :confused:
I wouldn't use a fixed-length string, I would use a variable-length one instead (as it means you can re-use the variable to store the final result).
As to the length you should use, you should follow what Microsoft say - it is their function that you are calling.
I believe (could be wrong) that max path was 255 in windows 3.1/3.11/DOS versions < 6.0 but changed with win95/DOS 6.0 (might have been DOS 5.X) and there are or used to be other OS's with the limit of 255 (Netware I think was one of them but has since been changed).
SO, to be absoluty sure, you need to check all of the documentation if you are on mixed OS/Protocal network but if you are on a pure MS network then 260 is the one you need to use as MAX_PATH is used for many a different thing.
Good Luck
I'm not sure that I understand what you want to say. Do you mean that if I use Fixed-length string in the end I always will have to use Left$() to get correct result? Maybe you can show me an example how would you do taht?
I agree with you, I just was curious about that. :)
Anyway I'll follow what Microsoft says - 260.
If you alter the MsgBox lines for each version like this:
Code:S = Left(S, R)
MsgBox S & "*"
..you will see that the variable length one (S) contains just the value you want, and is immediately followed by the *Code:modName = Left$(modName, i)
MsgBox modName & "*"
The fixed-length one (modName) contains the value followed by enough spaces to fill up the length specified, and then the *
To get the fixed-length version to work the same way, you need to use Left (with the same parameters, eg: MsgBox Left$(modName, i) & "*") every time you use the value - which you may forget to do, especially with code that you add later.
Thanks for all who have posted in this thread! :thumb:
Resolved :check:
Laurynas :afrog: