3 out of 4 rated this helpful - Rate this topic

FreeLibraryAndExitThread function

Applies to: desktop apps only

Decrements the reference count of a loaded dynamic-link library (DLL) by one, then calls ExitThread to terminate the calling thread. The function does not return.

Syntax

VOID WINAPI FreeLibraryAndExitThread(
  __in  HMODULE hModule,
  __in  DWORD dwExitCode
);

Parameters

hModule [in]

A handle to the DLL module whose reference count the function decrements. The LoadLibrary or GetModuleHandleEx function returns this handle.

Do not call this function with a handle returned by the GetModuleHandle function, since this function does not maintain a reference count for the module.

dwExitCode [in]

The exit code for the calling thread.

Return value

This function does not return a value. Invalid module handles are ignored.

Remarks

The FreeLibraryAndExitThread function allows threads that are executing within a DLL to safely free the DLL in which they are executing and terminate themselves. If they were to call FreeLibrary and ExitThread separately, a race condition would exist. The library could be unloaded before ExitThread is called.

Requirements

Minimum supported client

Windows XP

Minimum supported server

Windows Server 2003

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

DisableThreadLibraryCalls
Dynamic-Link Library Functions
ExitThread
FreeLibrary
Run-Time Dynamic Linking

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
OK to call FreeLibraryAndExitThread() within DllMain()?

When you get the answer, please post here as well. Thanks.

I think it should be, since documentation says that LoadLibrary/FreeLibrary are serialized by loader critical section... but I have been wrong in the past... once... :-)

If you are in DllMain, you didn't create the thread yourself.. And I don't see why you should terminate somebody elses thread. Especially when you are handling an event generated by them. The function is there to allow you to terminate your own thread, while also releasing (possibly the last) reference to your DLL.

 

[tfl - 07 04 10] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&