Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

GetModuleHandleA function (libloaderapi.h)

Feedback

In this article

Retrieves a module handle for the specified module. The module must have been loaded by the calling process.

To avoid the race conditions described in the Remarks section, use theGetModuleHandleEx function.

Syntax

HMODULE GetModuleHandleA(  [in, optional] LPCSTR lpModuleName);

Parameters

[in, optional] lpModuleName

The name of the loaded module (either a .dll or .exe file). If the file name extension is omitted, the default library extension .dll is appended. The file name string can include a trailing point character (.) to indicate that the module name has no extension. 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,GetModuleHandle returns a handle to the file used to create the calling process (.exe file).

TheGetModuleHandle function does not retrieve handles for modules that were loaded using theLOAD_LIBRARY_AS_DATAFILE flag. For more information, seeLoadLibraryEx.

Return value

If the function succeeds, the return value is a handle to the specified module.

If the function fails, the return value is NULL. To get extended error information, callGetLastError.

Remarks

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

IflpModuleName 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, useside-by-side assemblies, or useGetModuleHandleEx to specify a memory location rather than a DLL name.

TheGetModuleHandle function returns a handle to a mapped module without incrementing its reference count. However, if this handle is passed to theFreeLibrary function, the reference count of the mapped module will be decremented. Therefore, do not pass a handle returned byGetModuleHandle to theFreeLibrary function. Doing so can cause a DLL module to be unmapped prematurely.

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, suppose that 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, the first thread would have a handle to a different module than the one intended.

Examples

For an example, seeUsing Brushes.

Note

The libloaderapi.h header defines GetModuleHandle as an alias that automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that is not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, seeConventions for Function Prototypes.

Requirements

RequirementValue
Minimum supported clientWindows XP [desktop apps only]
Minimum supported serverWindows Server 2003 [desktop apps only]
Target PlatformWindows
Headerlibloaderapi.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See also

Dynamic-Link Library Functions

FreeLibrary

GetModuleFileName

GetModuleHandleEx

LoadLibrary

LoadLibraryEx


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?