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.

190 lines
4.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: dynload.cxx
  7. //
  8. // Contents: Wrappers for functions exported by DLLs that are loaded only
  9. // when needed.
  10. //
  11. // Classes:
  12. //
  13. // Functions: FreeDynLoadLibs
  14. //
  15. // History: 07-Oct-96 AnirudhS Created.
  16. //
  17. //--------------------------------------------------------------------------
  18. #include "..\pch\headers.hxx"
  19. #pragma hdrstop
  20. #include <debug.hxx>
  21. #include "dynload.hxx"
  22. //
  23. // These typedefs must match the function prototypes in objbase.h
  24. //
  25. typedef LPVOID (STDAPICALLTYPE *PF_CoTaskMemAlloc) (ULONG cb);
  26. typedef LPVOID (STDAPICALLTYPE *PF_CoTaskMemRealloc) (LPVOID pv, ULONG cb);
  27. typedef void (STDAPICALLTYPE *PF_CoTaskMemFree) (LPVOID pv);
  28. //
  29. // Local functions
  30. //
  31. void
  32. LoadOleFunction(
  33. FARPROC * pfCurrentValue,
  34. LPCSTR pszFunctionName
  35. );
  36. //
  37. // Static data
  38. //
  39. HMODULE hOle32Dll;
  40. //+----------------------------------------------------------------------------
  41. //
  42. // Function: GetProcAddressInModule
  43. //
  44. // Synopsis: Gets the address of the specified function exported by the
  45. // specified module.
  46. //
  47. // Arguments: [pszFunction] - Name of the function desired.
  48. // [ptszModule] - Name of the module. This must already be loaded.
  49. //
  50. //-----------------------------------------------------------------------------
  51. FARPROC
  52. GetProcAddressInModule(
  53. LPCSTR pszFunction,
  54. LPCTSTR ptszModule
  55. )
  56. {
  57. HMODULE hMod = GetModuleHandle(ptszModule);
  58. schAssert(hMod != NULL);
  59. return (GetProcAddress(hMod, pszFunction));
  60. }
  61. //+----------------------------------------------------------------------------
  62. //
  63. // Function: LoadOleFunction
  64. //
  65. // Synopsis: Loads OLE32.DLL if it hasn't been loaded, and gets the address
  66. // of the specified function exported by it.
  67. //
  68. // Arguments: [pfCurrentValue] - Address where the function pointer will be
  69. // returned. If this is not NULL on entry it is assumed to
  70. // already point to the function.
  71. // [pszFunctionName] - Name of the function desired.
  72. //
  73. //-----------------------------------------------------------------------------
  74. void
  75. LoadOleFunction(
  76. FARPROC * pfCurrentValue,
  77. LPCSTR pszFunctionName
  78. )
  79. {
  80. if (*pfCurrentValue != NULL)
  81. {
  82. schAssert(hOle32Dll != NULL);
  83. return;
  84. }
  85. if (hOle32Dll != NULL
  86. || (hOle32Dll = LoadLibrary(TEXT("OLE32.DLL"))) != NULL)
  87. {
  88. *pfCurrentValue = GetProcAddress(hOle32Dll, pszFunctionName);
  89. }
  90. }
  91. //+----------------------------------------------------------------------------
  92. //
  93. // Function: FreeDynLoadLibs
  94. //
  95. // Synopsis: Unloads DLLs that have been dynamically loaded.
  96. //
  97. // Arguments: None.
  98. //
  99. // Notes: WARNING: This will make all the function pointers invalid.
  100. // Therefore, this function should only be called if this
  101. // module is being unloaded.
  102. //
  103. //-----------------------------------------------------------------------------
  104. void
  105. FreeDynLoadLibs(void)
  106. {
  107. if (hOle32Dll != NULL)
  108. {
  109. FreeLibrary(hOle32Dll);
  110. #if DBG == 1
  111. hOle32Dll = NULL;
  112. #endif
  113. }
  114. }
  115. //+----------------------------------------------------------------------------
  116. //
  117. // Function wrappers
  118. //
  119. //-----------------------------------------------------------------------------
  120. LPVOID
  121. WrapCoTaskMemAlloc(
  122. ULONG cb
  123. )
  124. {
  125. static PF_CoTaskMemAlloc pf_CoTaskMemAlloc;
  126. LoadOleFunction((FARPROC *) &pf_CoTaskMemAlloc, "CoTaskMemAlloc");
  127. if (pf_CoTaskMemAlloc != NULL)
  128. {
  129. return pf_CoTaskMemAlloc(cb);
  130. }
  131. else
  132. {
  133. return NULL;
  134. }
  135. }
  136. LPVOID
  137. WrapCoTaskMemRealloc(
  138. LPVOID pv,
  139. ULONG cb
  140. )
  141. {
  142. static PF_CoTaskMemRealloc pf_CoTaskMemRealloc;
  143. LoadOleFunction((FARPROC *) &pf_CoTaskMemRealloc, "CoTaskMemRealloc");
  144. if (pf_CoTaskMemRealloc != NULL)
  145. {
  146. return pf_CoTaskMemRealloc(pv, cb);
  147. }
  148. else
  149. {
  150. return NULL;
  151. }
  152. }
  153. void
  154. WrapCoTaskMemFree(
  155. LPVOID pv
  156. )
  157. {
  158. static PF_CoTaskMemFree pf_CoTaskMemFree;
  159. LoadOleFunction((FARPROC *) &pf_CoTaskMemFree, "CoTaskMemFree");
  160. if (pf_CoTaskMemFree != NULL)
  161. {
  162. pf_CoTaskMemFree(pv);
  163. }
  164. }