自己翻阅的-TLS-DLL使用中要注意的DLL_PROCESS_ATTACH


#include "stdafx.h"
#include
DWORD dwTlsIndex;
int iNum=100;
void fuckmsg()
{
 TCHAR szBuffer[50] = {0};
 DWORD dwThreadID;
 int lpNum=(int)TlsGetValue(dwTlsIndex);
 lpNum++;
 dwThreadID = GetCurrentThreadId();
 wsprintf(szBuffer,TEXT("dwTlsIndex %d Thread %d exit,Value is %d"),dwTlsIndex,dwThreadID,lpNum);
    printf("%s\n",szBuffer);
}

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
 
 switch (ul_reason_for_call)
 {
  
  
  case DLL_PROCESS_ATTACH: //DLL_PROCESS_ATTACH:当一个DLL被首次载入进程地址空间时,系统会调用该DLL的DLLMain函数
        printf("%s----%d\n","DLL_PROCESS_ATTACH",GetCurrentProcessId());
  if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)
                return FALSE;
        break;
     case DLL_THREAD_ATTACH:
  printf("%s----%d\n","DLL_THREAD_ATTACH",GetCurrentThreadId());
        TlsSetValue(dwTlsIndex,(LPVOID)iNum);
  break;
 
        // The thread of the attached process terminates.
  
 case DLL_PROCESS_DETACH:
  
   TlsFree(dwTlsIndex);
            // Release the allocated memory for this thread.
   break;
 }
  return TRUE;
}

 

你可能感兴趣的:(自己翻阅的-TLS-DLL使用中要注意的DLL_PROCESS_ATTACH)