1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dynlib.cpp
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
7 // Copyright: (c) 1998 Guilhem Lavaux
8 // 2000-2005 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 //FIXME: This class isn't really common at all, it should be moved into
13 // platform dependent files (already done for Windows and Unix)
15 // ============================================================================
17 // ============================================================================
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
23 #include "wx/wxprec.h"
29 #if wxUSE_DYNLIB_CLASS
31 #include "wx/dynlib.h"
40 #include "wx/filefn.h"
41 #include "wx/filename.h" // for SplitPath()
42 #include "wx/platinfo.h"
44 #include "wx/arrimpl.cpp"
46 WX_DEFINE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetailsArray
)
48 // ============================================================================
50 // ============================================================================
52 // ---------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------
56 // for MSW/Unix it is defined in platform-specific file
57 #if !(defined(__WINDOWS__) || defined(__UNIX__)) || defined(__EMX__)
59 wxDllType
wxDynamicLibrary::GetProgramHandle()
61 wxFAIL_MSG( wxT("GetProgramHandle() is not implemented under this platform"));
65 #endif // __WINDOWS__ || __UNIX__
68 bool wxDynamicLibrary::Load(const wxString
& libnameOrig
, int flags
)
70 wxASSERT_MSG(m_handle
== 0, wxT("Library already loaded."));
72 // add the proper extension for the DLL ourselves unless told not to
73 wxString libname
= libnameOrig
;
74 if ( !(flags
& wxDL_VERBATIM
) )
76 // and also check that the libname doesn't already have it
78 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
81 libname
+= GetDllExt(wxDL_MODULE
);
85 // different ways to load a shared library
87 // FIXME: should go to the platform-specific files!
88 #if defined(__WXPM__) || defined(__EMX__)
90 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
91 #else // this should be the only remaining branch eventually
92 m_handle
= RawLoad(libname
, flags
);
95 if ( m_handle
== 0 && !(flags
& wxDL_QUIET
) )
97 #ifdef wxHAVE_DYNLIB_ERROR
100 wxLogSysError(_("Failed to load shared library '%s'"), libname
.c_str());
107 // for MSW and Unix this is implemented in the platform-specific file
109 // TODO: move the rest to os2/dlpm.cpp and mac/dlmac.cpp!
110 #if (!defined(__WINDOWS__) && !defined(__UNIX__)) || defined(__EMX__)
113 void wxDynamicLibrary::Unload(wxDllType handle
)
115 #if defined(__OS2__) || defined(__EMX__)
116 DosFreeModule( handle
);
118 #error "runtime shared lib support not implemented"
122 #endif // !(__WINDOWS__ || __UNIX__)
124 void *wxDynamicLibrary::DoGetSymbol(const wxString
&name
, bool *success
) const
126 wxCHECK_MSG( IsLoaded(), NULL
,
127 wxT("Can't load symbol from unloaded library") );
132 #if defined(__WXPM__) || defined(__EMX__)
133 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
135 symbol
= RawGetSymbol(m_handle
, name
);
139 *success
= symbol
!= NULL
;
144 void *wxDynamicLibrary::GetSymbol(const wxString
& name
, bool *success
) const
146 void *symbol
= DoGetSymbol(name
, success
);
149 #ifdef wxHAVE_DYNLIB_ERROR
152 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
160 // ----------------------------------------------------------------------------
161 // informational methods
162 // ----------------------------------------------------------------------------
165 wxString
wxDynamicLibrary::GetDllExt(wxDynamicLibraryCategory cat
)
168 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
170 #elif defined(__HPUX__)
172 #elif defined(__DARWIN__)
180 wxFAIL_MSG("unreachable");
181 return wxString(); // silence gcc warning
189 wxDynamicLibrary::CanonicalizeName(const wxString
& name
,
190 wxDynamicLibraryCategory cat
)
192 wxString nameCanonic
;
194 // under Unix the library names usually start with "lib" prefix, add it
195 #if defined(__UNIX__) && !defined(__EMX__)
199 // Library names should start with "lib" under Unix.
203 // Module names are arbitrary and should have no prefix added.
208 nameCanonic
<< name
<< GetDllExt(cat
);
214 wxString
wxDynamicLibrary::CanonicalizePluginName(const wxString
& name
,
215 wxPluginCategory cat
)
218 if ( cat
== wxDL_PLUGIN_GUI
)
220 suffix
= wxPlatformInfo::Get().GetPortIdShortName();
229 if ( !suffix
.empty() )
230 suffix
= wxString(wxT("_")) + suffix
;
232 #define WXSTRINGIZE(x) #x
233 #if defined(__UNIX__) && !defined(__EMX__)
234 #if (wxMINOR_VERSION % 2) == 0
235 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
237 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
240 #if (wxMINOR_VERSION % 2) == 0
241 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
243 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
247 suffix
<< wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION
, wxMINOR_VERSION
,
253 // Add compiler identification:
254 #if defined(__GNUG__)
255 suffix
<< wxT("_gcc");
256 #elif defined(__VISUALC__)
257 suffix
<< wxT("_vc");
258 #elif defined(__WATCOMC__)
259 suffix
<< wxT("_wat");
260 #elif defined(__BORLANDC__)
261 suffix
<< wxT("_bcc");
265 return CanonicalizeName(name
+ suffix
, wxDL_MODULE
);
269 wxString
wxDynamicLibrary::GetPluginsDirectory()
272 wxString format
= wxGetInstallPrefix();
274 format
<< wxFILE_SEP_PATH
275 << wxT("lib") << wxFILE_SEP_PATH
276 << wxT("wx") << wxFILE_SEP_PATH
277 #if (wxMINOR_VERSION % 2) == 0
279 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
282 dir
.Printf(format
.c_str(),
283 wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
288 return wxEmptyString
;
293 #endif // wxUSE_DYNLIB_CLASS