Leaked source code of windows server 2003
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.

97 lines
2.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 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. if (NULL != 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 (NULL == 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. for (INT i = 0; i < m_nNumFunctions; i++)
  64. {
  65. m_apfFunctions[i] = ::GetProcAddress( m_hLibrary, m_apchFunctionNames[i] );
  66. if ( NULL == m_apfFunctions[i] )
  67. {
  68. // The library is present but does not have all of the entrypoints
  69. VERIFY( ::FreeLibrary( m_hLibrary ) );
  70. m_hLibrary = NULL;
  71. return FALSE;
  72. }
  73. }
  74. return TRUE;
  75. }
  76. FARPROC DynamicDLL::QueryFunctionPtr(INT i) const
  77. {
  78. if ( 0 > i || m_nNumFunctions <= i || NULL == m_apfFunctions || NULL == m_apfFunctions[i] )
  79. {
  80. ASSERT( FALSE );
  81. return NULL;
  82. }
  83. return m_apfFunctions[i];
  84. }