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.

101 lines
2.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1998
  6. //
  7. // File: dynamlnk.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. // DynamLnk.cpp : base class for DLLs which are loaded only when needed
  11. #include "stdafx.h"
  12. //#include "safetemp.h"
  13. #include "DynamLnk.h"
  14. //#include "macros.h"
  15. //USE_HANDLE_MACROS("TAPISNAP(DynamLnk.cpp)")
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. DynamicDLL::DynamicDLL(LPCTSTR ptchLibraryName, LPCSTR* apchFunctionNames)
  22. : m_hLibrary( (HMODULE)-1 ),
  23. m_apfFunctions( NULL ),
  24. m_ptchLibraryName( ptchLibraryName ),
  25. m_apchFunctionNames( apchFunctionNames ),
  26. m_nNumFunctions( 0 )
  27. {
  28. ASSERT( !IsBadStringPtr(m_ptchLibraryName,MAX_PATH) );
  29. ASSERT( NULL != apchFunctionNames );
  30. for (LPCSTR pchFunctionName = *apchFunctionNames;
  31. NULL != pchFunctionName;
  32. pchFunctionName = *(++apchFunctionNames) )
  33. {
  34. m_nNumFunctions++;
  35. ASSERT( !IsBadStringPtrA(pchFunctionName,MAX_PATH) );
  36. }
  37. }
  38. DynamicDLL::~DynamicDLL()
  39. {
  40. Unload();
  41. }
  42. BOOL DynamicDLL::LoadFunctionPointers()
  43. {
  44. if ((HMODULE)-1 != m_hLibrary)
  45. return (NULL != m_hLibrary);
  46. m_hLibrary = ::LoadLibrary( m_ptchLibraryName );
  47. if (NULL == m_hLibrary)
  48. {
  49. // The library is not present
  50. return FALSE;
  51. }
  52. // let this throw an exception
  53. m_apfFunctions = new FARPROC[m_nNumFunctions];
  54. for (INT i = 0; i < m_nNumFunctions; i++)
  55. {
  56. m_apfFunctions[i] = ::GetProcAddress( m_hLibrary, m_apchFunctionNames[i] );
  57. if ( NULL == m_apfFunctions[i] )
  58. {
  59. // The library is present but does not have all of the entrypoints
  60. VERIFY( ::FreeLibrary( m_hLibrary ) );
  61. m_hLibrary = NULL;
  62. return FALSE;
  63. }
  64. }
  65. return TRUE;
  66. }
  67. FARPROC DynamicDLL::QueryFunctionPtr(INT i) const
  68. {
  69. if ( 0 > i || m_nNumFunctions <= i || NULL == m_apfFunctions || NULL == m_apfFunctions[i] )
  70. {
  71. ASSERT( FALSE );
  72. return NULL;
  73. }
  74. return m_apfFunctions[i];
  75. }
  76. void DynamicDLL::Unload()
  77. {
  78. if (NULL != m_apfFunctions)
  79. {
  80. delete m_apfFunctions;
  81. m_apfFunctions = NULL;
  82. }
  83. if ((HMODULE)-1 != m_hLibrary && NULL != m_hLibrary)
  84. {
  85. VERIFY( ::FreeLibrary( m_hLibrary ) );
  86. m_hLibrary = (HMODULE) -1;
  87. }
  88. }