Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get path to directory in which .exe is being run??? 2

Status
Not open for further replies.

Paladyr

Programmer
Apr 22, 2001
508
US
Is there a function that will return the path to the .exe that is running in VB??? Thanks!
 
Yes. Try

spath = App.Path Anything is possible, the problem is I only have one lifetime.
[cheers]
 
I just read this from an article and am wondering if it is true:

"The obvious solution is to use the .Path property of the App object (App.Path). However, you may be in for a surprise when you see the results when your executable resides on a server in a network directory. App.Path often returns erroneous results like returning the path to the system folder instead."

This program will defnitely be run off a network so it doesn't look like App.Path will work. This looks like what i need but I don't know how to use it:

 
Sorry, I did not know that. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
Code:
Public Declare Function GetClassWord 
Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Public Declare 
Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal 
lpFileName As String, ByVal nSize As (Long) As Long

Private Sub 
Command2_Click()
hModule = GetClassWord(hwndTarget, 
GCW_HMODULE) 
fnam$ = String$(255, vbNullChar)
retlen% = GetModuleFileName(hModule, fnam$, 254)
fnam$ = Left$(fnam$, retlen%)

MsgBox fnam$

End Sub
[\code]

This is what i found... i'll see if it works...
 
The code worked... but can someone explain to me what is going on when this runs:

Code:
hModule = GetClassWord(hwndTarget, GCW_HMODULE)
fnam$ = String$(255, vbNullChar)
retlen% = GetModuleFileName(hModule, fnam$, 254)
fnam$ = Left$(fnam$, retlen%)

MsgBox fnam$
pathCTX = GetPath("CTX")
 
' call the API to get the class word
hModule = GetClassWord(hwndTarget, GCW_HMODULE)

' initialize a string variable with 255 null chars
fnam$ = String$(255, vbNullChar)

' this function does a Memory Copy of up to 254 characters
' from the hModule into fname. The last character is
' reserved to contain a nullchar, as from the previous
' initialization. The funtion will actually return the
' number of bytes moved. by initializing the string with
' all nulls, we know that somewhere we will have a valid
' end of string (vbnullchar) character.
retlen% = GetModuleFileName(hModule, fnam$, 254)

' strip away all of the trailing nulls
fnam$ = Left$(fnam$, retlen%)

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Thank you for the explanation :). I have a few more questions though:

What is a class word, and what gets stored in hModule? It looks like it is just a number... i suppose the FreeFile number???

What is HWND??? Also what is HWNDTarget?? I used this (HWND) in code I used to fill a list box and am not clear on what it is.

I assume the "GetModuleFileName" identifies the program running by the number stored in hModule, runs whatever code is in there, then sticks the path to the exe referred to by hModule into fnam$ with a max path length of 254????
 
Windows keeps track of what is going on in the system be maintaing a set of data structues which contain information about tasks, processes, windows, etc. It access these data structures thru the use of pointers, and these points are called handles. Generally speaking, any variable name that is preceeded by a small 'h' is one of these handles.

hWnd is the handle to a window
hModule is the handle to a modules

The GetModuleFileName filename takes the module handle (hModule), which tells windows where the information about that module is, and within this specific function, the FileName is extracted from that structure and placed in the fnam$ variable, up to a max of 254 characters.

A listbox is iteslf a window, so the listbox.hwnd is the handle to the listbox window so windows knows to which window to fill when you're doing that fill operation.

the hWndTarget parameter is to get the class word of the module associated with a specific window. The hWndTarget is the handle to the window that is responsible for that module.

As far as I know, the ClassWord is also a pointer specifically to extended information about the window in question.

You should also be aware the GetClassWord is an old 16 bit function, and is only available for backward compatability. It has been superseeded by the GetClassLong function, and I would suggest that you consider using GetClassLong instead.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Woh, that was an awesome explanation!!!! Thank you so much for explaining all of that. I could only speculate to get to an explanation that is close to what you said... now I am a lot clearer on the subject. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top