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 #if defined(__WXPM__) || defined(__EMX__)
58 const wxString
wxDynamicLibrary::ms_dllext(wxT(".dll"));
61 // for MSW/Unix it is defined in platform-specific file
62 #if !(defined(__WINDOWS__) || defined(__UNIX__)) || defined(__EMX__)
64 wxDllType
wxDynamicLibrary::GetProgramHandle()
66 wxFAIL_MSG( wxT("GetProgramHandle() is not implemented under this platform"));
70 #endif // __WINDOWS__ || __UNIX__
73 bool wxDynamicLibrary::Load(const wxString
& libnameOrig
, int flags
)
75 wxASSERT_MSG(m_handle
== 0, wxT("Library already loaded."));
77 // add the proper extension for the DLL ourselves unless told not to
78 wxString libname
= libnameOrig
;
79 if ( !(flags
& wxDL_VERBATIM
) )
81 // and also check that the libname doesn't already have it
83 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
86 libname
+= GetDllExt();
90 // different ways to load a shared library
92 // FIXME: should go to the platform-specific files!
93 #if defined(__WXPM__) || defined(__EMX__)
95 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
96 #else // this should be the only remaining branch eventually
97 m_handle
= RawLoad(libname
, flags
);
100 if ( m_handle
== 0 && !(flags
& wxDL_QUIET
) )
102 #ifdef wxHAVE_DYNLIB_ERROR
105 wxLogSysError(_("Failed to load shared library '%s'"), libname
.c_str());
112 // for MSW and Unix this is implemented in the platform-specific file
114 // TODO: move the rest to os2/dlpm.cpp and mac/dlmac.cpp!
115 #if (!defined(__WINDOWS__) && !defined(__UNIX__)) || defined(__EMX__)
118 void wxDynamicLibrary::Unload(wxDllType handle
)
120 #if defined(__OS2__) || defined(__EMX__)
121 DosFreeModule( handle
);
123 #error "runtime shared lib support not implemented"
127 #endif // !(__WINDOWS__ || __UNIX__)
129 void *wxDynamicLibrary::DoGetSymbol(const wxString
&name
, bool *success
) const
131 wxCHECK_MSG( IsLoaded(), NULL
,
132 wxT("Can't load symbol from unloaded library") );
137 #if defined(__WXPM__) || defined(__EMX__)
138 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
140 symbol
= RawGetSymbol(m_handle
, name
);
144 *success
= symbol
!= NULL
;
149 void *wxDynamicLibrary::GetSymbol(const wxString
& name
, bool *success
) const
151 void *symbol
= DoGetSymbol(name
, success
);
154 #ifdef wxHAVE_DYNLIB_ERROR
157 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
165 // ----------------------------------------------------------------------------
166 // informational methods
167 // ----------------------------------------------------------------------------
171 wxDynamicLibrary::CanonicalizeName(const wxString
& name
,
172 wxDynamicLibraryCategory cat
)
174 wxString nameCanonic
;
176 // under Unix the library names usually start with "lib" prefix, add it
177 #if defined(__UNIX__) && !defined(__EMX__)
181 wxFAIL_MSG( wxT("unknown wxDynamicLibraryCategory value") );
185 // don't do anything for modules, their names are arbitrary
189 // library names should start with "lib" under Unix
190 nameCanonic
= wxT("lib");
195 #endif // __UNIX__/!__UNIX__
197 nameCanonic
<< name
<< GetDllExt();
202 wxString
wxDynamicLibrary::CanonicalizePluginName(const wxString
& name
,
203 wxPluginCategory cat
)
206 if ( cat
== wxDL_PLUGIN_GUI
)
208 suffix
= wxPlatformInfo::Get().GetPortIdShortName();
217 if ( !suffix
.empty() )
218 suffix
= wxString(wxT("_")) + suffix
;
220 #define WXSTRINGIZE(x) #x
221 #if defined(__UNIX__) && !defined(__EMX__)
222 #if (wxMINOR_VERSION % 2) == 0
223 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
225 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
228 #if (wxMINOR_VERSION % 2) == 0
229 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
231 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
235 suffix
<< wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION
, wxMINOR_VERSION
,
241 // Add compiler identification:
242 #if defined(__GNUG__)
243 suffix
<< wxT("_gcc");
244 #elif defined(__VISUALC__)
245 suffix
<< wxT("_vc");
246 #elif defined(__WATCOMC__)
247 suffix
<< wxT("_wat");
248 #elif defined(__BORLANDC__)
249 suffix
<< wxT("_bcc");
253 return CanonicalizeName(name
+ suffix
, wxDL_MODULE
);
257 wxString
wxDynamicLibrary::GetPluginsDirectory()
260 wxString format
= wxGetInstallPrefix();
262 format
<< wxFILE_SEP_PATH
263 << wxT("lib") << wxFILE_SEP_PATH
264 << wxT("wx") << wxFILE_SEP_PATH
265 #if (wxMINOR_VERSION % 2) == 0
267 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
270 dir
.Printf(format
.c_str(),
271 wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
276 return wxEmptyString
;
281 #endif // wxUSE_DYNLIB_CLASS