Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

DLL.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // DLL.cpp
00003 // $Id: DLL.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:21 chad Exp $
00004 
00005 #include "ace/DLL.h"
00006 
00007 #include "ace/Log_Msg.h"
00008 #include "ace/ACE.h"
00009 #include "ace/DLL_Manager.h"
00010 
00011 ACE_RCSID(ace, DLL, "$Id: DLL.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:21 chad Exp $")
00012 
00013 // Default constructor. Also, by default, the object will be closed
00014 // before it is destroyed.
00015 
00016 ACE_DLL::ACE_DLL (int close_on_destruction)
00017   : open_mode_ (0),
00018     dll_name_ (0),
00019     close_on_destruction_ (close_on_destruction),
00020     dll_handle_ (0),
00021     error_ (0)
00022 {
00023   ACE_TRACE ("ACE_DLL::ACE_DLL (int)");
00024 }
00025 
00026 ACE_DLL::ACE_DLL (const ACE_DLL &rhs)
00027 {
00028   ACE_TRACE ("ACE_DLL::ACE_DLL (const ACE_DLL &)");
00029 
00030   // Have to do this since open() calls close()...
00031   this->dll_handle_ = 0;
00032   this->dll_name_ = 0;
00033   this->close_on_destruction_ = 1;
00034   this->error_ = 0;
00035 
00036   if (rhs.dll_name_)
00037     // This will automatically up the refcount.
00038     if (this->open (rhs.dll_name_, rhs.open_mode_, this->close_on_destruction_) != 0)
00039       ACE_ERROR ((LM_ERROR,
00040                   ACE_LIB_TEXT ("ACE_DLL::copy_ctor: error: %s\n"),
00041                   this->error ()));
00042 }
00043 
00044 // If the library name and the opening mode are specified than on
00045 // object creation the library is implicitly opened.
00046 
00047 ACE_DLL::ACE_DLL (const ACE_TCHAR *dll_name,
00048                   int open_mode,
00049                   int close_on_destruction)
00050   : open_mode_ (open_mode),
00051     dll_name_ (0),
00052     close_on_destruction_ (close_on_destruction),
00053     dll_handle_ (0),
00054     error_ (0)
00055 {
00056   ACE_TRACE ("ACE_DLL::ACE_DLL");
00057 
00058   if (this->open (dll_name, this->open_mode_, close_on_destruction) != 0)
00059     ACE_ERROR ((LM_ERROR,
00060                 ACE_LIB_TEXT ("ACE_DLL::open: error calling open: %s\n"),
00061                 this->error ()));
00062 }
00063 
00064 // The library is closed before the class gets destroyed depending on
00065 // the close_on_destruction value specified which is stored in
00066 // close_on_destruction_.
00067 
00068 ACE_DLL::~ACE_DLL (void)
00069 {
00070   ACE_TRACE ("ACE_DLL::~ACE_DLL");
00071 
00072   this->close ();
00073 }
00074 
00075 // This method opens the library based on the mode specified using the
00076 // ACE_SHLIB_HANDLE which is obtained on making the ACE_OS::dlopen call.
00077 // The default mode is:
00078 // RTLD_LAZY     Only references to data symbols are relocate when the
00079 //               object is first loaded.
00080 // The other modes include:
00081 //  RTLD_NOW     All necessary relocations are performed when the
00082 //               object is first loaded.
00083 //  RTLD_GLOBAL  The object symbols are made available for the
00084 //               relocation processing of any other object.
00085 
00086 int
00087 ACE_DLL::open (const ACE_TCHAR *dll_filename,
00088                int open_mode,
00089                int close_on_destruction)
00090 {
00091   ACE_TRACE ("ACE_DLL::open");
00092 
00093   return open_i (dll_filename, open_mode, close_on_destruction);
00094 }
00095 
00096 int
00097 ACE_DLL::open_i (const ACE_TCHAR *dll_filename,
00098                  int open_mode,
00099                  int close_on_destruction,
00100                  ACE_SHLIB_HANDLE handle)
00101 {
00102   ACE_TRACE ("ACE_DLL::open_i");
00103 
00104   this->error_ = 0;
00105 
00106   if (!dll_filename)
00107     ACE_ERROR_RETURN ((LM_ERROR,
00108                        ACE_LIB_TEXT ("ACE_DLL::open_i: dll_name: %s\n"),
00109                        this->dll_name_),
00110                       -1);
00111 
00112   if (this->dll_handle_)
00113     {
00114       // If we have a good handle and its the same name, just return.
00115       if (ACE_OS_String::strcmp (this->dll_name_, dll_filename) == 0)
00116         return 0;
00117       else
00118         this->close ();
00119     }
00120   if (!this->dll_name_)
00121     {
00122       this->dll_name_ = ACE::strnew (dll_filename);
00123     }
00124   this->open_mode_ = open_mode;
00125   this->close_on_destruction_ = close_on_destruction;
00126 
00127   this->dll_handle_ = ACE_DLL_Manager::instance()->open_dll (this->dll_name_,
00128                                                              this->open_mode_,
00129                                                              handle);
00130 
00131   if (!this->dll_handle_)
00132     this->error_ = 1;
00133 
00134   return this->error_ ? -1 : 0;
00135 }
00136 
00137 // The symbol refernce of the name specified is obtained.
00138 
00139 void *
00140 ACE_DLL::symbol (const ACE_TCHAR *sym_name, int ignore_errors)
00141 {
00142   ACE_TRACE ("ACE_DLL::symbol");
00143 
00144   this->error_ = 0;
00145 
00146   void *sym = this->dll_handle_->symbol (sym_name, ignore_errors);
00147 
00148   if (!sym)
00149     this->error_ = 1;
00150 
00151   return sym;
00152 }
00153 
00154 // The library is closed using the ACE_SHLIB_HANDLE obejct.  i.e. The
00155 // shared object is now disassociated form the current process.
00156 
00157 int
00158 ACE_DLL::close (void)
00159 {
00160   ACE_TRACE ("ACE_DLL::close");
00161   int retval = 0;
00162 
00163   if (this->close_on_destruction_ && this->dll_name_ &&
00164       (retval = ACE_DLL_Manager::instance ()->close_dll (this->dll_name_)) != 0)
00165     this->error_ = 1;
00166 
00167   // Even if close_dll() failed, go ahead and cleanup.
00168   this->dll_handle_ = 0;
00169   delete[] this->dll_name_;
00170   this->dll_name_ = 0;
00171   this->close_on_destruction_ = 0;
00172 
00173   return retval;
00174 }
00175 
00176 // This method is used return the last error of a library operation.
00177 
00178 ACE_TCHAR *
00179 ACE_DLL::error (void) const
00180 {
00181   ACE_TRACE ("ACE_DLL::error");
00182   if (this->error_)
00183     return ACE_const_cast (ACE_TCHAR *,
00184                            ACE_LIB_TEXT ("Error:  check log for details."));
00185 
00186   return 0;
00187 }
00188 
00189 // Return the handle to the user either temporarily or forever, thus
00190 // orphaning it. If 0 means the user wants the handle forever and if 1
00191 // means the user temporarily wants to take the handle.
00192 
00193 ACE_SHLIB_HANDLE
00194 ACE_DLL::get_handle (int become_owner) const
00195 {
00196   ACE_TRACE ("ACE_DLL::get_handle");
00197 
00198   ACE_SHLIB_HANDLE handle = ACE_SHLIB_INVALID_HANDLE;
00199 
00200   if (this->dll_handle_)
00201     handle = this->dll_handle_->get_handle (become_owner);
00202 
00203   return handle;
00204 }
00205 
00206 // Set the handle for the DLL. By default, the object will be closed
00207 // before it is destroyed.
00208 
00209 int
00210 ACE_DLL::set_handle (ACE_SHLIB_HANDLE handle,
00211                      int close_on_destruction)
00212 {
00213   ACE_TRACE ("ACE_DLL::set_handle");
00214 
00215   // Create a unique name.  Note that this name is only quaranteed
00216   // to be unique for the life of this object.
00217   ACE_TCHAR temp[ACE_UNIQUE_NAME_LEN];
00218   ACE_OS::unique_name (this, temp, ACE_UNIQUE_NAME_LEN);
00219 
00220   return this->open_i (temp, 1, close_on_destruction, handle);
00221 }

Generated on Mon Jun 16 11:19:37 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002