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.

94 lines
2.1 KiB

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