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.

77 lines
1.7 KiB

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