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.

100 lines
2.2 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("CERTMGR(DynamLnk.cpp)")
  14. #ifdef _DEBUG
  15. #ifndef ALPHA
  16. #define new DEBUG_NEW
  17. #endif
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. DynamicDLL::DynamicDLL(LPCWSTR 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 (apchFunctionNames);
  30. for (LPCSTR pchFunctionName = *apchFunctionNames;
  31. pchFunctionName;
  32. pchFunctionName = *(++apchFunctionNames) )
  33. {
  34. m_nNumFunctions++;
  35. ASSERT( !IsBadStringPtrA(pchFunctionName,MAX_PATH) );
  36. }
  37. }
  38. DynamicDLL::~DynamicDLL()
  39. {
  40. if ( m_apfFunctions )
  41. {
  42. delete m_apfFunctions;
  43. m_apfFunctions = NULL;
  44. }
  45. if ((HMODULE)-1 != m_hLibrary && NULL != m_hLibrary)
  46. {
  47. VERIFY( ::FreeLibrary( m_hLibrary ) );
  48. m_hLibrary = NULL;
  49. }
  50. }
  51. BOOL DynamicDLL::LoadFunctionPointers()
  52. {
  53. if ((HMODULE)-1 != m_hLibrary)
  54. return (NULL != m_hLibrary);
  55. m_hLibrary = ::LoadLibrary( m_ptchLibraryName );
  56. if ( !m_hLibrary)
  57. {
  58. // The library is not present
  59. return FALSE;
  60. }
  61. // let this throw an exception
  62. m_apfFunctions = new FARPROC[m_nNumFunctions];
  63. if ( m_apfFunctions )
  64. {
  65. for (INT i = 0; i < m_nNumFunctions; i++)
  66. {
  67. m_apfFunctions[i] = ::GetProcAddress( m_hLibrary, m_apchFunctionNames[i] );
  68. if ( NULL == m_apfFunctions[i] )
  69. {
  70. // The library is present but does not have all of the entrypoints
  71. VERIFY( ::FreeLibrary( m_hLibrary ) );
  72. m_hLibrary = NULL;
  73. return FALSE;
  74. }
  75. }
  76. }
  77. else
  78. return FALSE;
  79. return TRUE;
  80. }
  81. FARPROC DynamicDLL::QueryFunctionPtr(INT i) const
  82. {
  83. if ( 0 > i || m_nNumFunctions <= i || !m_apfFunctions || !m_apfFunctions[i] )
  84. {
  85. ASSERT( FALSE );
  86. return NULL;
  87. }
  88. return m_apfFunctions[i];
  89. }