Microsoft KB Archive/240239

= How To Capture the Path to a File that has been Loaded into an Applications Memory Space =

Article ID: 240239

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q240239



SUMMARY
Under some circumstances, developers might find it necessary to capture the path to an executable file created with Visual FoxPro or the path to a file that is running within an application's address space.

The path and file name of an executable file mapped into the address space of an application can be returned with the GetModuleFileName function of the Windows API.



MORE INFORMATION
The GetModuleHandle function is used to return a module handle for the specified module if the file has been mapped into the address space of the calling process.

The GetModuleFileName function retrieves the full path and filename for the file containing the specified module. If the function succeeds, the return value is the length, in characters, of the string copied to the buffer.

The following code illustrates this procedure.   Create a program file named GetName.PRG using the following code snippet:
 * 1) DEFINE MAX_PATH        256

parameter lcFileName

STORE SPACE(128) TO lpModuleName,lpFileName nSize       = MAX_PATH
 * !* Initialize variables

DECLARE INTEGER GetModuleHandle ; IN kernel32.DLL ; STRING @lpModuleName
 * !* Declare GetModuleHandle

DECLARE INTEGER GetModuleFileName ; IN kernel32.DLL ; INTEGER hModule, ; STRING @lpFileName, ; INTEGER nSize
 * !* Declare GetModuleFileName
 * !* End of API declarations

lpModuleName = lcFileName testa=GetModuleHandle(@lpModuleName) testb=GetModuleFileName(testa,@lpFileName,nSize) lpFileName=LEFT(ALLTRIM(lpFileName),AT(CHR(0),lpFileName)-1) =MESSAGEBOX(lpFileName,64,'Path returned by GetModuleFileName') CLEAR DLLS  From the command line, type DO GETNAME WITH ''. Because no file is referenced, a messagebox appears and displays the path to the Visual FoxPro executable file. From the command line, type DO GETNAME WITH 'GDI32'. Because GDI32.DLL is mapped into Visual FoxPro's address space, a messagebox appears and displays the path to the GDI32 dynamic link library. From the command line, type DO GETNAME WITH 'MYAPP'. Because MYAPP.DLL is not mapped into Visual FoxPro's address space, a messagebox appears and displays the path to the Visual FoxPro executable file. From the command line, type DO GETNAME WITH 'KERNEL32'. Because KERNEL32.DLL is mapped into Visual FoxPro's address space, a messagebox appears and displays the path to the KERNEL32 dynamic link library.
 * !* Will look for the name of the file passed to lcFileName
 * !* Get a handle to the executable file
 * !* Get the path to the file
 * !* Remove the null terminator from the string
 * !* Display the path in a messagebox.

(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by John Desch, Microsoft Corporation.

Keywords: kbhowto kbapi KB240239

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.