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.

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