Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.0 KiB

  1. //+----------------------------------------------------------------------------
  2. // File: dll.hxx
  3. //
  4. // Synopsis: This file contains the core routines and globals for creating
  5. // DLLs
  6. //
  7. //-----------------------------------------------------------------------------
  8. #ifndef _DLL_HXX
  9. #define _DLL_HXX
  10. extern HANDLE g_hinst; // Module instance HANDLE
  11. // Types ----------------------------------------------------------------------
  12. struct THREADSTATE
  13. {
  14. THREADSTATE * ptsNext;
  15. struct
  16. {
  17. DWORD idThread;
  18. LONG cUsage;
  19. IMalloc * pmalloc;
  20. } dll;
  21. };
  22. typedef HRESULT (*PFN_PATTACH)();
  23. typedef HRESULT (*PFN_TATTACH)(THREADSTATE * pts);
  24. typedef void (*PFN_PDETACH)();
  25. typedef void (*PFN_TDETACH)(THREADSTATE * pts);
  26. extern PFN_PATTACH g_apfnPAttach[];
  27. extern PFN_TATTACH g_apfnTAttach[];
  28. extern PFN_PDETACH g_apfnPDetach[];
  29. extern PFN_TDETACH g_apfnTDetach[];
  30. #define BEGIN_PROCESS_ATTACH \
  31. extern PFN_PATTACH g_apfnPAttach[] = \
  32. {
  33. #define END_PROCESS_ATTACH \
  34. NULL \
  35. };
  36. #define BEGIN_PROCESS_DETACH \
  37. extern PFN_PDETACH g_apfnPDetach[] = \
  38. {
  39. #define END_PROCESS_DETACH \
  40. NULL \
  41. };
  42. #define BEGIN_THREAD_ATTACH \
  43. extern PFN_TATTACH g_apfnTAttach[] = \
  44. {
  45. #define END_THREAD_ATTACH \
  46. NULL \
  47. };
  48. #define BEGIN_THREAD_DETACH \
  49. extern PFN_TDETACH g_apfnTDetach[] = \
  50. {
  51. #define END_THREAD_DETACH \
  52. NULL \
  53. };
  54. #define ATTACH_METHOD(x) \
  55. x,
  56. #define DETACH_METHOD(x) \
  57. x,
  58. typedef void (*PFN_PPASSIVATE)();
  59. typedef void (*PFN_TPASSIVATE)(THREADSTATE * pts);
  60. extern PFN_PPASSIVATE g_apfnPPassivate[];
  61. extern PFN_TPASSIVATE g_apfnTPassivate[];
  62. #define BEGIN_PROCESS_PASSIVATE \
  63. extern PFN_PPASSIVATE g_apfnPPassivate[] = \
  64. {
  65. #define END_PROCESS_PASSIVATE \
  66. NULL \
  67. };
  68. #define BEGIN_THREAD_PASSIVATE \
  69. extern PFN_TPASSIVATE g_apfnTPassivate[] = \
  70. {
  71. #define END_THREAD_PASSIVATE \
  72. NULL \
  73. };
  74. #define PASSIVATE_METHOD(x) \
  75. x,
  76. enum LICREQUEST
  77. {
  78. LICREQUEST_VALIDATE = 1,
  79. LICREQUEST_OBTAIN,
  80. LICREQUEST_INFO,
  81. };
  82. typedef HRESULT (*PFN_CLASSFACTORY)(IUnknown * pUnkOuter, REFIID riid, void ** ppvObj);
  83. typedef HRESULT (*PFN_CLASSLICENSE)(LICREQUEST lr, void * pv);
  84. struct CLASSFACTORY
  85. {
  86. const CLSID * pclsid;
  87. PFN_CLASSFACTORY pfnFactory;
  88. PFN_CLASSLICENSE pfnLicense;
  89. };
  90. extern CLASSFACTORY g_acf[];
  91. #define BEGIN_CLASS_FACTORIES \
  92. extern CLASSFACTORY g_acf[] = \
  93. {
  94. #define FACTORY(clsid, pfnFactory, pfnLicense) \
  95. { &clsid, pfnFactory, pfnLicense },
  96. #define END_CLASS_FACTORIES \
  97. { NULL, NULL, NULL } \
  98. };
  99. const DWORD NULL_TLS = (DWORD)(-1);
  100. const LONG REF_GUARD = 255;
  101. extern struct GINFO
  102. {
  103. union
  104. {
  105. DWORD dwFlags; // All flags
  106. struct
  107. {
  108. DWORD fUnicode:1; // System supports Unicode
  109. DWORD fWinNT:1; // System is WinNT
  110. };
  111. };
  112. } g_ginfo;
  113. extern HANDLE g_heap; // Process heap
  114. // Classes --------------------------------------------------------------------
  115. //+----------------------------------------------------------------------------
  116. // Template: TLock
  117. //
  118. // Synopsis: This class provides Enter/LeaveCriticalSection behavior
  119. // through its constructor/destructor
  120. //
  121. //-----------------------------------------------------------------------------
  122. template <class TYPE> class TLock
  123. {
  124. public:
  125. TLock()
  126. {
  127. EnterCriticalSection(&s_cs);
  128. #ifdef _DEBUG
  129. if (!s_cNesting)
  130. s_tidOwner = GetCurrentThreadId();
  131. else
  132. Assert(s_tidOwner == GetCurrentThreadId());
  133. s_cNesting++;
  134. #endif
  135. }
  136. ~TLock()
  137. {
  138. #ifdef _DEBUG
  139. Assert(s_tidOwner == GetCurrentThreadId());
  140. Assert(s_cNesting > 0);
  141. s_cNesting--;
  142. #endif
  143. LeaveCriticalSection(&s_cs);
  144. }
  145. static void Init()
  146. {
  147. InitializeCriticalSection(&s_cs);
  148. }
  149. static void Deinit()
  150. {
  151. Assert(!s_cNesting);
  152. DeleteCriticalSection(&s_cs);
  153. }
  154. protected:
  155. static CRITICAL_SECTION s_cs;
  156. #ifdef _DEBUG
  157. static LONG s_cNesting;
  158. static DWORD s_tidOwner;
  159. #endif
  160. };
  161. #define DEFINE_LOCK(x) typedef struct { int i; } x
  162. #define DECLARE_LOCK(x) CRITICAL_SECTION TLock<x>::s_cs; \
  163. Debug(LONG TLock<x>::s_cNesting = 0); \
  164. Debug(DWORD TLock<x>::s_tidOwner = 0);
  165. #define INIT_LOCK(x) TLock<x>::Init();
  166. #define DEINIT_LOCK(x) TLock<x>::Deinit();
  167. #define LOCK(x) TLock<x> lock##x;
  168. DEFINE_LOCK(DLL);
  169. // Prototypes ----------------------------------------------------------------
  170. HRESULT GetWin32Hresult();
  171. HRESULT AllocateThreadState(THREADSTATE ** ppts);
  172. HRESULT EnsureThreadState();
  173. inline THREADSTATE * GetThreadState()
  174. {
  175. extern DWORD g_tlsThreadState;
  176. Assert(g_tlsThreadState != NULL_TLS);
  177. Assert(TlsGetValue(g_tlsThreadState));
  178. return (THREADSTATE *)TlsGetValue(g_tlsThreadState);
  179. }
  180. #define TLS(x) GetThreadState()->x
  181. void IncrementProcessUsage();
  182. void IncrementThreadUsage();
  183. void DecrementProcessUsage();
  184. void DecrementThreadUsage();
  185. #endif // _DLL_HXX