00001
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
00017
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
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
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
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
00077
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
00098
00099
00100
00101 ACE_DLL::~ACE_DLL (void)
00102 {
00103 ACE_TRACE ("ACE_DLL::~ACE_DLL");
00104
00105 this->close ();
00106
00107
00108
00109
00110
00111 delete [] this->dll_name_;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
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
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
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
00200
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
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
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
00238
00239
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
00255
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
00264
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