Annotation of q_a/samples/tls/tlsdll.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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