somelib.c

来自「一本已经绝版的好书」· C语言 代码 · 共 144 行

C
144
字号
/************************************************************
Module name: SomeLib.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
************************************************************/


#include "..\CmnHdr.H"                  /* See Appendix C. */
#include <windows.h>
#include "Resource.h"

#define SOMELIBAPI __declspec(dllexport)
#include "SomeLib.H"


/////////////////////////////////////////////////////////////


// Out thread local storage slot
DWORD g_dwTlsIndex = TLS_OUT_OF_INDEXES;

// Per mapping instance data for this DLL
HINSTANCE g_hinstDll = NULL;

// This mutex is required to guard access to the static
// nStringId variable inside the LoadResString function.
HANDLE g_hMutex = NULL;


/////////////////////////////////////////////////////////////


BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason,
   LPVOID lpvReserved) {

   LPTSTR lpszStr;

   switch (fdwReason) {

      case DLL_PROCESS_ATTACH:
         // DLL is attaching to the address space
         // of the current process.
         g_hinstDll = hinstDll;

         // Allocate a thread-local storage index.
         g_dwTlsIndex = TlsAlloc();

         if (g_dwTlsIndex == TLS_OUT_OF_INDEXES) {
            // The TLS index couldn't be allocated--have the
            // DLL return that initialization was
            // NOT successful.
            return(FALSE);
         }

         g_hMutex = CreateMutex(NULL, FALSE, NULL);
         if (g_hMutex == NULL) {
            // The mutex couldn't be created--have the
            // DLL return that initialization was
            // NOT successful.
            return(FALSE);
         }

         break;

      case DLL_THREAD_ATTACH:
         // A new thread is being created in the process.
         break;

      case DLL_THREAD_DETACH:
         // A thread is exiting cleanly.

         // Ensure that the TLS index was
         // allocated successfully.
         if (g_dwTlsIndex != TLS_OUT_OF_INDEXES) {

            // Get the pointer to the allocated memory.
            lpszStr = TlsGetValue(g_dwTlsIndex);

            // Test whether memory was ever allocated
            // for this thread.
            if (lpszStr != NULL) {
               HeapFree(GetProcessHeap(), 0, lpszStr);
            }
         }
         break;

      case DLL_PROCESS_DETACH:
         // The calling process is detaching the DLL
         // from its address space.

         // Ensure that the TLS index was
         // allocated successfully.
         if (g_dwTlsIndex != TLS_OUT_OF_INDEXES) {

            // Get the pointer to the allocated memory.
            lpszStr = TlsGetValue(g_dwTlsIndex);

            // Test whether memory was ever allocated
            // for this thread.
            if (lpszStr != NULL) {
               HeapFree(GetProcessHeap(), 0, lpszStr);
            }

            // Free the TLS index.
            TlsFree(g_dwTlsIndex);
         }

         // Ensure that the mutex was created successfully.
         if (g_hMutex != NULL) {
            CloseHandle(g_hMutex);
         }
         break;
   }
   return(TRUE);
}


/////////////////////////////////////////////////////////////


#define RESSTR_SIZE     (1000 * sizeof(TCHAR))

SOMELIBAPI LPCTSTR LoadResString (void) {
   static int nStringId = 0;

   LPTSTR lpszStr = TlsGetValue(g_dwTlsIndex);

   if (lpszStr == NULL) {
      lpszStr = HeapAlloc(GetProcessHeap(), 0, RESSTR_SIZE);
      TlsSetValue(g_dwTlsIndex, lpszStr);
   }

   WaitForSingleObject(g_hMutex, INFINITE);
   LoadString(g_hinstDll, IDS_STRINGFIRST + nStringId,
      lpszStr, RESSTR_SIZE);

   nStringId = (nStringId + 1) % IDS_STRINGNUM;
   ReleaseMutex(g_hMutex);

   return(lpszStr);
}


//////////////////////// End Of File ////////////////////////

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?