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.

296 lines
7.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: dll.cpp
  8. //
  9. // Core entry points for the DLL
  10. //
  11. //--------------------------------------------------------------------------
  12. #include "pch.h"
  13. #pragma hdrstop
  14. #define INITGUID
  15. #include <initguid.h>
  16. #include "iids.h"
  17. /*----------------------------------------------------------------------------
  18. / Globals
  19. /----------------------------------------------------------------------------*/
  20. HINSTANCE g_hInstance = NULL;
  21. HINSTANCE g_hAclEditDll = NULL;
  22. DWORD g_tls = 0xffffffffL;
  23. /*-----------------------------------------------------------------------------
  24. / DllMain
  25. / -------
  26. / Main entry point. We are passed reason codes and assored other
  27. / information when loaded or closed down.
  28. /
  29. / In:
  30. / hInstance = our instance handle
  31. / dwReason = reason code
  32. / pReserved = depends on the reason code.
  33. /
  34. / Out:
  35. / -
  36. /----------------------------------------------------------------------------*/
  37. STDAPI_(BOOL)
  38. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*pReserved*/)
  39. {
  40. switch (dwReason)
  41. {
  42. case DLL_PROCESS_ATTACH:
  43. g_hInstance = hInstance;
  44. g_tls = TlsAlloc();
  45. DebugProcessAttach();
  46. TraceSetMaskFromCLSID(CLSID_DsSecurity);
  47. #ifndef DEBUG
  48. DisableThreadLibraryCalls(hInstance);
  49. #endif
  50. break;
  51. case DLL_PROCESS_DETACH:
  52. SchemaCache_Destroy();
  53. if (g_hAclEditDll)
  54. FreeLibrary(g_hAclEditDll);
  55. TlsFree(g_tls);
  56. DebugProcessDetach();
  57. break;
  58. case DLL_THREAD_DETACH:
  59. DebugThreadDetach();
  60. break;
  61. }
  62. return TRUE;
  63. }
  64. /*-----------------------------------------------------------------------------
  65. / DllCanUnloadNow
  66. / ---------------
  67. / Called by the outside world to determine if our DLL can be unloaded. If we
  68. / have any objects in existance then we must not unload.
  69. /
  70. / In:
  71. / -
  72. / Out:
  73. / BOOL inidicate unload state.
  74. /----------------------------------------------------------------------------*/
  75. STDAPI
  76. DllCanUnloadNow(void)
  77. {
  78. return GLOBAL_REFCOUNT ? S_FALSE : S_OK;
  79. }
  80. /*-----------------------------------------------------------------------------
  81. / DllGetClassObject
  82. / -----------------
  83. / Given a class ID and an interface ID, return the relevant object. This used
  84. / by the outside world to access the objects contained here in.
  85. /
  86. / In:
  87. / rCLISD = class ID required
  88. / riid = interface within that class required
  89. / ppv -> receives the newly created object.
  90. /
  91. / Out:
  92. / -
  93. /----------------------------------------------------------------------------*/
  94. STDAPI
  95. DllGetClassObject(REFCLSID rCLSID, REFIID riid, LPVOID *ppv)
  96. {
  97. HRESULT hr;
  98. CDsSecurityClassFactory *pClassFactory;
  99. TraceEnter(TRACE_CORE, "DllGetClassObject");
  100. TraceGUID("Object requested", rCLSID);
  101. TraceGUID("Interface requested", riid);
  102. *ppv = NULL;
  103. if (!IsEqualIID(rCLSID, CLSID_DsSecurity))
  104. ExitGracefully(hr, CLASS_E_CLASSNOTAVAILABLE, "CLSID not supported");
  105. pClassFactory = new CDsSecurityClassFactory;
  106. if (!pClassFactory)
  107. ExitGracefully(hr, E_OUTOFMEMORY, "Failed to create class factory");
  108. hr = pClassFactory->QueryInterface(riid, ppv);
  109. if (FAILED(hr))
  110. delete pClassFactory;
  111. exit_gracefully:
  112. TraceLeaveResult(hr);
  113. }
  114. /*-----------------------------------------------------------------------------
  115. / WaitOnThread
  116. / -----------------
  117. / If a thread is running (if the handle is non-NULL) wait for it to complete.
  118. / Then set the handle to NULL.
  119. /
  120. / In:
  121. / phThread = address of thread handle
  122. /
  123. / Out:
  124. / Result of WaitForSingleObject, or zero.
  125. /----------------------------------------------------------------------------*/
  126. DWORD
  127. WaitOnThread(HANDLE *phThread)
  128. {
  129. DWORD dwResult = 0;
  130. if (phThread != NULL && *phThread != NULL)
  131. {
  132. HCURSOR hcurPrevious = SetCursor(LoadCursor(NULL, IDC_WAIT));
  133. SetThreadPriority(*phThread, THREAD_PRIORITY_HIGHEST);
  134. dwResult = WaitForSingleObject(*phThread, INFINITE);
  135. CloseHandle(*phThread);
  136. *phThread = NULL;
  137. SetCursor(hcurPrevious);
  138. }
  139. return dwResult;
  140. }
  141. /*-----------------------------------------------------------------------------
  142. / Thread Local Storage helpers
  143. /----------------------------------------------------------------------------*/
  144. /*-----------------------------------------------------------------------------
  145. / ThreadCoInitialize
  146. / ------------------
  147. / There is some thread local storage that indicates if we have called
  148. / CoInitialize. If CoInitialize has not yet been called, call it now.
  149. / Otherwise, do nothing.
  150. /
  151. / In:
  152. / -
  153. / Out:
  154. / HRESULT
  155. /----------------------------------------------------------------------------*/
  156. HRESULT
  157. ThreadCoInitialize(void)
  158. {
  159. HRESULT hr = S_OK;
  160. TraceEnter(TRACE_CORE, "ThreadCoInitialize");
  161. if (!TlsGetValue(g_tls))
  162. {
  163. TraceMsg("Calling CoInitialize");
  164. hr = CoInitialize(NULL);
  165. TlsSetValue(g_tls, (LPVOID)SUCCEEDED(hr));
  166. }
  167. TraceLeaveResult(hr);
  168. }
  169. /*-----------------------------------------------------------------------------
  170. / ThreadCoUninitialize
  171. / ------------------
  172. / There is some thread local storage that indicates if we have called
  173. / CoInitialize. If CoInitialize has been called, call CoUninitialize now.
  174. / Otherwise, do nothing.
  175. /
  176. / In:
  177. / -
  178. / Out:
  179. / HRESULT
  180. /----------------------------------------------------------------------------*/
  181. void
  182. ThreadCoUninitialize(void)
  183. {
  184. TraceEnter(TRACE_CORE, "ThreadCoUninitialize");
  185. if (TlsGetValue(g_tls))
  186. {
  187. TraceMsg("Calling CoUninitialize");
  188. CoUninitialize();
  189. TlsSetValue(g_tls, NULL);
  190. }
  191. TraceLeaveVoid();
  192. }
  193. //
  194. // Wrappers for delay-loading aclui.dll
  195. //
  196. char const c_szCreateSecurityPage[] = "CreateSecurityPage";
  197. char const c_szEditSecurity[] = "EditSecurity";
  198. typedef HPROPSHEETPAGE (WINAPI *PFN_CREATESECPAGE)(LPSECURITYINFO);
  199. typedef BOOL (WINAPI *PFN_EDITSECURITY)(HWND, LPSECURITYINFO);
  200. HRESULT
  201. _CreateSecurityPage(LPSECURITYINFO pSI, HPROPSHEETPAGE *phPage)
  202. {
  203. HRESULT hr = E_FAIL;
  204. if (NULL == g_hAclEditDll)
  205. g_hAclEditDll = LoadLibrary(c_szAclUI);
  206. if (g_hAclEditDll)
  207. {
  208. static PFN_CREATESECPAGE s_pfnCreateSecPage = NULL;
  209. if (NULL == s_pfnCreateSecPage)
  210. s_pfnCreateSecPage = (PFN_CREATESECPAGE)GetProcAddress(g_hAclEditDll, c_szCreateSecurityPage);
  211. if (s_pfnCreateSecPage)
  212. {
  213. hr = S_OK;
  214. *phPage = (*s_pfnCreateSecPage)(pSI);
  215. if (NULL == *phPage)
  216. hr = E_OUTOFMEMORY;
  217. }
  218. }
  219. return hr;
  220. }
  221. HRESULT
  222. _EditSecurity(HWND hwndOwner, LPSECURITYINFO pSI)
  223. {
  224. HRESULT hr = E_FAIL;
  225. if (NULL == g_hAclEditDll)
  226. g_hAclEditDll = LoadLibrary(c_szAclUI);
  227. if (g_hAclEditDll)
  228. {
  229. static PFN_EDITSECURITY s_pfnEditSecurity = NULL;
  230. if (NULL == s_pfnEditSecurity)
  231. s_pfnEditSecurity = (PFN_EDITSECURITY)GetProcAddress(g_hAclEditDll, c_szEditSecurity);
  232. if (s_pfnEditSecurity)
  233. {
  234. hr = S_OK;
  235. (*s_pfnEditSecurity)(hwndOwner, pSI);
  236. }
  237. }
  238. return hr;
  239. }