|
-
Apr 5th, 2009, 04:37 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] How to get "self" application extension?
Maybe someone know how to find "self" application extension or maybe even full path with extension?
-
Apr 5th, 2009, 04:43 PM
#2
Re: How to get "self" application 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
-
Apr 5th, 2009, 04:58 PM
#3
Thread Starter
Hyperactive Member
Re: How to get "self" application extension?
Yes, I know that but App Object can't show extension.
-
Apr 5th, 2009, 05:01 PM
#4
Re: How to get "self" application extension?
Your right, I thought it did, sorry!
I can think of one way to do it using some APIs like, GetModuleFileNameEx
-
Apr 5th, 2009, 05:15 PM
#5
Re: How to get "self" application extension?
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
-
Apr 5th, 2009, 10:37 PM
#6
Frenzied Member
Re: How to get "self" application extension?
Just a bit easier...
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
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)
Good Luck
Option Explicit should not be an Option!
-
Apr 6th, 2009, 12:36 PM
#7
Thread Starter
Hyperactive Member
Re: How to get "self" application extension?
I also have found other example how to do that:
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
This example works in same way as vb5prgrmr's example, but I still can't fully figure out how it works. 
For example what is different between:
Code:
Private Const MAX_PATH = 260
Dim S As String
S = Space(MAX_PATH)
and
Code:
Dim modName As String * 256
Also in hModule vb5prgrmr use "0" in example which I have found in hModule is used App.hInstance. 
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.
-
Apr 6th, 2009, 01:46 PM
#8
Re: How to get "self" application extension?
 Originally Posted by Lauriux1
For example what is different between:
Code:
Private Const MAX_PATH = 260
Dim S As String
S = Space(MAX_PATH)
and
Code:
Dim modName As String * 256
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.
Also in hModule vb5prgrmr use "0" in example which I have found in hModule is used App.hInstance.
Take a look at the API documentation for the function (in this case GetModuleFileName) for explanations of what the values of the parameters mean.
Don't blame me if I'm little bit obsessive, I just want to do that correctly.
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.
-
Apr 6th, 2009, 01:47 PM
#9
Re: How to get "self" application extension?
Code:
Dim modName As String * 256
creates a fixed length string in VB.
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.
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Apr 6th, 2009, 03:16 PM
#10
Thread Starter
Hyperactive Member
Re: How to get "self" application extension?
Thanks, mates!
Now it is clear for me. 
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?
-
Apr 6th, 2009, 04:12 PM
#11
Re: How to get "self" application extension?
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.
-
Apr 6th, 2009, 10:38 PM
#12
Frenzied Member
Re: How to get "self" application extension?
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
Option Explicit should not be an Option!
-
Apr 7th, 2009, 07:06 AM
#13
Thread Starter
Hyperactive Member
Re: How to get "self" application extension?
 Originally Posted by si_the_geek
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).
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?
 Originally Posted by si_the_geek
As to the length you should use, you should follow what Microsoft say - it is their function that you are calling.
I agree with you, I just was curious about that.
Anyway I'll follow what Microsoft says - 260.
-
Apr 7th, 2009, 11:29 AM
#14
Re: How to get "self" application extension?
If you alter the MsgBox lines for each version like this:
Code:
S = Left(S, R)
MsgBox S & "*"
Code:
modName = Left$(modName, i)
MsgBox modName & "*"
..you will see that the variable length one (S) contains just the value you want, and is immediately followed by the *
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.
-
Apr 7th, 2009, 02:13 PM
#15
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|