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 WX_DEFINE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetailsArray
)
49 // ============================================================================
51 // ============================================================================
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 // for MSW/Unix it is defined in platform-specific file
58 #if !(defined(__WINDOWS__) || defined(__UNIX__)) || defined(__EMX__)
60 wxDllType
wxDynamicLibrary::GetProgramHandle()
62 wxFAIL_MSG( wxT("GetProgramHandle() is not implemented under this platform"));
66 #endif // __WINDOWS__ || __UNIX__
69 bool wxDynamicLibrary::Load(const wxString
& libnameOrig
, int flags
)
71 wxASSERT_MSG(m_handle
== 0, wxT("Library already loaded."));
73 // add the proper extension for the DLL ourselves unless told not to
74 wxString libname
= libnameOrig
;
75 if ( !(flags
& wxDL_VERBATIM
) )
77 // and also check that the libname doesn't already have it
79 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
82 libname
+= GetDllExt(wxDL_MODULE
);
86 // different ways to load a shared library
88 // FIXME: should go to the platform-specific files!
89 #if defined(__WXPM__) || defined(__EMX__)
91 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
92 #else // this should be the only remaining branch eventually
93 m_handle
= RawLoad(libname
, flags
);
96 if ( m_handle
== 0 && !(flags
& wxDL_QUIET
) )
98 #ifdef wxHAVE_DYNLIB_ERROR
101 wxLogSysError(_("Failed to load shared library '%s'"), libname
.c_str());
108 // for MSW and Unix this is implemented in the platform-specific file
110 // TODO: move the rest to os2/dlpm.cpp and mac/dlmac.cpp!
111 #if (!defined(__WINDOWS__) && !defined(__UNIX__)) || defined(__EMX__)
114 void wxDynamicLibrary::Unload(wxDllType handle
)
116 #if defined(__OS2__) || defined(__EMX__)
117 DosFreeModule( handle
);
119 #error "runtime shared lib support not implemented"
123 #endif // !(__WINDOWS__ || __UNIX__)
125 void *wxDynamicLibrary::DoGetSymbol(const wxString
&name
, bool *success
) const
127 wxCHECK_MSG( IsLoaded(), NULL
,
128 wxT("Can't load symbol from unloaded library") );
133 #if defined(__WXPM__) || defined(__EMX__)
134 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
136 symbol
= RawGetSymbol(m_handle
, name
);
140 *success
= symbol
!= NULL
;
145 void *wxDynamicLibrary::GetSymbol(const wxString
& name
, bool *success
) const
147 void *symbol
= DoGetSymbol(name
, success
);
150 #ifdef wxHAVE_DYNLIB_ERROR
153 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
161 // ----------------------------------------------------------------------------
162 // informational methods
163 // ----------------------------------------------------------------------------
166 wxString
wxDynamicLibrary::GetDllExt(wxDynamicLibraryCategory cat
)
169 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
171 #elif defined(__HPUX__)
173 #elif defined(__DARWIN__)
181 wxFAIL_MSG("unreachable");
182 return wxString(); // silence gcc warning
190 wxDynamicLibrary::CanonicalizeName(const wxString
& name
,
191 wxDynamicLibraryCategory cat
)
193 wxString nameCanonic
;
195 // under Unix the library names usually start with "lib" prefix, add it
196 #if defined(__UNIX__) && !defined(__EMX__)
200 // Library names should start with "lib" under Unix.
204 // Module names are arbitrary and should have no prefix added.
209 nameCanonic
<< name
<< GetDllExt(cat
);
215 wxString
wxDynamicLibrary::CanonicalizePluginName(const wxString
& name
,
216 wxPluginCategory cat
)
219 if ( cat
== wxDL_PLUGIN_GUI
)
221 suffix
= wxPlatformInfo::Get().GetPortIdShortName();
230 if ( !suffix
.empty() )
231 suffix
= wxString(wxT("_")) + suffix
;
233 #define WXSTRINGIZE(x) #x
234 #if defined(__UNIX__) && !defined(__EMX__)
235 #if (wxMINOR_VERSION % 2) == 0
236 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
238 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
241 #if (wxMINOR_VERSION % 2) == 0
242 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
244 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
248 suffix
<< wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION
, wxMINOR_VERSION
,
254 // Add compiler identification:
255 #if defined(__GNUG__)
256 suffix
<< wxT("_gcc");
257 #elif defined(__VISUALC__)
258 suffix
<< wxT("_vc");
259 #elif defined(__WATCOMC__)
260 suffix
<< wxT("_wat");
261 #elif defined(__BORLANDC__)
262 suffix
<< wxT("_bcc");
266 return CanonicalizeName(name
+ suffix
, wxDL_MODULE
);
270 wxString
wxDynamicLibrary::GetPluginsDirectory()
273 wxString format
= wxGetInstallPrefix();
275 format
<< wxFILE_SEP_PATH
276 << wxT("lib") << wxFILE_SEP_PATH
277 << wxT("wx") << wxFILE_SEP_PATH
278 #if (wxMINOR_VERSION % 2) == 0
280 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
283 dir
.Printf(format
.c_str(),
284 wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
289 return wxEmptyString
;
294 #endif // wxUSE_DYNLIB_CLASS