Listing 4: Partial listing of class dll_object_ref

template
< class object >
class dll_object_ref
   {
      friend class dll_object_ptr< object >;

      // not shown: invariant()
      ...

   private:

      dll_object_ref
         ( dll_sentinel&         dll
         , std::string const& function_name )
         : m_dll( dll ), m_count( 1 ), m_obj( NULL )
         {
         #ifdef _WIN32
            typedef object* (WINAPI* p_instantiate)( );
         #elif defined( _DLSYM )
            typedef object* (* p_instantiate)( );
         #else
            #error Unsupported Platform.   Please port me
         #endif

         void* pvfunc = m_dll.find_func( function_name );
         if ( !_pvfunc )
            throw dll_exception
               ( "Could not find instantiation routine." );

         p_instantiate pfunc = 
            *reinterpret_cast<p_instantiate*>( &_pvfunc );
         m_obj = pfunc( );

         assert( invariant() );
         } 

      virtual ~dll_object_ref( )
         {
         assert( invariant() );
         m_obj->destroy();
         m_obj = NULL;
         } // ~dll_object_ref( )


      // not shown: functions ref and unref
      ...

   private: // data

      dll_sentinel   m_dll;
      object*          m_obj;
      unsigned long m_count;

   }; // class dll_object_ref< >