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.

167 lines
3.5 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // ExplicitLink.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // This file describes the classes ExplicitLinkBase and ExplicitLink<T>.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 01/12/1998 Original version.
  16. // 06/22/1998 Misc. bug fixes.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #ifndef _EXPLICITLINK_H_
  20. #define _EXPLICITLINK_H_
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //
  23. // CLASS
  24. //
  25. // ExplicitLinkBase
  26. //
  27. // DESCRIPTION
  28. //
  29. // This class provides a wrapper around a function pointer that is
  30. // explicitly loaded from a DLL at run-time.
  31. //
  32. ///////////////////////////////////////////////////////////////////////////////
  33. class ExplicitLinkBase
  34. {
  35. public:
  36. ExplicitLinkBase() throw ()
  37. : module(NULL), proc(NULL)
  38. { }
  39. ExplicitLinkBase(PCWSTR moduleName, PCSTR procName) throw ()
  40. {
  41. loadInternal(moduleName, procName);
  42. }
  43. ExplicitLinkBase(const ExplicitLinkBase& elb) throw ()
  44. {
  45. copyInternal(elb);
  46. }
  47. ExplicitLinkBase& operator=(const ExplicitLinkBase& elb) throw ()
  48. {
  49. copy(elb);
  50. return *this;
  51. }
  52. ~ExplicitLinkBase() throw ()
  53. {
  54. free();
  55. }
  56. void copy(const ExplicitLinkBase& original) throw ()
  57. {
  58. // Protect against self-assignment.
  59. if (this != &original)
  60. {
  61. free();
  62. copyInternal(original);
  63. }
  64. }
  65. void free() throw ()
  66. {
  67. if (module)
  68. {
  69. FreeLibrary(module);
  70. module = NULL;
  71. proc = NULL;
  72. }
  73. }
  74. bool isValid() const throw ()
  75. {
  76. return proc != NULL;
  77. }
  78. bool load(PCWSTR moduleName, PCSTR procName) throw ()
  79. {
  80. free();
  81. loadInternal(moduleName, procName);
  82. return proc != NULL;
  83. }
  84. FARPROC operator()() const throw ()
  85. {
  86. return proc;
  87. }
  88. protected:
  89. HINSTANCE module; // The DLL containing the function.
  90. FARPROC proc; // Pointer to the function.
  91. // Copy without free.
  92. void copyInternal(const ExplicitLinkBase& original) throw ()
  93. {
  94. WCHAR filename[MAX_PATH + 1];
  95. if (original.module == NULL ||
  96. GetModuleFileNameW(original.module, filename, MAX_PATH + 1) == 0)
  97. {
  98. module = NULL;
  99. }
  100. else
  101. {
  102. module = LoadLibraryW(filename);
  103. }
  104. proc = module ? original.proc : NULL;
  105. }
  106. // Load without free.
  107. void loadInternal(PCWSTR moduleName, PCSTR procName) throw ()
  108. {
  109. module = LoadLibraryW(moduleName);
  110. proc = module ? GetProcAddress(module, procName) : NULL;
  111. }
  112. };
  113. ///////////////////////////////////////////////////////////////////////////////
  114. //
  115. // CLASS
  116. //
  117. // ExplicitLink<T>
  118. //
  119. // DESCRIPTION
  120. //
  121. // This class extends ExplicitLinkBase to provide type safety. The template
  122. // parameter is the actual type of the function pointer.
  123. //
  124. ///////////////////////////////////////////////////////////////////////////////
  125. template <class Pfn>
  126. class ExplicitLink : public ExplicitLinkBase
  127. {
  128. public:
  129. ExplicitLink() throw () { }
  130. ExplicitLink(PCWSTR moduleName, PCSTR procName) throw ()
  131. : ExplicitLinkBase(moduleName, procName)
  132. { }
  133. Pfn operator()() const throw ()
  134. {
  135. return (Pfn)proc;
  136. }
  137. };
  138. #endif _EXPLICITLINK_H_