Expand

GetModuleBaseName function

7 out of 8 rated this helpful Rate this topic

Retrieves the base name of the specified module.

Syntax

DWORD WINAPI GetModuleBaseName(
  __in      HANDLE hProcess,
  __in_opt  HMODULE hModule,
  __out     LPTSTR lpBaseName,
  __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.

hModule [in, optional]

A handle to the module. If this parameter is NULL, this function returns the name of the file used to create the calling process.

lpBaseName [out]

A pointer to the buffer that receives the base name of the module. If the base name is longer than maximum number of characters specified by the nSize parameter, the base name is truncated.

nSize [in]

The size of the lpBaseName buffer, in characters.

Return value

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

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

Remarks

The GetModuleBaseName 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, GetModuleBaseName may fail or return incorrect information.

To retrieve the base name of a module in the current process, use the GetModuleFileName function to retrieve the full module name and then use a function call such as strrchr(szmodulename, '\\') to scan to the beginning of the base name within the module name string. This is more efficient and more reliable than calling GetModuleBaseName with a handle to the current process.

To retrieve the base name of the main executable module for a remote process, use the GetProcessImageFileName or QueryFullProcessImageName function to retrieve the module name and then use the strrchr function as described in the previous paragraph. This is more efficient and more reliable than calling GetModuleBaseName with a NULL module handle.

The GetModuleBaseName function does not retrieve the base name for modules that were loaded with the LOAD_LIBRARY_AS_DATAFILE flag. For more information, see LoadLibraryEx.

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 K32GetModuleBaseName in Psapi.h and exported in Kernel32.lib and Kernel32.dll. If PSAPI_VERSION is 1, this function is defined as GetModuleBaseName in Psapi.h and exported in Psapi.lib and Psapi.dll as a wrapper that calls K32GetModuleBaseName.

Programs that must run on earlier versions of Windows as well as Windows 7 and later versions should always call this function as GetModuleBaseName. 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 Processes.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

Psapi.h

Library

Kernel32.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

DLL

Kernel32.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

Unicode and ANSI names

GetModuleBaseNameW (Unicode) and GetModuleBaseNameA (ANSI)

See also

EnumProcesses
GetModuleFileNameEx
Module Information
PSAPI Functions
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l

 

 

Send comments about this topic to Microsoft

Build date: 12/3/2011

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
documentation error
for the second parameter the documentation states:

"If this parameter is NULL, this function returns the name of the file used to create the calling process."

that is not true because the returned name is of course that of the executable file used to create the process identified by the first parameter, not the calling one.
vb.net syntax
<DllImport("psapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function GetModuleBaseName(ByVal processHandle As SafeProcessHandle, ByVal moduleHandle As Integer, ByVal baseName As StringBuilder, ByVal size As Integer) As Integer End Function
C# syntax
[DllImport("psapi.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int GetModuleBaseName(SafeProcessHandle processHandle, int moduleHandle, StringBuilder baseName, int size);