Source code of Windows XP (NT5)
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.

315 lines
8.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: demand.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. /*
  11. ** d e m a n d . c p p
  12. **
  13. ** Purpose: implement the loader functions for defer/demand -loaded libraries
  14. **
  15. ** Creators: jimsch, brimo, t-erikne
  16. ** Created: 5/15/97
  17. **
  18. ** Copyright (C) Microsoft Corp. 1997
  19. */
  20. #include "global.hxx"
  21. #include <dbgdef.h>
  22. // W4 stuff
  23. #pragma warning(disable: 4201) // nameless struct/union
  24. #pragma warning(disable: 4514) // unreferenced inline function removed
  25. #define IMPLEMENT_LOADER_FUNCTIONS
  26. #include "demand.h"
  27. #ifndef MAC
  28. ////////////////////////////////////////////////////////////////////////////
  29. //
  30. // Macros
  31. #define CRIT_GET_PROC_ADDR(h, fn, temp) \
  32. temp = (TYP_##fn) GetProcAddress(h, #fn); \
  33. if (temp) \
  34. VAR_##fn = temp; \
  35. else \
  36. { \
  37. AssertSz(VAR_##fn" failed to load"); \
  38. goto error; \
  39. }
  40. #define RESET(fn) \
  41. VAR_##fn = LOADER_##fn;
  42. #define GET_PROC_ADDR(h, fn) \
  43. VAR_##fn = (TYP_##fn) GetProcAddress(h, #fn); \
  44. // Assert(VAR_##fn != NULL);
  45. #define GET_PROC_ADDR3(h, fn, varname) \
  46. VAR_##varname = (TYP_##varname) GetProcAddress(h, #fn); \
  47. // Assert(VAR_##varname != NULL);
  48. ////////////////////////////////////////////////////////////////////////////
  49. //
  50. // Variables
  51. static HMODULE s_hCrypt = 0;
  52. static HMODULE s_hAdvApi = 0;
  53. static HMODULE s_hShell32 = 0;
  54. #if 0 // JLS
  55. static HMODULE s_hShLWAPI = 0;
  56. static HMODULE s_hURLMon = 0;
  57. static HMODULE s_hVersion = 0;
  58. static HMODULE s_hWinINET = 0;
  59. static HMODULE s_hComctl32 = 0;
  60. static HMODULE s_hPstoreC = 0;
  61. static HMODULE s_hMAPI = 0;
  62. static HMODULE s_hWSOCK = 0;
  63. static HMODULE s_hOLEAUT = 0;
  64. static HMODULE s_hKernel = 0;
  65. #endif // 0 // JLS
  66. #ifdef USE_CRITSEC
  67. static CRITICAL_SECTION cs = {0};
  68. #endif
  69. #ifdef DEBUG
  70. static BOOL s_fInit = FALSE;
  71. #endif
  72. ////////////////////////////////////////////////////////////////////////////
  73. //
  74. // Management functions
  75. void InitDemandLoadedLibs()
  76. {
  77. #ifdef USE_CRITSEC
  78. InitializeCriticalSection(&cs);
  79. #endif
  80. #ifdef DEBUG
  81. s_fInit = TRUE;
  82. #endif
  83. }
  84. void FreeDemandLoadedLibs()
  85. {
  86. #ifdef USE_CRITSEC
  87. EnterCriticalSection(&cs);
  88. #endif
  89. if (s_hCrypt)
  90. FreeLibrary(s_hCrypt);
  91. if (s_hAdvApi)
  92. FreeLibrary(s_hAdvApi);
  93. if (s_hShell32)
  94. FreeLibrary(s_hShell32);
  95. #if 0 //JLS
  96. if (s_hShLWAPI)
  97. FreeLibrary(s_hShLWAPI);
  98. if (s_hURLMon)
  99. FreeLibrary(s_hURLMon);
  100. if (s_hOLEAUT)
  101. FreeLibrary(s_hOLEAUT);
  102. if (s_hMAPI)
  103. FreeLibrary(s_hMAPI);
  104. if (s_hWSOCK)
  105. FreeLibrary(s_hWSOCK);
  106. if (s_hPstoreC)
  107. FreeLibrary(s_hPstoreC);
  108. if (s_hKernel)
  109. FreeLibrary(s_hKernel);
  110. #endif // JLS
  111. #ifdef DEBUG
  112. s_fInit = FALSE;
  113. #endif
  114. #ifdef USE_CRITSEC
  115. LeaveCriticalSection(&cs);
  116. DeleteCriticalSection(&cs);
  117. #endif
  118. }
  119. #if 0 // JLS
  120. ////////////////////////////////////////////////////////////////////////////
  121. //
  122. // Loader functions
  123. /* sample loader with critical proc addrs
  124. ** but not thread-safe
  125. BOOL DemandLoadFoo()
  126. {
  127. FARPROC fp;
  128. if (0 == g_hFoo)
  129. {
  130. g_hFoo = LoadLibrary("FOO.DLL");
  131. if (0 == g_hFoo)
  132. return FALSE;
  133. CRIT_GET_PROC_ADDR(NeededFunction1, fp);
  134. CRIT_GET_PROC_ADDR(NeededFunction2, fp);
  135. GET_PROC_ADDR(OptionalFunction);
  136. }
  137. return TRUE;
  138. error:
  139. FreeLibrary(g_hFoo);
  140. g_hFoo = NULL;
  141. RESET(NeededFunction1)
  142. RESET(NeededFunction2)
  143. RESET(OptionalFunction)
  144. return FALSE;
  145. }
  146. */
  147. #endif // 0
  148. BOOL DemandLoadCrypt32()
  149. {
  150. BOOL fRet = TRUE;
  151. // Assert(s_fInit);
  152. #ifdef USE_CRITSEC
  153. EnterCriticalSection(&cs);
  154. #endif
  155. if (0 == s_hCrypt) {
  156. s_hCrypt = LoadLibraryA("CRYPT32.DLL");
  157. // AssertSz((BOOL)s_hCrypt, TEXT("LoadLibrary failed on CRYPT32.DLL"));
  158. if (0 == s_hCrypt)
  159. fRet = FALSE;
  160. else {
  161. GET_PROC_ADDR(s_hCrypt, CertFreeCertificateContext)
  162. GET_PROC_ADDR(s_hCrypt, CertDuplicateCertificateContext)
  163. GET_PROC_ADDR(s_hCrypt, CertFindCertificateInStore)
  164. GET_PROC_ADDR(s_hCrypt, CertCloseStore)
  165. GET_PROC_ADDR(s_hCrypt, CertGetCertificateContextProperty)
  166. GET_PROC_ADDR(s_hCrypt, CertOpenStore)
  167. GET_PROC_ADDR(s_hCrypt, CertGetCertificateContextProperty)
  168. GET_PROC_ADDR(s_hCrypt, CertCompareCertificate)
  169. GET_PROC_ADDR(s_hCrypt, CertEnumCertificatesInStore)
  170. GET_PROC_ADDR(s_hCrypt, CryptDecodeObject)
  171. GET_PROC_ADDR(s_hCrypt, CertFindExtension)
  172. GET_PROC_ADDR(s_hCrypt, CryptFormatObject)
  173. GET_PROC_ADDR(s_hCrypt, CertNameToStrW)
  174. GET_PROC_ADDR(s_hCrypt, CertNameToStrA)
  175. GET_PROC_ADDR(s_hCrypt, CertStrToNameA)
  176. GET_PROC_ADDR(s_hCrypt, CertRDNValueToStrW)
  177. GET_PROC_ADDR(s_hCrypt, CertFindRDNAttr)
  178. GET_PROC_ADDR(s_hCrypt, CryptRegisterOIDFunction)
  179. GET_PROC_ADDR(s_hCrypt, CryptUnregisterOIDFunction)
  180. GET_PROC_ADDR(s_hCrypt, CertSetCertificateContextProperty)
  181. GET_PROC_ADDR(s_hCrypt, CertVerifyCTLUsage)
  182. GET_PROC_ADDR(s_hCrypt, CertGetIssuerCertificateFromStore)
  183. GET_PROC_ADDR(s_hCrypt, CertFreeCTLContext)
  184. GET_PROC_ADDR(s_hCrypt, CertAddEncodedCTLToStore)
  185. GET_PROC_ADDR(s_hCrypt, CryptMsgEncodeAndSignCTL)
  186. GET_PROC_ADDR(s_hCrypt, CertFindCTLInStore)
  187. GET_PROC_ADDR(s_hCrypt, CryptSignAndEncodeCertificate)
  188. GET_PROC_ADDR(s_hCrypt, CryptEncodeObject)
  189. GET_PROC_ADDR(s_hCrypt, CryptExportPublicKeyInfo)
  190. GET_PROC_ADDR(s_hCrypt, CertDuplicateStore)
  191. GET_PROC_ADDR(s_hCrypt, CertAddEncodedCertificateToStore);
  192. GET_PROC_ADDR(s_hCrypt, CertVerifyTimeValidity);
  193. GET_PROC_ADDR(s_hCrypt, CertFindSubjectInCTL);
  194. GET_PROC_ADDR(s_hCrypt, CertVerifySubjectCertificateContext);
  195. GET_PROC_ADDR(s_hCrypt, CertGetEnhancedKeyUsage);
  196. }
  197. }
  198. #ifdef USE_CRITSEC
  199. LeaveCriticalSection(&cs);
  200. #endif
  201. return fRet;
  202. }
  203. BOOL DemandLoadAdvApi32()
  204. {
  205. BOOL fRet = TRUE;
  206. // Assert(s_fInit);
  207. #ifdef USE_CRITSEC
  208. EnterCriticalSection(&cs);
  209. #endif
  210. if (0 == s_hAdvApi) {
  211. s_hAdvApi = LoadLibraryA("ADVAPI32.DLL");
  212. // AssertSz((BOOL)s_hAdvApi, TEXT("LoadLibrary failed on ADVAPI32.DLL"));
  213. if (0 == s_hAdvApi)
  214. fRet = FALSE;
  215. else {
  216. if (FIsWin95) {
  217. VAR_CryptAcquireContextW = MyCryptAcquireContextW;
  218. }
  219. else {
  220. GET_PROC_ADDR(s_hAdvApi, CryptAcquireContextW);
  221. }
  222. #if 0
  223. GET_PROC_ADDR(s_hAdvApi, CryptGetProvParam)
  224. GET_PROC_ADDR(s_hAdvApi, CryptReleaseContext)
  225. GET_PROC_ADDR(s_hAdvApi, CryptGenKey)
  226. GET_PROC_ADDR(s_hAdvApi, CryptDestroyKey)
  227. #endif // 0
  228. }
  229. }
  230. #ifdef USE_CRITSEC
  231. LeaveCriticalSection(&cs);
  232. #endif
  233. return fRet;
  234. }
  235. #ifndef WIN16
  236. BOOL DemandLoadUser32()
  237. {
  238. BOOL fRet = TRUE;
  239. // Assert(s_fInit);
  240. #ifdef USE_CRITSEC
  241. EnterCriticalSection(&cs);
  242. #endif
  243. #undef SendDlgitemMessageW
  244. #undef SetDlgItemTextW
  245. #undef GetDlgItemTextW
  246. #undef LoadStringW
  247. #undef FormatMessageW
  248. if (0 == s_hShell32) {
  249. if (FIsWin95) {
  250. VAR_SendDlgItemMessageW = MySendDlgItemMessageW;
  251. VAR_SetDlgItemTextW = MySetDlgItemTextW;
  252. VAR_GetDlgItemTextW = MyGetDlgItemTextW;
  253. VAR_LoadStringW = MyLoadStringW;
  254. VAR_FormatMessageW = MyFormatMessageW;
  255. VAR_WinHelpW = MyWinHelpW;
  256. }
  257. else {
  258. s_hShell32 = LoadLibraryA("kernel32.dll");
  259. GET_PROC_ADDR(s_hShell32, FormatMessageW);
  260. FreeLibrary(s_hShell32);
  261. s_hShell32 = LoadLibraryA("USER32.DLL");
  262. GET_PROC_ADDR(s_hShell32, SendDlgItemMessageW);
  263. GET_PROC_ADDR(s_hShell32, SetDlgItemTextW);
  264. GET_PROC_ADDR(s_hShell32, GetDlgItemTextW);
  265. GET_PROC_ADDR(s_hShell32, LoadStringW);
  266. GET_PROC_ADDR(s_hShell32, WinHelpW);
  267. }
  268. }
  269. #ifdef USE_CRITSEC
  270. LeaveCriticalSection(&cs);
  271. #endif
  272. return fRet;
  273. }
  274. #endif // !WIN16
  275. #endif // !MAC