1 // $Id: DLL.cpp 80826 2008-03-04 14:51:23Z wotte $
5 #include "ace/Log_Msg.h"
7 #include "ace/DLL_Manager.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/OS_NS_dlfcn.h"
10 #include "ace/OS_NS_Thread.h"
14 ACE_RCSID(ace
, DLL
, "$Id: DLL.cpp 80826 2008-03-04 14:51:23Z wotte $")
16 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
18 // Default constructor. Also, by default, the object will be closed
19 // before it is destroyed.
21 ACE_DLL::ACE_DLL (bool close_handle_on_destruction
)
24 close_handle_on_destruction_ (close_handle_on_destruction
),
28 ACE_TRACE ("ACE_DLL::ACE_DLL (int)");
31 ACE_DLL::ACE_DLL (const ACE_DLL
&rhs
)
34 close_handle_on_destruction_ (false),
38 ACE_TRACE ("ACE_DLL::ACE_DLL (const ACE_DLL &)");
41 // This will automatically up the refcount.
42 && this->open (rhs
.dll_name_
,
44 rhs
.close_handle_on_destruction_
) != 0
47 ACE_TEXT ("ACE_DLL::copy_ctor: error: %s\n"),
51 // Assignment operator
54 ACE_DLL::operator= (const ACE_DLL
&rhs
)
56 ACE_TRACE ("ACE_DLL::operator= (const ACE_DLL &)");
60 std::swap (this->open_mode_
, tmp
.open_mode_
);
61 std::swap (this->dll_name_
, tmp
.dll_name_
);
62 std::swap (this->close_handle_on_destruction_
,
63 tmp
.close_handle_on_destruction_
);
64 std::swap (this->dll_handle_
, tmp
.dll_handle_
);
65 std::swap (this->error_
, tmp
.error_
);
71 // If the library name and the opening mode are specified than on
72 // object creation the library is implicitly opened.
74 ACE_DLL::ACE_DLL (const ACE_TCHAR
*dll_name
,
76 bool close_handle_on_destruction
)
77 : open_mode_ (open_mode
),
79 close_handle_on_destruction_ (close_handle_on_destruction
),
83 ACE_TRACE ("ACE_DLL::ACE_DLL");
85 if (this->open (dll_name
, this->open_mode_
, close_handle_on_destruction
) != 0
88 ACE_TEXT ("ACE_DLL::open: error calling open: %s\n"),
92 // The library is closed before the class gets destroyed depending on
93 // the close_handle_on_destruction value specified which is stored in
94 // close_handle_on_destruction_.
96 ACE_DLL::~ACE_DLL (void)
98 ACE_TRACE ("ACE_DLL::~ACE_DLL");
102 // Normally delete()d in ACE_DLL::close(). However, that may not
103 // occur if full ACE_DLL initialization is interrupted due to errors
104 // (e.g. attempting to open a DSO/DLL that does not exist). Make
105 // sure this->dll_name_ is deallocated.
106 delete [] this->dll_name_
;
109 // This method opens the library based on the mode specified using the
110 // ACE_SHLIB_HANDLE which is obtained on making the ACE_OS::dlopen call.
111 // The default mode is:
112 // RTLD_LAZY Only references to data symbols are relocate when the
113 // object is first loaded.
114 // The other modes include:
115 // RTLD_NOW All necessary relocations are performed when the
116 // object is first loaded.
117 // RTLD_GLOBAL The object symbols are made available for the
118 // relocation processing of any other object.
121 ACE_DLL::open (const ACE_TCHAR
*dll_filename
,
123 bool close_handle_on_destruction
)
125 ACE_TRACE ("ACE_DLL::open");
127 return open_i (dll_filename
, open_mode
, close_handle_on_destruction
);
131 ACE_DLL::open_i (const ACE_TCHAR
*dll_filename
,
133 bool close_handle_on_destruction
,
134 ACE_SHLIB_HANDLE handle
)
136 ACE_TRACE ("ACE_DLL::open_i");
143 ACE_ERROR ((LM_ERROR
,
144 ACE_TEXT ("ACE_DLL::open_i: dll_name is %s\n"),
145 this->dll_name_
== 0 ? ACE_TEXT ("(null)")
150 if (this->dll_handle_
)
152 // If we have a good handle and its the same name, just return.
153 if (ACE_OS::strcmp (this->dll_name_
, dll_filename
) == 0)
159 if (!this->dll_name_
)
160 this->dll_name_
= ACE::strnew (dll_filename
);
162 this->open_mode_
= open_mode
;
163 this->close_handle_on_destruction_
= close_handle_on_destruction
;
165 this->dll_handle_
= ACE_DLL_Manager::instance()->open_dll (this->dll_name_
,
169 if (!this->dll_handle_
)
172 return this->error_
? -1 : 0;
175 // The symbol refernce of the name specified is obtained.
178 ACE_DLL::symbol (const ACE_TCHAR
*sym_name
, int ignore_errors
)
180 ACE_TRACE ("ACE_DLL::symbol");
185 if (this->dll_handle_
)
186 sym
= this->dll_handle_
->symbol (sym_name
, ignore_errors
);
194 // The library is closed using the ACE_SHLIB_HANDLE object, i.e., the
195 // shared object is now disassociated form the current process.
198 ACE_DLL::close (void)
200 ACE_TRACE ("ACE_DLL::close");
204 if (this->dll_handle_
205 && this->close_handle_on_destruction_
207 && (retval
= ACE_DLL_Manager::instance ()->close_dll (this->dll_name_
)) != 0)
210 // Even if close_dll() failed, go ahead and cleanup.
211 this->dll_handle_
= 0;
212 delete [] this->dll_name_
;
214 this->close_handle_on_destruction_
= false;
219 // This method is used return the last error of a library operation.
222 ACE_DLL::error (void) const
224 ACE_TRACE ("ACE_DLL::error");
227 return ACE_OS::dlerror ();
233 // Return the handle to the user either temporarily or forever, thus
234 // orphaning it. If 0 means the user wants the handle forever and if 1
235 // means the user temporarily wants to take the handle.
238 ACE_DLL::get_handle (int become_owner
) const
240 ACE_TRACE ("ACE_DLL::get_handle");
242 ACE_SHLIB_HANDLE handle
= ACE_SHLIB_INVALID_HANDLE
;
244 if (this->dll_handle_
)
245 handle
= this->dll_handle_
->get_handle (become_owner
);
250 // Set the handle for the DLL. By default, the object will be closed
251 // before it is destroyed.
254 ACE_DLL::set_handle (ACE_SHLIB_HANDLE handle
,
255 bool close_handle_on_destruction
)
257 ACE_TRACE ("ACE_DLL::set_handle");
259 // Create a unique name. Note that this name is only quaranteed
260 // to be unique for the life of this object.
261 ACE_TCHAR temp
[ACE_UNIQUE_NAME_LEN
];
262 ACE_OS::unique_name (this, temp
, ACE_UNIQUE_NAME_LEN
);
264 return this->open_i (temp
, 1, close_handle_on_destruction
, handle
);
267 ACE_END_VERSIONED_NAMESPACE_DECL