1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
18 #include <OSD_Function.hxx>
19 #include <OSD_LoadMode.hxx>
20 #include <OSD_SharedLibrary.hxx>
23 #ifdef __some_crappy_system__
25 * Values for 'mode' argument in dlopen().
31 * Interface to rld via unsupported __rld_libdl_interface() call.
34 #define _LIBDL_RLD_DLOPEN 1
35 #define _LIBDL_RLD_DLCLOSE 2
36 #define _LIBDL_RLD_DLSYM 3
37 #define _LIBDL_RLD_DLERROR 4
38 extern "C" {void *dlopen(char *path, int mode);}
39 extern "C" {void* dlsym ( void* handle,char* name);}
40 extern "C" {int dlclose ( void *handle );}
41 extern "C" {void *dlerror (void);}
46 #ifdef __GNU_LIBRARY__
47 extern "C" { size_t strlen(const char* s) __THROW; }
49 extern "C" { size_t strlen(const char* s); }
53 #define BAD(X) ((X) == NULL)
55 // ----------------------------------------------------------------
57 // Create and initialize a shared library object to NULL
59 // ----------------------------------------------------------------
60 OSD_SharedLibrary::OSD_SharedLibrary():myHandle(NULL),myName(NULL){
62 // ----------------------------------------------------------------
64 // Create and initialize a shared library object to the
65 // name given as argument
67 // ----------------------------------------------------------------
68 OSD_SharedLibrary::OSD_SharedLibrary(const Standard_CString aName):myHandle(NULL)
71 myName = new char [(strlen (aName) + 1 )];
72 strcpy (myName,aName);
75 // ----------------------------------------------------------------
77 // Name: Returns the shared library name
79 // ----------------------------------------------------------------
80 Standard_CString OSD_SharedLibrary::Name() const {
83 // ----------------------------------------------------------------
85 // SetName: Sets a name to a shared library object
87 // ----------------------------------------------------------------
88 void OSD_SharedLibrary::SetName(const Standard_CString aName) {
90 myName = new char [(strlen (aName) + 1 )];
91 strcpy (myName,aName);
94 // ----------------------------------------------------------------
96 // DlOpen: The dlopen function provides an interface to the dynamic
97 // library loader to allow shared libraries to be loaded and called at
99 // The dlopen function attempts to load filename, in the address space
100 // of the process, resolving symbols as appropriate. Any libraries that
101 // filename depends upon are also loaded.
103 // If mode is RTLD_LAZY, then the runtime loader does symbol resolution
104 // only as needed. Typically, this means that the first call
105 // to a function in the newly loaded library will cause the resolution
106 // of the address of that function to occur.
108 // If mode is RTLD_NOW, then the runtime loader must do all
109 // symbol binding during the dlopen call.
110 // The dlopen function returns a handle that is used by dlsym or
111 // dlclose call. If there is an error, a NULLpointer is returned.
113 // If a NULL filename is specified, dlopen returns a handle for the main
114 // executable, which allows access to dynamic symbols in the running program.
116 // ----------------------------------------------------------------
117 Standard_Boolean OSD_SharedLibrary::DlOpen(const OSD_LoadMode aMode ) {
118 if (aMode == OSD_RTLD_LAZY){
119 myHandle = dlopen (myName,RTLD_LAZY);
121 else if (aMode == OSD_RTLD_NOW){
122 myHandle = dlopen (myName,RTLD_NOW);
126 return Standard_True;
129 return Standard_False;
132 // ----------------------------------------------------------------
134 // DlSymb: The dlsym function returns the address of the
135 // symbol name found in the shared library corresponding to handle.
136 // If the symbol is not found, a NULL
137 // pointer is returned.
139 // ----------------------------------------------------------------
140 OSD_Function OSD_SharedLibrary::DlSymb(const Standard_CString aName )const{
142 fp = (void (*)()) dlsym (myHandle,aName);
144 return (OSD_Function)fp;
147 return (OSD_Function)NULL;
150 // ----------------------------------------------------------------
152 //DlClose: The dlclose function deallocates the address space for the library
153 //corresponding to handle. If any user function continues to call a symbol
154 //resolved in the address space of a library that has been since been deallo-
155 //cated by dlclose, the results are undefined.
157 // ----------------------------------------------------------------
158 void OSD_SharedLibrary::DlClose()const{
161 // ----------------------------------------------------------------
163 // DlError: returns a string describing the last error that
164 // occurred from a call to dlopen, dlclose or dlsym.
166 // ----------------------------------------------------------------
167 Standard_CString OSD_SharedLibrary::DlError()const{
168 return (char*) dlerror();
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
173 void OSD_SharedLibrary::Destroy() {
174 if (myName != NULL) {
183 //------------------------------------------------------------------------
184 //------------------- Windows NT sources for OSD_SharedLibrary ----------
185 //------------------------------------------------------------------------
187 //it is important to define STRICT and enforce including <windows.h> before
188 //Standard_Macro.hxx undefines it and includes <windows.h> causing compilation errors
195 #include <OSD_Path.hxx>
196 #include <OSD_SharedLibrary.hxx>
197 #include <TCollection_AsciiString.hxx>
199 static DWORD lastDLLError;
200 static Standard_Character errMsg[ 1024 ];
202 OSD_SharedLibrary :: OSD_SharedLibrary () {
207 } // end constructor ( 1 )
209 OSD_SharedLibrary :: OSD_SharedLibrary ( const Standard_CString aFilename ) {
214 SetName ( aFilename );
216 } // end constructro ( 2 )
218 void OSD_SharedLibrary :: SetName ( const Standard_CString aName ) {
220 OSD_Path path ( aName );
221 TCollection_AsciiString name ( aName );
223 if ( myName != NULL )
227 myName = new Standard_Character[ strlen ( aName ) + 1 ];
229 strcpy ( myName, aName );
232 name.AssignCat ( path.Extension () );
234 myHandle = GetModuleHandle ( name.ToCString () );
236 } // end OSD_SharedLibrary :: SetName
238 Standard_CString OSD_SharedLibrary :: Name () const {
242 } // end OSD_SharedLibrary :: Name
244 Standard_Boolean OSD_SharedLibrary :: DlOpen ( const OSD_LoadMode /*Mode*/ ) {
246 Standard_Boolean retVal = Standard_True;
248 if ( ( myHandle ) == NULL &&
249 ( myHandle = ( HINSTANCE )LoadLibraryEx (
250 myName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH
254 lastDLLError = GetLastError ();
255 retVal = Standard_False;
261 } // end OSD_SharedLibrary :: DlOpen
263 OSD_Function OSD_SharedLibrary :: DlSymb ( const Standard_CString Name ) const {
265 OSD_Function func = ( OSD_Function )GetProcAddress ( ( HMODULE )myHandle, Name );
269 lastDLLError = GetLastError ();
273 } // end OSD_SharedLibrary :: DlSymb
275 void OSD_SharedLibrary :: DlClose () const {
277 FreeLibrary ( ( HMODULE )myHandle );
279 } // end OSD_SharedLibrary :: DlClose
281 Standard_CString OSD_SharedLibrary :: DlError () const {
284 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
285 0, lastDLLError, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ), errMsg, 1024, ( va_list* )&myName
290 } // end OSD_SharedLibrary :: DlError
292 void OSD_SharedLibrary :: Destroy () {
294 if ( myName != NULL ) delete [] myName;
296 } // end OSD_SharedLibrary :: Destroy