Stream Destruction Notification

Is there a way to get a notification that the thread is no longer working (returned) in your application?
I know this is possible in kernel mode (using PsSetCreateThreadNotifyRoutine), but is there any way to find this out from user mode using only the Win32 API?

The problem is that I cannot control the code in the stream, because my module is part of the library. Creating a driver for monitoring the system will not be too complicated, but it is annoying for users to install the driver even for the base application that uses my library.

My code uses TLS storage, and on Linux / Unix pthread_key_create can take a pointer to a function that is called when the thread is destroyed. But TlsAlloc (Windows) has nothing of the kind ...

Thanks in advance!

+4
source share
4 answers

Depending on which librarian you have. For a DLL, it can handle thread termination in your DllMain ( DLL_THREAD_DETACH ). MSDN claims this is the best place to work with TLS resources.

Keep in mind that this callback is only processed for a thread that comes out cleanly (not, for example, TerminateThread() ).

+5
source

Similar functionality is available with Fibers. From MSDN:

FlsAlloc , FlsCallback , FlsFree

FlsCallback Callback Function

The function defined by the application. If the FLS slot is used, FlsCallback - this is called the removal of the fiber, the output stream, and when the FLS index is freed.

+5
source

You can simply call WaitForSingleObject in the stream handle.

+2
source

You can try installing the IAT APIs on ExitThread () ...

The advantage of this is that you have to start in the context of a thread that exits, which may or may not be useful to you.

See this post: Windows API spying / hijacking methods for some details about this connection ...

+2
source

All Articles