DLL.cpp

Go to the documentation of this file.
00001 // DLL.cpp,v 4.57 2006/04/28 18:04:32 jeliazkov_i Exp
00002 
00003 #include "ace/DLL.h"
00004 
00005 #include "ace/Log_Msg.h"
00006 #include "ace/ACE.h"
00007 #include "ace/DLL_Manager.h"
00008 #include "ace/OS_NS_string.h"
00009 #include "ace/OS_NS_dlfcn.h"
00010 #include "ace/OS_NS_Thread.h"
00011 
00012 ACE_RCSID(ace, DLL, "DLL.cpp,v 4.57 2006/04/28 18:04:32 jeliazkov_i Exp")
00013 
00014   ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 // Default constructor. Also, by default, the object will be closed
00017 // before it is destroyed.
00018 
00019 ACE_DLL::ACE_DLL (int close_handle_on_destruction)
00020   : open_mode_ (0),
00021     dll_name_ (0),
00022     close_handle_on_destruction_ (close_handle_on_destruction),
00023     dll_handle_ (0),
00024     error_ (0)
00025 {
00026   ACE_TRACE ("ACE_DLL::ACE_DLL (int)");
00027 }
00028 
00029 ACE_DLL::ACE_DLL (const ACE_DLL &rhs)
00030   : open_mode_ (0),
00031     dll_name_ (0),
00032     close_handle_on_destruction_ (0),
00033     dll_handle_ (0),
00034     error_ (0)
00035 {
00036   ACE_TRACE ("ACE_DLL::ACE_DLL (const ACE_DLL &)");
00037 
00038   if (rhs.dll_name_
00039       // This will automatically up the refcount.
00040       && this->open (rhs.dll_name_,
00041                      rhs.open_mode_,
00042                      rhs.close_handle_on_destruction_) != 0
00043       && ACE::debug ())
00044     ACE_ERROR ((LM_ERROR,
00045     ACE_LIB_TEXT ("ACE_DLL::copy_ctor: error: %s\n"),
00046     this->error ()));
00047 }
00048 
00049 // Assignment operator
00050 
00051 const ACE_DLL &
00052 ACE_DLL::operator= (const ACE_DLL &rhs)
00053 {
00054   ACE_TRACE ("ACE_DLL::operator= (const ACE_DLL &)");
00055 
00056   open_mode_ = 0;
00057   dll_name_ = 0;
00058   close_handle_on_destruction_=0;
00059   dll_handle_=0;
00060   error_=0;
00061 
00062   if (rhs.dll_name_
00063       // This will automatically up the refcount and initialize *this
00064       && this->open (rhs.dll_name_,
00065                      rhs.open_mode_,
00066                      rhs.close_handle_on_destruction_) != 0
00067       && ACE::debug ())
00068     ACE_ERROR ((LM_ERROR,
00069     ACE_LIB_TEXT ("ACE_DLL::operator=: error: %s\n"),
00070     this->error ()));
00071 
00072   return *this;
00073 }
00074 
00075 
00076 // If the library name and the opening mode are specified than on
00077 // object creation the library is implicitly opened.
00078 
00079 ACE_DLL::ACE_DLL (const ACE_TCHAR *dll_name,
00080                   int open_mode,
00081                   int close_handle_on_destruction)
00082   : open_mode_ (open_mode),
00083     dll_name_ (0),
00084     close_handle_on_destruction_ (close_handle_on_destruction),
00085     dll_handle_ (0),
00086     error_ (0)
00087 {
00088   ACE_TRACE ("ACE_DLL::ACE_DLL");
00089 
00090   if (this->open (dll_name, this->open_mode_, close_handle_on_destruction) != 0
00091       && ACE::debug ())
00092     ACE_ERROR ((LM_ERROR,
00093                 ACE_LIB_TEXT ("ACE_DLL::open: error calling open: %s\n"),
00094                 this->error ()));
00095 }
00096 
00097 // The library is closed before the class gets destroyed depending on
00098 // the close_handle_on_destruction value specified which is stored in
00099 // close_handle_on_destruction_.
00100 
00101 ACE_DLL::~ACE_DLL (void)
00102 {
00103   ACE_TRACE ("ACE_DLL::~ACE_DLL");
00104 
00105   this->close ();
00106 
00107   // Normally delete()d in ACE_DLL::close().  However, that may not
00108   // occur if full ACE_DLL initialization is interrupted due to errors
00109   // (e.g. attempting to open a DSO/DLL that does not exist).  Make
00110   // sure this->dll_name_ is deallocated.
00111   delete [] this->dll_name_;
00112 }
00113 
00114 // This method opens the library based on the mode specified using the
00115 // ACE_SHLIB_HANDLE which is obtained on making the ACE_OS::dlopen call.
00116 // The default mode is:
00117 // RTLD_LAZY     Only references to data symbols are relocate when the
00118 //               object is first loaded.
00119 // The other modes include:
00120 //  RTLD_NOW     All necessary relocations are performed when the
00121 //               object is first loaded.
00122 //  RTLD_GLOBAL  The object symbols are made available for the
00123 //               relocation processing of any other object.
00124 
00125 int
00126 ACE_DLL::open (const ACE_TCHAR *dll_filename,
00127                int open_mode,
00128                int close_handle_on_destruction)
00129 {
00130   ACE_TRACE ("ACE_DLL::open");
00131 
00132   return open_i (dll_filename, open_mode, close_handle_on_destruction);
00133 }
00134 
00135 int
00136 ACE_DLL::open_i (const ACE_TCHAR *dll_filename,
00137                  int open_mode,
00138                  int close_handle_on_destruction,
00139                  ACE_SHLIB_HANDLE handle)
00140 {
00141   ACE_TRACE ("ACE_DLL::open_i");
00142 
00143   this->error_ = 0;
00144 
00145   if (!dll_filename)
00146     {
00147       if (ACE::debug ())
00148         ACE_ERROR ((LM_ERROR,
00149                     ACE_LIB_TEXT ("ACE_DLL::open_i: dll_name is %s\n"),
00150                     this->dll_name_ == 0 ? ACE_LIB_TEXT ("(null)")
00151         : this->dll_name_));
00152       return -1;
00153     }
00154 
00155   if (this->dll_handle_)
00156     {
00157       // If we have a good handle and its the same name, just return.
00158       if (ACE_OS::strcmp (this->dll_name_, dll_filename) == 0)
00159         return 0;
00160       else
00161         this->close ();
00162     }
00163 
00164   if (!this->dll_name_)
00165     this->dll_name_ = ACE::strnew (dll_filename);
00166 
00167   this->open_mode_ = open_mode;
00168   this->close_handle_on_destruction_ = close_handle_on_destruction;
00169 
00170   this->dll_handle_ = ACE_DLL_Manager::instance()->open_dll (this->dll_name_,
00171                                                              this->open_mode_,
00172                                                              handle);
00173 
00174   if (!this->dll_handle_)
00175     this->error_ = 1;
00176 
00177   return this->error_ ? -1 : 0;
00178 }
00179 
00180 // The symbol refernce of the name specified is obtained.
00181 
00182 void *
00183 ACE_DLL::symbol (const ACE_TCHAR *sym_name, int ignore_errors)
00184 {
00185   ACE_TRACE ("ACE_DLL::symbol");
00186 
00187   this->error_ = 0;
00188 
00189   void *sym = 0;
00190   if (this->dll_handle_)
00191     sym = this->dll_handle_->symbol (sym_name, ignore_errors);
00192 
00193   if (!sym)
00194     this->error_ = 1;
00195 
00196   return sym;
00197 }
00198 
00199 // The library is closed using the ACE_SHLIB_HANDLE object, i.e., the
00200 // shared object is now disassociated form the current process.
00201 
00202 int
00203 ACE_DLL::close (void)
00204 {
00205   ACE_TRACE ("ACE_DLL::close");
00206 
00207   int retval = 0;
00208 
00209   if (this->dll_handle_
00210       && this->close_handle_on_destruction_
00211       && this->dll_name_
00212       && (retval = ACE_DLL_Manager::instance ()->close_dll (this->dll_name_)) != 0)
00213     this->error_ = 1;
00214 
00215   // Even if close_dll() failed, go ahead and cleanup.
00216   this->dll_handle_ = 0;
00217   delete [] this->dll_name_;
00218   this->dll_name_ = 0;
00219   this->close_handle_on_destruction_ = 0;
00220 
00221   return retval;
00222 }
00223 
00224 // This method is used return the last error of a library operation.
00225 
00226 ACE_TCHAR *
00227 ACE_DLL::error (void) const
00228 {
00229   ACE_TRACE ("ACE_DLL::error");
00230   if (this->error_)
00231     return
00232       const_cast<ACE_TCHAR *> (ACE_LIB_TEXT ("Error: check log for details."));
00233 
00234   return 0;
00235 }
00236 
00237 // Return the handle to the user either temporarily or forever, thus
00238 // orphaning it. If 0 means the user wants the handle forever and if 1
00239 // means the user temporarily wants to take the handle.
00240 
00241 ACE_SHLIB_HANDLE
00242 ACE_DLL::get_handle (int become_owner) const
00243 {
00244   ACE_TRACE ("ACE_DLL::get_handle");
00245 
00246   ACE_SHLIB_HANDLE handle = ACE_SHLIB_INVALID_HANDLE;
00247 
00248   if (this->dll_handle_)
00249     handle = this->dll_handle_->get_handle (become_owner);
00250 
00251   return handle;
00252 }
00253 
00254 // Set the handle for the DLL. By default, the object will be closed
00255 // before it is destroyed.
00256 
00257 int
00258 ACE_DLL::set_handle (ACE_SHLIB_HANDLE handle,
00259                      int close_handle_on_destruction)
00260 {
00261   ACE_TRACE ("ACE_DLL::set_handle");
00262 
00263   // Create a unique name.  Note that this name is only quaranteed
00264   // to be unique for the life of this object.
00265   ACE_TCHAR temp[ACE_UNIQUE_NAME_LEN];
00266   ACE_OS::unique_name (this, temp, ACE_UNIQUE_NAME_LEN);
00267 
00268   return this->open_i (temp, 1, close_handle_on_destruction, handle);
00269 }
00270 
00271 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:41:50 2006 for ACE by doxygen 1.3.6