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.

280 lines
12 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997
  5. //
  6. // File:
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History:
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "private.h"
  18. // #include <winsock.h>
  19. #ifdef DEBUG
  20. extern DWORD g_dwThreadDllMain;
  21. #endif
  22. #pragma warning(disable:4229) // No warnings when modifiers used on data
  23. //----------------------------------------------------------------------------
  24. // Delay loading mechanism. [Stolen from shdocvw.]
  25. //
  26. // This allows you to write code as if you are
  27. // calling implicitly linked APIs, and yet have these APIs really be
  28. // explicitly linked. You can reduce the initial number of DLLs that
  29. // are loaded (load on demand) using this technique.
  30. //
  31. // Use the following macros to indicate which APIs/DLLs are delay-linked
  32. // and -loaded.
  33. //
  34. // DELAY_LOAD
  35. // DELAY_LOAD_HRESULT
  36. // DELAY_LOAD_SAFEARRAY
  37. // DELAY_LOAD_UINT
  38. // DELAY_LOAD_INT
  39. // DELAY_LOAD_VOID
  40. //
  41. // Use these macros for APIs that are exported by ordinal only.
  42. //
  43. // DELAY_LOAD_ORD
  44. // DELAY_LOAD_ORD_VOID
  45. //
  46. // Use these macros for APIs that only exist on the integrated-shell
  47. // installations (i.e., a new shell32 is on the system).
  48. //
  49. // DELAY_LOAD_SHELL
  50. // DELAY_LOAD_SHELL_HRESULT
  51. // DELAY_LOAD_SHELL_VOID
  52. //
  53. //----------------------------------------------------------------------------
  54. #define ENSURE_LOADED(_hinst, _dll, pszfn) (_hinst ? _hinst : (_hinst = LoadLibrary(#_dll)))
  55. #define DELAY_LOAD_ERR(_hinst, _dll, _ret, _fn, _args, _nargs, _err) \
  56. static _ret (* __stdcall _pfn##_fn) _args = NULL; \
  57. _ret __stdcall _fn _args \
  58. { \
  59. Assert(g_dwThreadDllMain != GetCurrentThreadId()); \
  60. if (!ENSURE_LOADED(_hinst, _dll, #_fn)) \
  61. { \
  62. AssertMsg(_hinst != NULL, "LoadLibrary failed on " ## #_dll); \
  63. return (_ret)_err; \
  64. } \
  65. if (_pfn##_fn == NULL) \
  66. { \
  67. *(FARPROC*)&(_pfn##_fn) = GetProcAddress(_hinst, #_fn); \
  68. AssertMsg(_pfn##_fn != NULL, "GetProcAddress failed on " ## #_fn); \
  69. if (_pfn##_fn == NULL) \
  70. return (_ret)_err; \
  71. } \
  72. return _pfn##_fn _nargs; \
  73. }
  74. #define DELAY_LOAD_VOID(_hinst, _dll, _fn, _args, _nargs) \
  75. void __stdcall _fn _args \
  76. { \
  77. static void (* __stdcall _pfn##_fn) _args = NULL; \
  78. Assert(g_dwThreadDllMain != GetCurrentThreadId()); \
  79. if (!ENSURE_LOADED(_hinst, _dll, #_fn)) \
  80. { \
  81. AssertMsg(_hinst != NULL, "LoadLibrary failed on " ## #_dll); \
  82. return; \
  83. } \
  84. if (_pfn##_fn == NULL) \
  85. { \
  86. *(FARPROC*)&(_pfn##_fn) = GetProcAddress(_hinst, #_fn); \
  87. AssertMsg(_pfn##_fn != NULL, "GetProcAddress failed on " ## #_fn); \
  88. if (_pfn##_fn == NULL) \
  89. return; \
  90. } \
  91. _pfn##_fn _nargs; \
  92. }
  93. #define DELAY_LOAD(_hinst, _dll, _ret, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, _ret, _fn, _args, _nargs, 0)
  94. #define DELAY_LOAD_HRESULT(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, HRESULT, _fn, _args, _nargs, E_FAIL)
  95. #define DELAY_LOAD_SAFEARRAY(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, SAFEARRAY *, _fn, _args, _nargs, NULL)
  96. #define DELAY_LOAD_DWORD(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, DWORD, _fn, _args, _nargs, 0)
  97. #define DELAY_LOAD_UINT(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, UINT, _fn, _args, _nargs, 0)
  98. #define DELAY_LOAD_INT(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, INT, _fn, _args, _nargs, 0)
  99. #define DELAY_LOAD_UCHAR(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, unsigned char *, _fn, _args, _nargs, 0)
  100. #define DELAY_LOAD_ULONG(_hinst, _dll, _fn, _args, _nargs) DELAY_LOAD_ERR(_hinst, _dll, ULONG, _fn, _args, _nargs, 0)
  101. #define DELAY_LOAD_ORD_ERR(_hinst, _dll, _ret, _fn, _ord, _args, _nargs, _err) \
  102. _ret __stdcall _fn _args \
  103. { \
  104. static _ret (* __stdcall _pfn##_fn) _args = NULL; \
  105. Assert(g_dwThreadDllMain != GetCurrentThreadId()); \
  106. if (!ENSURE_LOADED(_hinst, _dll, "(ordinal " ## #_ord ## ")")) \
  107. { \
  108. TraceMsg(TF_ERROR, "LoadLibrary failed on " ## #_dll); \
  109. return (_ret)_err; \
  110. } \
  111. if (_pfn##_fn == NULL) \
  112. { \
  113. *(FARPROC*)&(_pfn##_fn) = GetProcAddress(_hinst, (LPSTR) _ord); \
  114. \
  115. /* GetProcAddress always returns non-NULL, even for bad ordinals. \
  116. But do the check anyways... */ \
  117. \
  118. if (_pfn##_fn == NULL) \
  119. return (_ret)_err; \
  120. } \
  121. return _pfn##_fn _nargs; \
  122. }
  123. #define DELAY_LOAD_ORD_VOID(_hinst, _dll, _fn, _ord, _args, _nargs) \
  124. void __stdcall _fn _args \
  125. { \
  126. static void (* __stdcall _pfn##_fn) _args = NULL; \
  127. Assert(g_dwThreadDllMain != GetCurrentThreadId()); \
  128. if (!ENSURE_LOADED(_hinst, _dll, "(ordinal " ## #_ord ## ")")) \
  129. { \
  130. TraceMsg(TF_ERROR, "LoadLibrary failed on " ## #_dll); \
  131. return; \
  132. } \
  133. if (_pfn##_fn == NULL) \
  134. { \
  135. *(FARPROC*)&(_pfn##_fn) = GetProcAddress(_hinst, (LPSTR) _ord); \
  136. \
  137. /* GetProcAddress always returns non-NULL, even for bad ordinals. \
  138. But do the check anyways... */ \
  139. \
  140. if (_pfn##_fn == NULL) \
  141. return; \
  142. } \
  143. _pfn##_fn _nargs; \
  144. }
  145. #define DELAY_LOAD_ORD(_hinst, _dll, _ret, _fn, _ord, _args, _nargs) DELAY_LOAD_ORD_ERR(_hinst, _dll, _ret, _fn, _ord, _args, _nargs, 0)
  146. //
  147. // And now the DLLs which are delay loaded
  148. //
  149. // --------- OLEAUT32.DLL ---------------
  150. HINSTANCE g_hinstOLEAUT32 = NULL;
  151. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, RegisterTypeLib,
  152. (ITypeLib *ptlib, OLECHAR *szFullPath, OLECHAR *szHelpDir),
  153. (ptlib, szFullPath, szHelpDir));
  154. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, LoadTypeLib,
  155. (const OLECHAR *szFile, ITypeLib **pptlib), (szFile, pptlib));
  156. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, SetErrorInfo,
  157. (unsigned long dwReserved, IErrorInfo*perrinfo), (dwReserved, perrinfo));
  158. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, LoadRegTypeLib,
  159. (REFGUID rguid, WORD wVerMajor, WORD wVerMinor, LCID lcid, ITypeLib **pptlib),
  160. (rguid, wVerMajor, wVerMinor, lcid, pptlib));
  161. #undef VariantClear
  162. #undef VariantCopy
  163. // Use QuickVariantInit instead!
  164. //DELAY_LOAD_VOID(g_hinstOLEAUT32, OLEAUT32.DLL, VariantInit,
  165. // (VARIANTARG *pvarg), (pvarg));
  166. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, VariantClear,
  167. (VARIANTARG *pvarg), (pvarg));
  168. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, VariantCopy,
  169. (VARIANTARG *pvargDest, VARIANTARG *pvargSrc), (pvargDest, pvargSrc));
  170. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, VariantCopyInd,
  171. (VARIANT * pvarDest, VARIANTARG * pvargSrc), (pvarDest, pvargSrc));
  172. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, VariantChangeType,
  173. (VARIANTARG *pvargDest, VARIANTARG *pvarSrc, unsigned short wFlags, VARTYPE vt),
  174. (pvargDest, pvarSrc, wFlags, vt));
  175. DELAY_LOAD(g_hinstOLEAUT32, OLEAUT32.DLL, BSTR, SysAllocStringLen,
  176. (const OLECHAR*pch, unsigned int i), (pch, i));
  177. DELAY_LOAD(g_hinstOLEAUT32, OLEAUT32.DLL, BSTR, SysAllocString,
  178. (const OLECHAR*pch), (pch));
  179. DELAY_LOAD(g_hinstOLEAUT32, OLEAUT32.DLL, BSTR, SysAllocStringByteLen,
  180. (LPCSTR psz, UINT i), (psz, i));
  181. DELAY_LOAD_UINT(g_hinstOLEAUT32, OLEAUT32.DLL, SysStringByteLen,
  182. (BSTR bstr), (bstr));
  183. DELAY_LOAD_VOID(g_hinstOLEAUT32, OLEAUT32.DLL, SysFreeString, (BSTR bs), (bs));
  184. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, DispGetIDsOfNames,
  185. (ITypeInfo*ptinfo, OLECHAR **rgszNames, UINT cNames, DISPID*rgdispid),
  186. (ptinfo, rgszNames, cNames, rgdispid));
  187. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, CreateErrorInfo,
  188. (ICreateErrorInfo **pperrinfo), (pperrinfo));
  189. DELAY_LOAD_SAFEARRAY(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayCreateVector,
  190. (VARTYPE vt, long iBound, ULONG cElements), (vt, iBound, cElements) );
  191. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayAccessData,
  192. (SAFEARRAY * psa, void HUGEP** ppvData), (psa, ppvData));
  193. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayUnaccessData,
  194. (SAFEARRAY * psa), (psa) );
  195. DELAY_LOAD_SAFEARRAY(g_hinstOLEAUT32, OLEAUT32, SafeArrayCreate,
  196. (VARTYPE vt, UINT cDims, SAFEARRAYBOUND * rgsabound), (vt, cDims, rgsabound));
  197. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32, SafeArrayPutElement,
  198. (SAFEARRAY * psa, LONG * rgIndices, void * pv), (psa, rgIndices, pv));
  199. DELAY_LOAD_UINT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayGetElemsize,
  200. (SAFEARRAY * psa), (psa) );
  201. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayGetLBound,
  202. (SAFEARRAY * psa, UINT nDim, LONG * plLBound),
  203. (psa,nDim,plLBound));
  204. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayGetUBound,
  205. (SAFEARRAY * psa, UINT nDim, LONG * plUBound),
  206. (psa,nDim,plUBound));
  207. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayGetElement,
  208. (SAFEARRAY * psa, LONG * rgIndices, void * pv), (psa, rgIndices, pv));
  209. DELAY_LOAD_UINT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayGetDim,
  210. (SAFEARRAY * psa), (psa));
  211. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayLock,
  212. (SAFEARRAY * psa), (psa));
  213. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayUnlock,
  214. (SAFEARRAY * psa), (psa));
  215. DELAY_LOAD_UINT(g_hinstOLEAUT32, OLEAUT32.DLL, SysStringLen,
  216. (BSTR bstr), (bstr));
  217. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, SafeArrayDestroy,
  218. (SAFEARRAY * psa), (psa));
  219. DELAY_LOAD_INT(g_hinstOLEAUT32, OLEAUT32.DLL, DosDateTimeToVariantTime,
  220. (USHORT wDosDate, USHORT wDosTime, DOUBLE * pvtime), (wDosDate, wDosTime, pvtime));
  221. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, VarI4FromStr,
  222. (OLECHAR FAR * strIn, LCID lcid, DWORD dwFlags, LONG * plOut), (strIn, lcid, dwFlags, plOut));
  223. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, VarUI4FromStr,
  224. (OLECHAR FAR * strIn, LCID lcid, DWORD dwFlags, ULONG * pulOut), (strIn, lcid, dwFlags, pulOut));
  225. DELAY_LOAD_HRESULT(g_hinstOLEAUT32, OLEAUT32.DLL, VarR8FromDec,
  226. (DECIMAL *pdecIn, double *pdbOut), (pdecIn, pdbOut));
  227. #pragma warning(default:4229)