// Win原理第七天.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "windows.h" _declspec (thread) int a = 200; _declspec (thread) int b = 300; void Fun() { printf("%d", a); //在不同的线程内,线程局部存储的全局变量在每一个线程内都有一份, //故而它仅仅是和线程相关的,在不同的线程内打印的数字不一样。 } DWORD WINAPI ThreadProc(LPVOID pParam) { a = 500; b = 600; Fun(); return 0; } DWORD WINAPI ThreadProc2(LPVOID pParam) { a = 700; b = 600; Fun(); return 0; } void NTAPI t_TlsCallBack_A(PVOID DllHandle, DWORD Reason, PVOID Red) { if (Reason == DLL_PROCESS_ATTACH) //可以做反调试的检测 { printf("Hello15pb1 "); } } void NTAPI t_TlsCallBack_B(PVOID DllHandle, DWORD Reason, PVOID Red) { if (Reason == DLL_PROCESS_ATTACH) //可以做反调试的检测 printf("HelloWorld2 "); } #pragma data_seg(".CRT$XLB") PIMAGE_TLS_CALLBACK p_thread_callback[] = { t_TlsCallBack_A, t_TlsCallBack_B, NULL }; #pragma data_seg() int _tmain(int argc, _TCHAR* argv[]) { //printf("线程局部存储的变量改变之前:%d,非线程局部存储的变量改变之前%d ",a,b); //HANDLE hThread = CreateThread(NULL, NULL, ThreadProc, NULL, NULL, NULL); //HANDLE hThread2 = CreateThread(NULL, NULL, ThreadProc2, NULL, NULL, NULL); //WaitForSingleObject(hThread, -1); //printf("线程局部存储的变量改变之后:%d,非线程局部存储的变量改变之后%d ", a, b); return 0; }