00001 #include "ace_pch.h"
00002
00003
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
00014
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
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
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
00045
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
00065
00066
00067
00068 ACE_DLL::~ACE_DLL (void)
00069 {
00070 ACE_TRACE ("ACE_DLL::~ACE_DLL");
00071
00072 this->close ();
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
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
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
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
00155
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
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
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
00190
00191
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
00207
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
00216
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 }