#include <windows.h>
#include <stdio.h>
#ifdef _WIN64
#pragma comment(linker, "/INCLUDE:_tls_used")
#pragma comment(linker, "/INCLUDE:tls_callback")
#else
#pragma comment(linker, "/INCLUDE:__tls_used")
#pragma comment(linker, "/INCLUDE:_tls_callback")
#endif
void NTAPI tls_callback(PVOID hModule, DWORD reason, PVOID pContext);
#if 0
#ifdef _WIN64
#pragma const_seg(".CRT$XLB")
EXTERN_C const
#else
#pragma data_seg(".CRT$XLB")
EXTERN_C
#endif
PIMAGE_TLS_CALLBACK tls_callback_func = tls_callback;
#ifdef _WIN64
#pragma const_seg()
#else
#pragma data_seg()
#endif //_WIN64
#endif
#if 1
#pragma section(".CRT$XLB", long, read)
__declspec(allocate(".CRT$XLB"))
EXTERN_C PIMAGE_TLS_CALLBACK tls_callback_func = tls_callback;
#endif
void NTAPI tls_callback(PVOID hModule, DWORD reason, PVOID pContext)
{
UNREFERENCED_PARAMETER(hModule);
UNREFERENCED_PARAMETER(pContext);
if (reason == DLL_PROCESS_ATTACH) {
printf("Process attach.\n");
}
else if (reason == DLL_THREAD_ATTACH) {
printf("Thread attach.\n");
}
else if (reason == DLL_THREAD_DETACH) {
printf("Thread detach.\n");
}
else if (reason == DLL_PROCESS_DETACH) {
printf("Process detach.\n");
}
}
DWORD WINAPI ThreadFunc(LPVOID lpParam) {
printf("Inside new thread.\n");
return 0;
}
int main()
{
printf("Main function.\n");
DWORD threadId;
HANDLE hThread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, &threadId);
WaitForSingleObject(hThread, INFINITE);
return 0;
}
TLS回调函数的两种写法
最新推荐文章于 2024-08-31 06:24:00 发布