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.

176 lines
3.5 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: dynamiclib.h
  4. //
  5. // Module: Various Connection Manager modules (CMDIAL32.DLL, CMMON32.EXE, etc)
  6. //
  7. // Synopsis: Definition of CDynamicLibrary, a utility class that helps with
  8. // the dynamic loading of a library and the getting of proc
  9. // addresses from that library.
  10. //
  11. // Copyright (c) 1998-1999 Microsoft Corporation
  12. //
  13. // Author: fengsun Created 02/17/98
  14. //
  15. //+----------------------------------------------------------------------------
  16. #ifndef DYNAMICLIB_H
  17. #define DYNAMICLIB_H
  18. //
  19. // Define A_W as A for ansi and W for unicode
  20. //
  21. #ifdef UNICODE
  22. #define A_W "W"
  23. #else
  24. #define A_W "A"
  25. #endif // UNICODE
  26. //
  27. // Define LoadLibraryExU since not everyone is using the UAPI's yet
  28. //
  29. #ifndef _CMUTOA
  30. #ifdef UNICODE
  31. #define LoadLibraryExU LoadLibraryExW
  32. #else
  33. #define LoadLibraryExU LoadLibraryExA
  34. #endif // UNICODE
  35. #endif // _CMUTOA
  36. //+---------------------------------------------------------------------------
  37. //
  38. // class : CDynamicLibrary
  39. //
  40. // Synopsis: A class that will unload the library on destructor
  41. //
  42. // History: fengsun created 2/17/97
  43. //
  44. //----------------------------------------------------------------------------
  45. class CDynamicLibrary
  46. {
  47. public:
  48. CDynamicLibrary();
  49. CDynamicLibrary(const TCHAR* lpLibraryName);
  50. ~CDynamicLibrary();
  51. BOOL Load(const TCHAR* lpLibraryName);
  52. void Unload();
  53. BOOL IsLoaded() const;
  54. BOOL EnsureLoaded(const TCHAR* lpLibraryName);
  55. HINSTANCE GetInstance() const;
  56. FARPROC GetProcAddress(const char* lpProcName) const;
  57. protected:
  58. HINSTANCE m_hInst; // The instance handle returned by LoadLibrary
  59. };
  60. //
  61. // Constructor
  62. //
  63. inline CDynamicLibrary::CDynamicLibrary() :m_hInst(NULL) {}
  64. //
  65. // Constructor that calls LoadLibrary
  66. //
  67. inline CDynamicLibrary::CDynamicLibrary(const TCHAR* lpLibraryName)
  68. {
  69. m_hInst = NULL;
  70. Load(lpLibraryName);
  71. }
  72. //
  73. // Destructor that automatic FreeLibrary
  74. //
  75. inline CDynamicLibrary::~CDynamicLibrary()
  76. {
  77. Unload();
  78. }
  79. //
  80. // Call LoadLibrary
  81. //
  82. inline BOOL CDynamicLibrary::Load(const TCHAR* lpLibraryName)
  83. {
  84. MYDBGASSERT(m_hInst == NULL);
  85. MYDBGASSERT(lpLibraryName);
  86. CMTRACE1(TEXT("CDynamicLibrary - Loading library - %s"), lpLibraryName);
  87. m_hInst = LoadLibraryExU(lpLibraryName, NULL, 0);
  88. #ifdef DEBUG
  89. if (!m_hInst)
  90. {
  91. CMTRACE1(TEXT("CDynamicLibrary - LoadLibrary failed - GetLastError() = %u"), GetLastError());
  92. }
  93. #endif
  94. return m_hInst != NULL;
  95. }
  96. //
  97. // Call FreeLibrary
  98. //
  99. inline void CDynamicLibrary::Unload()
  100. {
  101. if (m_hInst)
  102. {
  103. FreeLibrary(m_hInst);
  104. m_hInst = NULL;
  105. }
  106. }
  107. //
  108. // Whether the library is successfully loaded
  109. //
  110. inline BOOL CDynamicLibrary::IsLoaded() const
  111. {
  112. return m_hInst != NULL;
  113. }
  114. //
  115. // Retrieve the instance handle
  116. //
  117. inline HINSTANCE CDynamicLibrary::GetInstance() const
  118. {
  119. return m_hInst;
  120. }
  121. //
  122. // call ::GetProcAddress on m_hInst
  123. //
  124. inline FARPROC CDynamicLibrary::GetProcAddress(const char* lpProcName) const
  125. {
  126. MYDBGASSERT(m_hInst);
  127. if (m_hInst)
  128. {
  129. return ::GetProcAddress(m_hInst, lpProcName);
  130. }
  131. return NULL;
  132. }
  133. //
  134. // Load the library, if not loaded yet,
  135. // Note, we do not check the consistence of lpLibraryName. Use caution with this function
  136. //
  137. inline BOOL CDynamicLibrary::EnsureLoaded(const TCHAR* lpLibraryName)
  138. {
  139. MYDBGASSERT(lpLibraryName);
  140. if (m_hInst == NULL)
  141. {
  142. m_hInst = LoadLibraryEx(lpLibraryName, NULL, 0);
  143. }
  144. return m_hInst != NULL;
  145. }
  146. #endif