Annotation of mstools/samples/tls/tlsdll.c, revision 1.1

1.1     ! root        1: #include "windows.h"
        !             2: #include "stdlib.h"
        !             3: #include "string.h"
        !             4: #include "stdio.h"
        !             5: #include "tlsdll.h"
        !             6: 
        !             7: #define TLS_MINIMUM_AVAILABLE 64
        !             8: 
        !             9: DWORD TlsIndex;        /* Global TLS index */
        !            10: int TlsCount=0;        /* a counter for data in TLS storage */
        !            11: char chTlsCount[3];    /* string for counter values */
        !            12: 
        !            13: /*-----------------------------------------------------------------*/
        !            14: 
        !            15: void ErrorOut(char errstring[30])
        !            16: /*
        !            17: Purpose: Print out an meainful error code by means of
        !            18:          GetLastError and printf
        !            19: 
        !            20: Inputs:  errstring - the action that failed, passed by the
        !            21:                      calling proc.
        !            22: 
        !            23: Returns: none
        !            24: 
        !            25: Calls:   GetLastError
        !            26: */
        !            27: 
        !            28: {
        !            29: DWORD Error;
        !            30: 
        !            31: Error= GetLastError();
        !            32: printf("Error on %s = %d\n", errstring, Error);
        !            33: }
        !            34: 
        !            35: /*-----------------------------------------------------------------*/
        !            36: 
        !            37: INT  APIENTRY LibMain(HANDLE hInst, ULONG ul_reason_being_called, LPVOID lpReserved)
        !            38: 
        !            39: /*
        !            40: Purpose: Is called by LibEntry, upon entering a dll or detaching
        !            41:          from a DLL.  This function does most of the TLS manipulation.
        !            42: 
        !            43: Inputs:  hInst - not used
        !            44:          ul_reason_being_called - reason LibMain is called
        !            45: 
        !            46: 
        !            47: Returns: N/A
        !            48: 
        !            49: Calls:   TlsAlloc - allocate a TLS index
        !            50:          malloc - allocate space for tls storage
        !            51:          TlsSetValue - Store TLS values
        !            52:          TlsFree - free TLS index
        !            53:          TlsRetreive - retreives TLS values, based on index
        !            54: */
        !            55: 
        !            56: {
        !            57: 
        !            58: LPVOID TlsString;  /* TLS string for storage */
        !            59: 
        !            60: 
        !            61: UNREFERENCED_PARAMETER(hInst);
        !            62: UNREFERENCED_PARAMETER(lpReserved);
        !            63: 
        !            64: switch (ul_reason_being_called)
        !            65:   {
        !            66:   case DLL_PROCESS_ATTACH:           /* process attaches         */
        !            67:     printf("Process Attaching\n");
        !            68:     TlsIndex= TlsAlloc();            /* create TLS index         */
        !            69:     if (TlsIndex == 0xFFFFFFFF)
        !            70:       ErrorOut("TlsAlloc");
        !            71:     break;
        !            72: 
        !            73:   case DLL_THREAD_ATTACH:            /* thread attaches          */
        !            74:     if (!(DLL_PROCESS_ATTACH & ul_reason_being_called))
        !            75:     {
        !            76:     printf("Thread Attaching\n");
        !            77:     TlsCount++;
        !            78:     TlsString= malloc(100);          /* allocate storage         */
        !            79:     strcat(TlsString, "Tls Thread#");/* build -                  */
        !            80:     strcat(TlsString,
        !            81:           itoa(TlsCount, chTlsCount,
        !            82:           10) );                    /* - string                 */
        !            83:     if (TlsSetValue(TlsIndex,        /* set TLS value            */
        !            84:                    TlsString))
        !            85:       printf("TlsSetValue success\n\n");
        !            86:     else
        !            87:       ErrorOut("TlsSetValue");
        !            88:     }
        !            89:     else
        !            90:       printf("Process & Thread attaching\n\n");
        !            91:     break;
        !            92: 
        !            93:   case DLL_THREAD_DETACH:            /* thread detaches          */
        !            94:     printf("Thread Detaching\n");
        !            95:     TlsRetreive(TlsIndex);           /* retreive TLS data        */
        !            96:     break;
        !            97: 
        !            98:   case DLL_PROCESS_DETACH:           /* process detaches         */
        !            99:     printf("Process Detaching\n");
        !           100:     if (!TlsFree(TlsIndex))          /* free TLS index           */
        !           101:       ErrorOut("TlsFree");
        !           102:     else
        !           103:       printf("TlsIndex Freed\n");
        !           104:     break;
        !           105: 
        !           106: 
        !           107:   default:
        !           108:     break;
        !           109:   }
        !           110: 
        !           111: return 1;
        !           112: }
        !           113: 
        !           114: /****************************************************************************/
        !           115: 
        !           116: INT APIENTRY TlsInit()
        !           117: 
        !           118: /*
        !           119: Purpose: Allow LibMain to be executed for each thread
        !           120: 
        !           121: Inputs:  none
        !           122: 
        !           123: Returns: none
        !           124: 
        !           125: Calls:   none
        !           126: */
        !           127: 
        !           128: {
        !           129: return 1;
        !           130: }
        !           131: 
        !           132: /****************************************************************************/
        !           133: 
        !           134: INT APIENTRY TlsRetreive(DWORD TlsIndex)
        !           135: 
        !           136: /*
        !           137: Purpose: Retreive TLS values and display (printf) them
        !           138: 
        !           139: Inputs:  TlsIndex - TLS index value
        !           140: 
        !           141: Returns: none
        !           142: 
        !           143: Calls:   TlsGetValue - Gets the TLS value from the index
        !           144:                        based on the current thread.
        !           145: */
        !           146: 
        !           147: {
        !           148: LPVOID tlsvals;
        !           149: 
        !           150: (LPVOID)tlsvals= TlsGetValue(TlsIndex);   /* get the TLS value */
        !           151: if (tlsvals == 0)
        !           152:   ErrorOut("TlsGetValue");
        !           153: else
        !           154:   printf("tls value=  %s\n\n", tlsvals ); /* output it         */
        !           155: return 1;
        !           156: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.