GetModuleHandleEx

The GetModuleHandleEx function retrieves a handle for the module name specified by parameter lpModuleName in a specified process.

Syntax

BOOL GetModuleHandleEx(
    DWORD dwFlags,
    LPCTSTR lpModuleName,
    HMODULE *phModule,
);

Parameters

dwFlags

This parameter can be zero or one or more of the following values. If the module's reference count is incremented, the caller must use the FreeLibrary function to decrement the reference count when the module handle is no longer needed.

Value Meaning

GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS

0x00000004

The lpModuleName parameter is an address in the module.

GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT

0x00000002

The reference count for the module is not incremented. This option is equivalent to the behavior of GetModuleHandle. Do not pass the retrieved module handle to the FreeLibrary function; doing so can cause the DLL to be unmapped prematurely. For more information, see Remarks. This option cannot be used with GET_MODULE_HANDLE_EX_FLAG_PIN.

lpModuleName

The name of the loaded module (either a .rtdll or .rtss file), or an address in the module (if dwFlags is GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS).

For a module name, if the file name extension is omitted, the default library extension .rtdll is appended. The string does not have to specify a path. When specifying a path, be sure to use backslashes (\), not forward slashes (/). The name is compared (case independently) to the names of modules currently mapped into the address space of the calling process.

If this parameter is NULL, the function returns a handle to the file used to create the calling process (.rtss file).

If parameter is NULL you must use the GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT Flag, otherwise the function will fail as you cannot directly increment the reference count to the calling process.

phModule

A handle to the specified module. If the function fails, this parameter is NULL.

Return Value

Returns TRUE if the function succeeds, FALSE if the function fails.

To get extended error information, call GetLastError.

Remarks

The handle returned is not global or inheritable. It cannot be duplicated or used by another process.

The RTX64 implementation of GetModuleHandleEx differs from Windows behavior in that the current process module will not have its reference count incremented. Attempts to retrieve the module from GetHandleModuleEx return the module, but the reference to that module will not be incremented.

If lpModuleName does not include a path and there is more than one loaded module with the same base name and extension, you cannot predict which module handle will be returned. To work around this problem, you could specify a path, use side-by-side assemblies, or specify a memory location rather than a RTDLL name in the lpModuleName parameter.

If dwFlags contains GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, the GetModuleHandleEx function returns a handle to a mapped module without incrementing its reference count. However, if this handle is passed to the FreeLibrary function, the reference count of the mapped module will be decremented. Therefore, do not pass a handle returned by GetModuleHandleEx with GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT to the FreeLibrary function. Doing so can cause a RTDLL module to be unmapped prematurely.

If dwFlags contains GET_MODULE_HANDLE_EX_UNCHANGED_REFCOUNT, this function must be used carefully in a multithreaded application. There is no guarantee that the module handle remains valid between the time this function returns the handle and the time it is used. For example, a thread retrieves a module handle, but before it uses the handle, a second thread frees the module. If the system loads another module, it could reuse the module handle that was recently freed. Therefore, first thread would have a handle to a module different than the one intended.

There is a known limitation in GetHandleModuleEx and GetHandleModule where implicitly-loaded DLLs cannot be searched, as they reside in a different handle List.

Requirements

Minimum Supported Version RTX64 3.0
Header windows.h
Library Rtx_Rtss.lib