In the following article, I share with you how to use TLS callbacks in your C/C++ program compiled with Visual Studio.
Background
TLS (thread local storage) callbacks are a mechanism provided by the Windows loader to give your program a chance to do initialization/deinitialization tasks when the process starts, terminates, a thread is created or terminated.
A TLS callback has the following prototype:
typedef VOID (NTAPI *PIMAGE_TLS_CALLBACK) (
PVOID DllHandle,
DWORD Reason,
PVOID Reserved);
The Reason argument can be any of the following constants:
- DLL_PROCESS_ATTACH = 1
- DLL_PROCESS_DETACH = 0
- DLL_THREAD_ATTACH = 2
- DLL_THREAD_DETACH = 3
The TLS callbacks are encoded inside the compiled program’s TLS data directory(IMAGE_DIRECTORY_ENTRY_TLS). Please refer to the PE file structure. Continue reading “Using C/C++ TLS callbacks in Visual Studio with your 32 or 64bits programs”

Have you ever noticed
Two weeks ago, I started my road trip from Seattle, WA to San Diego, CA. It was my first very long road trip in the US. At first, I was just going to punch-in my destination into my GPS and drive along whatever route it suggests. However, after talking to friends who already took a similar road trip, they advised me to research taking the scenic route instead of the plain and dull I-5 highway. I became intrigued about the scenic route and my research indicated that it would be a longer but much more enjoyable trip.