GetModuleHandleExA function (libloaderapi.h) - Win32 apps | Microsoft Docs
The flag GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT says we shouldnt use FreeLibrary function (libloaderapi.h) - Win32 apps | Microsoft Docs or the FreeLibraryAndExitThread function (libloaderapi.h) - Win32 apps | Microsoft Docs function.
It says
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.
Remarks
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 DLL module to be unmapped prematurely.
If dwFlags contains GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, this function must be used carefully in a multithreaded application.
FreeLibrary and FreeLibraryAndExitThread confirm the above, so does anyone know how to unload a module when this flag is used?
TIA