1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dynlib.cpp
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 #include "wx/wxprec.h"
30 #if wxUSE_DYNLIB_CLASS
32 #include "wx/dynlib.h"
41 #include "wx/filefn.h"
42 #include "wx/filename.h" // for SplitPath()
43 #include "wx/platinfo.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 wxString
wxDynamicLibrary::ms_dllext(_T(".dll"));
65 // for MSW/Unix it is defined in platform-specific file
66 #if !(defined(__WXMSW__) || defined(__UNIX__)) || defined(__EMX__)
68 wxDllType
wxDynamicLibrary::GetProgramHandle()
70 wxFAIL_MSG( wxT("GetProgramHandle() is not implemented under this platform"));
74 #endif // __WXMSW__ || __UNIX__
77 bool wxDynamicLibrary::Load(const wxString
& libnameOrig
, int flags
)
79 wxASSERT_MSG(m_handle
== 0, _T("Library already loaded."));
81 // add the proper extension for the DLL ourselves unless told not to
82 wxString libname
= libnameOrig
;
83 if ( !(flags
& wxDL_VERBATIM
) )
85 // and also check that the libname doesn't already have it
87 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
90 libname
+= GetDllExt();
94 // different ways to load a shared library
96 // FIXME: should go to the platform-specific files!
97 #if defined(__WXPM__) || defined(__EMX__)
99 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
100 #else // this should be the only remaining branch eventually
101 m_handle
= RawLoad(libname
, flags
);
104 if ( m_handle
== 0 && !(flags
& wxDL_QUIET
) )
106 #ifdef wxHAVE_DYNLIB_ERROR
109 wxLogSysError(_("Failed to load shared library '%s'"), libname
.c_str());
116 // for MSW and Unix this is implemented in the platform-specific file
118 // TODO: move the rest to os2/dlpm.cpp and mac/dlmac.cpp!
119 #if (!defined(__WXMSW__) && !defined(__UNIX__)) || defined(__EMX__)
122 void wxDynamicLibrary::Unload(wxDllType handle
)
124 #if defined(__OS2__) || defined(__EMX__)
125 DosFreeModule( handle
);
127 #error "runtime shared lib support not implemented"
131 #endif // !(__WXMSW__ || __UNIX__)
133 void *wxDynamicLibrary::DoGetSymbol(const wxString
&name
, bool *success
) const
135 wxCHECK_MSG( IsLoaded(), NULL
,
136 _T("Can't load symbol from unloaded library") );
141 #if defined(__WXPM__) || defined(__EMX__)
142 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
144 symbol
= RawGetSymbol(m_handle
, name
);
148 *success
= symbol
!= NULL
;
153 void *wxDynamicLibrary::GetSymbol(const wxString
& name
, bool *success
) const
155 void *symbol
= DoGetSymbol(name
, success
);
158 #ifdef wxHAVE_DYNLIB_ERROR
161 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
169 // ----------------------------------------------------------------------------
170 // informational methods
171 // ----------------------------------------------------------------------------
175 wxDynamicLibrary::CanonicalizeName(const wxString
& name
,
176 wxDynamicLibraryCategory cat
)
178 wxString nameCanonic
;
180 // under Unix the library names usually start with "lib" prefix, add it
181 #if defined(__UNIX__) && !defined(__EMX__)
185 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
189 // don't do anything for modules, their names are arbitrary
193 // library names should start with "lib" under Unix
194 nameCanonic
= _T("lib");
199 #endif // __UNIX__/!__UNIX__
201 nameCanonic
<< name
<< GetDllExt();
206 wxString
wxDynamicLibrary::CanonicalizePluginName(const wxString
& name
,
207 wxPluginCategory cat
)
210 if ( cat
== wxDL_PLUGIN_GUI
)
212 suffix
= wxPlatformInfo::Get().GetPortIdShortName();
221 if ( !suffix
.empty() )
222 suffix
= wxString(_T("_")) + suffix
;
224 #define WXSTRINGIZE(x) #x
225 #if defined(__UNIX__) && !defined(__EMX__)
226 #if (wxMINOR_VERSION % 2) == 0
227 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
229 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
232 #if (wxMINOR_VERSION % 2) == 0
233 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
235 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
239 suffix
<< wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION
, wxMINOR_VERSION
,
245 // Add compiler identification:
246 #if defined(__GNUG__)
247 suffix
<< _T("_gcc");
248 #elif defined(__VISUALC__)
250 #elif defined(__WATCOMC__)
251 suffix
<< _T("_wat");
252 #elif defined(__BORLANDC__)
253 suffix
<< _T("_bcc");
257 return CanonicalizeName(name
+ suffix
, wxDL_MODULE
);
261 wxString
wxDynamicLibrary::GetPluginsDirectory()
264 wxString format
= wxGetInstallPrefix();
266 format
<< wxFILE_SEP_PATH
267 << wxT("lib") << wxFILE_SEP_PATH
268 << wxT("wx") << wxFILE_SEP_PATH
269 #if (wxMINOR_VERSION % 2) == 0
271 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
274 dir
.Printf(format
.c_str(),
275 wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
280 return wxEmptyString
;
285 #endif // wxUSE_DYNLIB_CLASS