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

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

unix.superglobalmegacorp.com

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