This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
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.
HMODULE GetModuleHandleA( [in, optional] LPCSTR lpModuleName);[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.
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.
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.
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.
| Requirement | Value |
|---|---|
| Minimum supported client | Windows XP [desktop apps only] |
| Minimum supported server | Windows Server 2003 [desktop apps only] |
| Target Platform | Windows |
| Header | libloaderapi.h (include Windows.h) |
| Library | Kernel32.lib |
| DLL | Kernel32.dll |
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?