INF: Retrieving the File Name of an Application or a DLL

ID Number: Q72385

3.00 3.10

WINDOWS

Summary:

In the Microsoft Windows environment, the GetModuleFileName function

provides the file name of the executable file that corresponds to a

given module instance handle or data instance handle.

More Information:

GetModuleFileName retrieves the full path of an application or a

dynamic-link library (DLL). Specify the instance handle or module

handle of the executable hModule parameter. The syntax of this

function is:

int GetModuleFileName(HANDLE hModule, LPSTR lpFilename, int nSize);

The hModule parameter identifies the module or instance handle,

lpFilename points to the buffer to receive the file name, and nSize

specifies the buffer size.

When Windows launches an application, its instance handle is a

parameter to WinMain. For a DLL, the instance handle is a parameter to

LibMain. The instance handle is also available through the GetWindow

function, as follows:

// hWnd is a handle to any one of the target

// executable file's windows or controls.

hAppInstance = GetWindowWord(hWnd, GWW_HINSTANCE);

nPathLength = GetModuleFileName(hAppInstance,

(LPSTR)szPath, PATH_LENGTH);

The GetModuleHandle function provides the module handle for a

specified module. For example, if Microsoft Excel is loaded, the full

path to the Excel executable file is available through the following

code:

// "EXCEL" can be used instead of "EXCEL.EXE".

hModule = GetModuleHandle("EXCEL");

nPathLength = GetModuleFileName(hModule,

(LPSTR)szPath, PATH_LENGTH);

Given a handle to a window or control in an application, the

GCW_HMODULE parameter to the GetClassWord function provides the

application's module handle.

Additional reference words: 3.00 3.10