home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IPROCADR.HPP < prev    next >
C/C++ Source or Header  |  1993-10-22  |  8KB  |  163 lines

  1. #ifndef _IPROCADR_
  2. #define _IPROCADR_
  3. /*******************************************************************************
  4. * FILE NAME: iprocadr.hpp                                                      *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   This file contains the declaration(s) of the class(es):                    *
  8. *     IProcedureAddress - Dynamic link library entry point.                    *
  9. *                                                                              *
  10. * COPYRIGHT:                                                                   *
  11. *   Licensed Materials - Property of IBM                                       *
  12. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  13. *   All Rights Reserved                                                        *
  14. *   US Government Users Restricted Rights - Use, duplication, or               *
  15. *   disclosure                                                                 *
  16. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  17. *                                                                              *
  18. *******************************************************************************/
  19. #ifndef _IBASE_
  20.   #include <ibase.hpp>
  21. #endif
  22.  
  23. #ifndef _IRESLIB_
  24.   #include <ireslib.hpp>
  25. #endif
  26.  
  27. /*----------------------------------------------------------------------------*/
  28. /* Align classes on four byte boundary.                                       */
  29. /*----------------------------------------------------------------------------*/
  30. #pragma pack(4)
  31.  
  32. template < class PtrToFnType >
  33. class IProcedureAddress : public IBase {
  34. typedef IBase
  35.   Inherited;
  36. /*******************************************************************************
  37. * The IProcedureAddress class provides a wrapper for OS/2 dynamic-link         *
  38. * library (DLL) entry points obtained using DosLoadModule or DosGetProcAddr.   *
  39. * (DosLoadModule is used to do a runtime link to a DLL.  DosGetProcAddr is     *
  40. * used to acquire the runtime address of a function in a DLL.  When used       *
  41. * together, they allow the calling of code that was not known to the linker    *
  42. * when the executable module was built.)                                       *
  43. *                                                                              *
  44. * This class provides for:                                                     *
  45. *    - Constructing instances from entry point names (or ordinal entry point   *
  46. *      numbers) and DLL objects (or .dll names).                               *
  47. *    - Calling the entry point without having to deal with casting to and from *
  48. *      the generic "PFN" type supported by DosGetProcAddr.                     *
  49. *    - Managing (along with IDynamicLinkLibrary) the loading and freeing of    *
  50. *      the associated DLL file.                                                *
  51. *                                                                              *
  52. * Customization (Template Arguments)                                           *
  53. * Class IProcedureAddress is a template class instantiated with the template   *
  54. * arguments:                                                                   *
  55. *   class PtrToFnType - Type of the entry point to be loaded or called; this   *
  56. *                       must be a "pointer to function" type.                  *
  57. *                                                                              *
  58. * Example:                                                                     *
  59. *                                                                              *
  60. *  // "MyFnType" is pointer to function returning char *, ...                  *
  61. *  typedef char *(*MyFnType)( const char *, int, c );                          *
  62. *                                                                              *
  63. *  // myFn is dynamic entry point to function "myFunction" in DLL "myDLL".     *
  64. *  IProcedureAddress<MyFnType> myFn( "myFunction", "myDLL" );                  *
  65. *                                                                              *
  66. *  // Call dynamically loaded entry point:                                     *
  67. *  char *p = myFn( "argument 1", 2, '3' );                                     *
  68. *******************************************************************************/
  69. public:
  70. /*------------------------- Constructors ---------------------------------------
  71.   Two pieces of information are needed to construct an object of this class:
  72.      - The entry point.  This can be specified as either an entry point name
  73.        (const char *) or entry point ordinal (unsigned).
  74.      - The DLL from which the entry point is to be loaded.  This can be
  75.        specified either as a reference to an existing IDynamicLinkLibrary
  76.        object or simply by the DLL name.
  77. ------------------------------------------------------------------------------*/
  78.   IProcedureAddress ( const char          *entryPoint,
  79.                       const char          *dllName )
  80.     : dll( new IDynamicLinkLibrary( dllName ) ),
  81.       isOrdinal( false ),
  82.       name( entryPoint )
  83.     {
  84.     fnPtr = PtrToFnType( this->dll->procAddress( entryPoint ) );
  85.     }
  86.   IProcedureAddress ( const char          *entryPoint,
  87.                       IDynamicLinkLibrary &aDLL )
  88.     : dll( new IDynamicLinkLibrary( aDLL ) ),
  89.       isOrdinal( false ),
  90.       name( entryPoint )
  91.     {
  92.     fnPtr = PtrToFnType( this->dll->procAddress( entryPoint ) );
  93.     }
  94.  
  95.   IProcedureAddress ( unsigned long        ordinal,
  96.                       const char          *dllName )
  97.     : dll( new IDynamicLinkLibrary( dllName ) ),
  98.       isOrdinal( true ),
  99.       ord( ordinal )
  100.     {
  101.     fnPtr = PtrToFnType( this->dll->procAddress( ordinal ) );
  102.     }
  103.  
  104.   IProcedureAddress ( unsigned long        ordinal,
  105.                       IDynamicLinkLibrary &aDLL )
  106.     : dll( new IDynamicLinkLibrary( aDLL ) ),
  107.       isOrdinal( true ),
  108.       ord( ordinal )
  109.     {
  110.     fnPtr = PtrToFnType( this->dll->procAddress( ordinal ) );
  111.     }
  112.  
  113.   ~IProcedureAddress ( )
  114.     {
  115.     delete dll;
  116.     }
  117.  
  118. /*-------------------------------- Accessing -----------------------------------
  119. | The following functions provide means of getting an setting the accessible   |
  120. | attributes of instances of this class:                                       |
  121. |   operator PtrToFnType - This operator is provided to permit instances of    |
  122. |                          this template class to be used in the same way as   |
  123. |                          pointers-to-functions, which is like regular        |
  124. |                          function names.                                     |
  125. |   is32Bit              - Returns whether the entry point is 32-bit.          |
  126. ------------------------------------------------------------------------------*/
  127.   operator PtrToFnType ( ) const
  128.     {
  129.     return fnPtr;
  130.     }
  131.  
  132. Boolean
  133.   is32Bit ( ) const
  134.     {
  135.     if ( isOrdinal )
  136.       return dll->isEntryPoint32Bit( ord );
  137.     else
  138.       return dll->isEntryPoint32Bit( name );
  139.     }
  140.  
  141. private: /*------------------------ PRIVATE ----------------------------------*/
  142. PtrToFnType
  143.  fnPtr;
  144. IDynamicLinkLibrary
  145.  *dll;
  146. Boolean
  147.  isOrdinal;
  148. union
  149.   {
  150.   unsigned long
  151.     ord;
  152.   const char
  153.    *name;
  154.   };
  155. }; // class IProcedureAddress
  156.  
  157. /*----------------------------------------------------------------------------*/
  158. /* Resume compiler default packing.                                           */
  159. /*----------------------------------------------------------------------------*/
  160. #pragma pack()
  161.  
  162. #endif // _IPROCADR_
  163.