rnaui.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 6k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. //****************************************************************************
  2. //
  3. //  Module:     RNAUI.DLL
  4. //  File:       rnaui.c
  5. //  Content:    This file contains the moudle initialization.
  6. //  History:
  7. //      Tue 30-Nov-1993 07:42:02  -by-  Viroon  Touranachun [viroont]
  8. //
  9. //  Copyright (c) Microsoft Corporation 1991-1994
  10. //
  11. //****************************************************************************
  12. #include "rnaui.h"
  13. //****************************************************************************
  14. // Initialize GUIDs (should be done only and at-least once per DLL/EXE)
  15. //****************************************************************************
  16. #define INITGUID
  17. #include <initguid.h>
  18. #include <coguid.h>
  19. #include <oleguid.h>
  20. #include <shlguid.h>
  21. #include <shguidp.h>       // Remote CLSID
  22. //****************************************************************************
  23. // Constants
  24. //****************************************************************************
  25. //****************************************************************************
  26. // Global Parameters
  27. //****************************************************************************
  28. HINSTANCE ghInstance      = NULL;
  29. // we put the dll reference count and process count in the shared data
  30. //
  31. #pragma data_seg("SHAREDATA")
  32. static int  s_cProcesses  = 0;
  33. int         g_cRef        = 0;
  34. CRITICAL_SECTION g_csRNA = { 0 };
  35. #ifdef DEBUG
  36. BOOL g_bExclusive = FALSE;
  37. #endif
  38. #pragma data_seg()
  39. #ifdef DEBUG
  40. UINT g_uBreakFlags = 0;         // Controls when to int 3
  41. UINT g_uTraceFlags = 0;         // Controls what trace messages are spewed
  42. UINT g_uDumpFlags = 0;          // Controls what structs get dumped
  43. #endif
  44. #ifdef DEBUG
  45. /*----------------------------------------------------------
  46. Purpose: Enter a critical section
  47. Returns: --
  48. Cond:    --
  49. */
  50. void PUBLIC RNA_EnterExclusive(void)
  51.     {
  52.     EnterCriticalSection(&g_csRNA);
  53.     g_bExclusive = TRUE;
  54.     }
  55. /*----------------------------------------------------------
  56. Purpose: Leave a critical section
  57. Returns: --
  58. Cond:    --
  59. */
  60. void PUBLIC RNA_LeaveExclusive(void)
  61.     {
  62.     g_bExclusive = FALSE;
  63.     LeaveCriticalSection(&g_csRNA);
  64.     }
  65. #endif
  66. //****************************************************************************
  67. // BOOL _Processattach (HINSTANCE)
  68. //
  69. // This function is called when a process is attached to the DLL
  70. //
  71. // History:
  72. //  Mon 06-Sep-1993 09:20:10  -by-  Viroon  Touranachun [viroont]
  73. // Ported from Shell.
  74. //****************************************************************************
  75. BOOL _ProcessAttach(HINSTANCE hDll)
  76.     {
  77.     // It's okay to use a critical section in Chicago because (unlike
  78.     //  NT) they work across processes.
  79.     ReinitializeCriticalSection(&g_csRNA);
  80.     ASSERT(0 != *((LPDWORD)&g_csRNA));
  81.     ENTEREXCLUSIVE()
  82.         {
  83.         ghInstance = hDll;
  84.         if (0 == s_cProcesses++)
  85.             {
  86.             // Do first-time stuff here
  87.             }
  88. #ifdef DEBUG
  89.         // We do this simply to load the debug .ini flags
  90.         ProcessIniFile();
  91.         TRACE_MSG(TF_GENERAL, "Process Attach [%d] (hDll = %lx)", s_cProcesses, hDll);
  92.         DEBUG_BREAK(BF_ONPROCESSATT);
  93. #endif
  94.         }
  95.     LEAVEEXCLUSIVE()
  96.     return TRUE;
  97.     }
  98. //****************************************************************************
  99. // BOOL _ProcessDetach (HINSTANCE)
  100. //
  101. // This function is called when a process is detached from the DLL
  102. //
  103. // History:
  104. //  Mon 06-Sep-1993 09:20:10  -by-  Viroon  Touranachun [viroont]
  105. // Ported from Shell.
  106. //****************************************************************************
  107. BOOL _ProcessDetach(HINSTANCE hDll)
  108.     {
  109.     ENTEREXCLUSIVE()
  110.         {
  111.         ASSERT(hDll == ghInstance);
  112. #ifdef DEBUG
  113.         TRACE_MSG(TF_GENERAL, "Process Detach [%d] (hDll = %lx)",
  114.             s_cProcesses-1, hDll);
  115.         DEBUG_BREAK(BF_ONPROCESSDET);
  116. #endif
  117.         --s_cProcesses;
  118.         if (s_cProcesses == 0)
  119.             {
  120.             if (g_pidlRemote != NULL)
  121.                 {
  122.                 ILGlobalFree(g_pidlRemote);
  123.                 g_pidlRemote = NULL;
  124.                 };
  125.             };
  126.         }
  127.     LEAVEEXCLUSIVE()
  128.     if (0 == s_cProcesses)
  129.         {
  130.         // This use of g_csRNA is unprotected
  131.         DeleteCriticalSection(&g_csRNA);
  132.         }
  133.     return TRUE;
  134.     }
  135. //****************************************************************************
  136. // BOOL APIENTRY LibMain (HINSTANCE, DWORD, LPVOID)
  137. //
  138. // This function is called when the DLL is loaded
  139. //
  140. // History:
  141. //  Mon 06-Sep-1993 09:20:10  -by-  Viroon  Touranachun [viroont]
  142. // Ported from Shell.
  143. //****************************************************************************
  144. BOOL APIENTRY LibMain(HANDLE hDll, DWORD dwReason,  LPVOID lpReserved)
  145. {
  146.   switch(dwReason)
  147.   {
  148.     case DLL_PROCESS_ATTACH:
  149.         _ProcessAttach(hDll);
  150.         break;
  151.     case DLL_PROCESS_DETACH:
  152.         _ProcessDetach(hDll);
  153.         break;
  154.     case DLL_THREAD_ATTACH:
  155. #ifdef DEBUG
  156.         // We do this simply to load the debug .ini flags
  157.         //
  158.         ProcessIniFile();
  159.         TRACE_MSG(TF_GENERAL, "Thread Attach [%d] (hDll = %lx)",
  160.                   s_cProcesses, hDll);
  161.         DEBUG_BREAK(BF_ONTHREADATT);
  162. #endif
  163.         break;
  164.     case DLL_THREAD_DETACH:
  165. #ifdef DEBUG
  166.         TRACE_MSG(TF_GENERAL, "Thread Detach [%d] (hDll = %lx)",
  167.             s_cProcesses, hDll);
  168.         DEBUG_BREAK(BF_ONTHREADDET);
  169. #endif
  170.         break;
  171.     default:
  172.         break;
  173.   } // end switch()
  174.   return TRUE;
  175. }
  176. //****************************************************************************
  177. // STDAPI DllCanUnLoadNow()
  178. //
  179. // This function is called by shell
  180. //
  181. // History:
  182. //  Tue 23-Feb-1993 14:12:21  -by-  Viroon  Touranachun [viroont]
  183. // Created.
  184. //****************************************************************************
  185. STDAPI DllCanUnloadNow(void)
  186.     {
  187.     HRESULT hr;
  188.     ENTEREXCLUSIVE()
  189.         {
  190.         if (0 == g_cRef)
  191.             {
  192.             DEBUG_CODE( TRACE_MSG(TF_GENERAL, "DllCanUnloadNow says OK (Ref=%d)",
  193.                 g_cRef); )
  194.             hr = ResultFromScode(S_OK);
  195.             }
  196.         else
  197.             {
  198.             DEBUG_CODE( TRACE_MSG(TF_GENERAL, "DllCanUnloadNow says FALSE (Ref=%d)",
  199.                 g_cRef); )
  200.             hr = ResultFromScode(S_FALSE);
  201.             }
  202.         }
  203.     LEAVEEXCLUSIVE()
  204.     return hr;
  205.     }