GetModuleFileNameEx Function

Retrieves the fully-qualified path for the file containing the specified module.

Syntax

C++
DWORD WINAPI GetModuleFileNameEx(
  __in      HANDLE hProcess,
  __in_opt  HMODULE hModule,
  __out     LPTSTR lpFilename,
  __in      DWORD nSize
);

Parameters

hProcess [in]

A handle to the process that contains the module.

The handle must have the PROCESS_QUERY_INFORMATION and PROCESS_VM_READ access rights. For more information, see Process Security and Access Rights.

The GetModuleFileNameEx function does not retrieve the path for modules that were loaded using the LOAD_LIBRARY_AS_DATAFILE flag. For more information, see LoadLibraryEx.

hModule [in, optional]

A handle to the module. If this parameter is NULL, GetModuleFileNameEx returns the path of the executable file of the process specified in hProcess.

lpFilename [out]

A pointer to a buffer that receives the fully-qualified path to the module. If the size of the file name is larger than the value of the nSize parameter, the function succeeds but the file name is truncated and null terminated.

nSize [in]

The size of the lpFilename buffer, in characters.

Return Value

If the function succeeds, the return value specifies the length of the string copied to the buffer.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The GetModuleFileNameEx function is primarily designed for use by debuggers and similar applications that must extract module information from another process. If the module list in the target process is corrupted or is not yet initialized, or if the module list changes during the function call as a result of DLLs being loaded or unloaded, GetModuleFileNameEx may fail or return incorrect information.

To retrieve the name of a module in the current process, use the GetModuleFileName function. This is more efficient and more reliable than calling GetModuleFileNameEx with a handle to the current process.

To retrieve the name of the main executable module for a remote process, use the GetProcessImageFileName or QueryFullProcessImageName function. This is more efficient and more reliable than calling the GetModuleFileNameEx function with a NULL module handle.

Starting with Windows 7 and Windows Server 2008 R2, Psapi.h establishes version numbers for the PSAPI functions. The PSAPI version number affects the name used to call the function and the library that a program must load.

If PSAPI_VERSION is 2 or greater, this function is defined as K32GetModuleFileNameEx in Psapi.h and exported in Kernel32.lib and Kernel32.dll. If PSAPI_VERSION is 1, this function is defined as GetModuleFileNameEx in Psapi.h and exported in Psapi.lib and Psapi.dll as a wrapper that calls K32GetModuleFileNameEx.

Programs that must run on earlier versions of Windows as well as Windows 7 and later versions should always call this function as GetModuleFileNameEx. To ensure correct resolution of symbols, add Psapi.lib to the TARGETLIBS macro and compile the program with –DPSAPI_VERSION=1. To use run-time dynamic linking, load Psapi.dll.

Examples

For an example, see Enumerating All Modules for a Process.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderPsapi.h
LibraryKernel32.lib on Windows 7 and Windows Server 2008 R2, Psapi.lib if PSAPI_VERSION=1 on Windows 7 and Windows Server 2008 R2, Psapi.lib on Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000
DLLKernel32.dll on Windows 7 and Windows Server 2008 R2, Psapi.dll if PSAPI_VERSION=1 on Windows 7 and Windows Server 2008 R2, Psapi.dll on Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000
Unicode and ANSI namesGetModuleFileNameExW (Unicode) and GetModuleFileNameExA (ANSI)

See Also

EnumProcesses
GetModuleBaseName
GetModuleFileName
GetModuleHandle
LoadLibrary
Module Information
PSAPI Functions

Send comments about this topic to Microsoft

Build date: 11/19/2009

Tags :


Community Content

Resplendence
does not work on WOW64

Hi, GetModuleFileNameEx ALWAYS fails when running on WOW64 (a 32 bit process running on x64 Windows). I just wasted a lot of time going with the debugger finding out this boils down to NtQueryInformationProcess without being able to find the cause, until a message from Pabel Lebedinsky [MSFT] in some forum explained to me this does not work reliably and does not work on
WOW64.

BTW GetProcessImageFileName returns a path in kernel object syntax which is
something that's worth mentioning too. That's not something you would expect
from a usermode API.

//Daniel

Tags :

Page view tracker