1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // 2000-2005 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 //FIXME: This class isn't really common at all, it should be moved into
14 // platform dependent files (already done for Windows and Unix)
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
25 # pragma implementation "dynlib.h"
28 #include "wx/wxprec.h"
34 #if wxUSE_DYNLIB_CLASS
36 #include "wx/dynlib.h"
37 #include "wx/filefn.h"
41 #include "wx/filename.h" // for SplitPath()
43 #include "wx/apptrait.h"
45 #include "wx/arrimpl.cpp"
47 #if defined(__WXMAC__)
48 #include "wx/mac/private.h"
51 WX_DEFINE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetailsArray
);
53 // ============================================================================
55 // ============================================================================
57 // ---------------------------------------------------------------------------
59 // ---------------------------------------------------------------------------
61 #if defined(__WXPM__) || defined(__EMX__)
62 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".dll");
63 #elif defined(__WXMAC__) && !defined(__DARWIN__)
64 const wxChar
*wxDynamicLibrary::ms_dllext
= wxEmptyString
;
67 // for Unix it is in src/unix/dlunix.cpp
68 #if !defined(__UNIX__) || defined(__EMX__)
70 wxDllType
wxDynamicLibrary::GetProgramHandle()
72 wxFAIL_MSG( wxT("GetProgramHandle() is not implemented under this platform"));
78 bool wxDynamicLibrary::Load(const wxString
& libnameOrig
, int flags
)
80 wxASSERT_MSG(m_handle
== 0, _T("Library already loaded."));
82 // add the proper extension for the DLL ourselves unless told not to
83 wxString libname
= libnameOrig
;
84 if ( !(flags
& wxDL_VERBATIM
) )
86 // and also check that the libname doesn't already have it
88 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
91 libname
+= GetDllExt();
95 // different ways to load a shared library
97 // FIXME: should go to the platform-specific files!
98 #if defined(__WXMAC__) && !defined(__DARWIN__)
103 wxMacFilename2FSSpec( libname
, &myFSSpec
);
105 if( GetDiskFragment( &myFSSpec
,
112 myErrName
) != noErr
)
114 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
116 wxMacMakeStringFromPascal( myErrName
).c_str() );
120 #elif defined(__WXPM__) || defined(__EMX__)
122 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
124 m_handle
= RawLoad(libname
, flags
);
129 #ifdef wxHAVE_DYNLIB_ERROR
132 wxLogSysError(_("Failed to load shared library '%s'"), libname
.c_str());
139 // for MSW and Unix this is implemented in the platform-specific file
141 // TODO: move the rest to os2/dlpm.cpp and mac/dlmac.cpp!
142 #if (!defined(__WXMSW__) && !defined(__UNIX__)) || defined(__EMX__)
145 void wxDynamicLibrary::Unload(wxDllType handle
)
147 #if defined(__WXPM__) || defined(__EMX__)
148 DosFreeModule( handle
);
149 #elif defined(__WXMAC__) && !defined(__DARWIN__)
150 CloseConnection( (CFragConnectionID
*) &handle
);
152 #error "runtime shared lib support not implemented"
156 #endif // !(__WXMSW__ || __UNIX__)
158 void *wxDynamicLibrary::DoGetSymbol(const wxString
&name
, bool *success
) const
160 wxCHECK_MSG( IsLoaded(), NULL
,
161 _T("Can't load symbol from unloaded library") );
166 #if defined(__WXMAC__) && !defined(__DARWIN__)
168 CFragSymbolClass symClass
;
171 c2pstrcpy( (StringPtr
) symName
, name
.fn_str() );
173 strcpy( (char *)symName
, name
.fn_str() );
174 c2pstr( (char *)symName
);
176 if( FindSymbol( m_handle
, symName
, &symAddress
, &symClass
) == noErr
)
177 symbol
= (void *)symAddress
;
178 #elif defined(__WXPM__) || defined(__EMX__)
179 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
181 symbol
= RawGetSymbol(m_handle
, name
);
185 *success
= symbol
!= NULL
;
190 void *wxDynamicLibrary::GetSymbol(const wxString
& name
, bool *success
) const
192 void *symbol
= DoGetSymbol(name
, success
);
195 #ifdef wxHAVE_DYNLIB_ERROR
198 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
206 // ----------------------------------------------------------------------------
207 // informational methods
208 // ----------------------------------------------------------------------------
212 wxDynamicLibrary::CanonicalizeName(const wxString
& name
,
213 wxDynamicLibraryCategory cat
)
215 wxString nameCanonic
;
217 // under Unix the library names usually start with "lib" prefix, add it
218 #if defined(__UNIX__) && !defined(__EMX__)
222 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
226 // don't do anything for modules, their names are arbitrary
230 // library names should start with "lib" under Unix
231 nameCanonic
= _T("lib");
236 #endif // __UNIX__/!__UNIX__
238 nameCanonic
<< name
<< GetDllExt();
243 wxString
wxDynamicLibrary::CanonicalizePluginName(const wxString
& name
,
244 wxPluginCategory cat
)
247 if ( cat
== wxDL_PLUGIN_GUI
)
249 wxAppTraits
*traits
= wxAppConsole::GetInstance() ?
250 wxAppConsole::GetInstance()->GetTraits() : NULL
;
251 wxASSERT_MSG( traits
,
252 _("can't query for GUI plugins name in console applications") );
253 suffix
= traits
->GetToolkitInfo().shortName
;
262 if ( !suffix
.empty() )
263 suffix
= wxString(_T("_")) + suffix
;
265 #define WXSTRINGIZE(x) #x
266 #if defined(__UNIX__) && !defined(__EMX__)
267 #if (wxMINOR_VERSION % 2) == 0
268 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
270 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
273 #if (wxMINOR_VERSION % 2) == 0
274 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
276 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
280 suffix
<< wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION
, wxMINOR_VERSION
,
286 // Add compiler identification:
287 #if defined(__GNUG__)
288 suffix
<< _T("_gcc");
289 #elif defined(__VISUALC__)
291 #elif defined(__WATCOMC__)
292 suffix
<< _T("_wat");
293 #elif defined(__BORLANDC__)
294 suffix
<< _T("_bcc");
298 return CanonicalizeName(name
+ suffix
, wxDL_MODULE
);
302 wxString
wxDynamicLibrary::GetPluginsDirectory()
305 wxString format
= wxGetInstallPrefix();
307 format
<< wxFILE_SEP_PATH
308 << wxT("lib") << wxFILE_SEP_PATH
309 << wxT("wx") << wxFILE_SEP_PATH
310 #if (wxMINOR_VERSION % 2) == 0
312 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
315 dir
.Printf(format
.c_str(),
316 wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
321 return wxEmptyString
;
326 #endif // wxUSE_DYNLIB_CLASS