| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/dynlib.cpp |
| 3 | // Purpose: Dynamic library management |
| 4 | // Author: Guilhem Lavaux |
| 5 | // Modified by: |
| 6 | // Created: 20/07/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1998 Guilhem Lavaux |
| 9 | // 2000-2005 Vadim Zeitlin |
| 10 | // Licence: wxWindows licence |
| 11 | ///////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 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) |
| 15 | |
| 16 | // ============================================================================ |
| 17 | // declarations |
| 18 | // ============================================================================ |
| 19 | |
| 20 | // ---------------------------------------------------------------------------- |
| 21 | // headers |
| 22 | // ---------------------------------------------------------------------------- |
| 23 | |
| 24 | #include "wx/wxprec.h" |
| 25 | |
| 26 | #ifdef __BORLANDC__ |
| 27 | #pragma hdrstop |
| 28 | #endif |
| 29 | |
| 30 | #if wxUSE_DYNLIB_CLASS |
| 31 | |
| 32 | #include "wx/dynlib.h" |
| 33 | |
| 34 | #ifndef WX_PRECOMP |
| 35 | #include "wx/intl.h" |
| 36 | #include "wx/log.h" |
| 37 | #include "wx/app.h" |
| 38 | #include "wx/utils.h" |
| 39 | #endif //WX_PRECOMP |
| 40 | |
| 41 | #include "wx/filefn.h" |
| 42 | #include "wx/filename.h" // for SplitPath() |
| 43 | #include "wx/platinfo.h" |
| 44 | |
| 45 | #include "wx/arrimpl.cpp" |
| 46 | |
| 47 | WX_DEFINE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetailsArray) |
| 48 | |
| 49 | // ============================================================================ |
| 50 | // implementation |
| 51 | // ============================================================================ |
| 52 | |
| 53 | // --------------------------------------------------------------------------- |
| 54 | // wxDynamicLibrary |
| 55 | // --------------------------------------------------------------------------- |
| 56 | |
| 57 | #if defined(__WXPM__) || defined(__EMX__) |
| 58 | const wxString wxDynamicLibrary::ms_dllext(_T(".dll")); |
| 59 | #endif |
| 60 | |
| 61 | // for MSW/Unix it is defined in platform-specific file |
| 62 | #if !(defined(__WXMSW__) || defined(__UNIX__)) || defined(__EMX__) |
| 63 | |
| 64 | wxDllType wxDynamicLibrary::GetProgramHandle() |
| 65 | { |
| 66 | wxFAIL_MSG( wxT("GetProgramHandle() is not implemented under this platform")); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | #endif // __WXMSW__ || __UNIX__ |
| 71 | |
| 72 | |
| 73 | bool wxDynamicLibrary::Load(const wxString& libnameOrig, int flags) |
| 74 | { |
| 75 | wxASSERT_MSG(m_handle == 0, _T("Library already loaded.")); |
| 76 | |
| 77 | // add the proper extension for the DLL ourselves unless told not to |
| 78 | wxString libname = libnameOrig; |
| 79 | if ( !(flags & wxDL_VERBATIM) ) |
| 80 | { |
| 81 | // and also check that the libname doesn't already have it |
| 82 | wxString ext; |
| 83 | wxFileName::SplitPath(libname, NULL, NULL, &ext); |
| 84 | if ( ext.empty() ) |
| 85 | { |
| 86 | libname += GetDllExt(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // different ways to load a shared library |
| 91 | // |
| 92 | // FIXME: should go to the platform-specific files! |
| 93 | #if defined(__WXPM__) || defined(__EMX__) |
| 94 | char err[256] = ""; |
| 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); |
| 98 | #endif |
| 99 | |
| 100 | if ( m_handle == 0 && !(flags & wxDL_QUIET) ) |
| 101 | { |
| 102 | #ifdef wxHAVE_DYNLIB_ERROR |
| 103 | Error(); |
| 104 | #else |
| 105 | wxLogSysError(_("Failed to load shared library '%s'"), libname.c_str()); |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | return IsLoaded(); |
| 110 | } |
| 111 | |
| 112 | // for MSW and Unix this is implemented in the platform-specific file |
| 113 | // |
| 114 | // TODO: move the rest to os2/dlpm.cpp and mac/dlmac.cpp! |
| 115 | #if (!defined(__WXMSW__) && !defined(__UNIX__)) || defined(__EMX__) |
| 116 | |
| 117 | /* static */ |
| 118 | void wxDynamicLibrary::Unload(wxDllType handle) |
| 119 | { |
| 120 | #if defined(__OS2__) || defined(__EMX__) |
| 121 | DosFreeModule( handle ); |
| 122 | #else |
| 123 | #error "runtime shared lib support not implemented" |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | #endif // !(__WXMSW__ || __UNIX__) |
| 128 | |
| 129 | void *wxDynamicLibrary::DoGetSymbol(const wxString &name, bool *success) const |
| 130 | { |
| 131 | wxCHECK_MSG( IsLoaded(), NULL, |
| 132 | _T("Can't load symbol from unloaded library") ); |
| 133 | |
| 134 | void *symbol = 0; |
| 135 | |
| 136 | wxUnusedVar(symbol); |
| 137 | #if defined(__WXPM__) || defined(__EMX__) |
| 138 | DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol ); |
| 139 | #else |
| 140 | symbol = RawGetSymbol(m_handle, name); |
| 141 | #endif |
| 142 | |
| 143 | if ( success ) |
| 144 | *success = symbol != NULL; |
| 145 | |
| 146 | return symbol; |
| 147 | } |
| 148 | |
| 149 | void *wxDynamicLibrary::GetSymbol(const wxString& name, bool *success) const |
| 150 | { |
| 151 | void *symbol = DoGetSymbol(name, success); |
| 152 | if ( !symbol ) |
| 153 | { |
| 154 | #ifdef wxHAVE_DYNLIB_ERROR |
| 155 | Error(); |
| 156 | #else |
| 157 | wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"), |
| 158 | name.c_str()); |
| 159 | #endif |
| 160 | } |
| 161 | |
| 162 | return symbol; |
| 163 | } |
| 164 | |
| 165 | // ---------------------------------------------------------------------------- |
| 166 | // informational methods |
| 167 | // ---------------------------------------------------------------------------- |
| 168 | |
| 169 | /*static*/ |
| 170 | wxString |
| 171 | wxDynamicLibrary::CanonicalizeName(const wxString& name, |
| 172 | wxDynamicLibraryCategory cat) |
| 173 | { |
| 174 | wxString nameCanonic; |
| 175 | |
| 176 | // under Unix the library names usually start with "lib" prefix, add it |
| 177 | #if defined(__UNIX__) && !defined(__EMX__) |
| 178 | switch ( cat ) |
| 179 | { |
| 180 | default: |
| 181 | wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") ); |
| 182 | // fall through |
| 183 | |
| 184 | case wxDL_MODULE: |
| 185 | // don't do anything for modules, their names are arbitrary |
| 186 | break; |
| 187 | |
| 188 | case wxDL_LIBRARY: |
| 189 | // library names should start with "lib" under Unix |
| 190 | nameCanonic = _T("lib"); |
| 191 | break; |
| 192 | } |
| 193 | #else // !__UNIX__ |
| 194 | wxUnusedVar(cat); |
| 195 | #endif // __UNIX__/!__UNIX__ |
| 196 | |
| 197 | nameCanonic << name << GetDllExt(); |
| 198 | return nameCanonic; |
| 199 | } |
| 200 | |
| 201 | /*static*/ |
| 202 | wxString wxDynamicLibrary::CanonicalizePluginName(const wxString& name, |
| 203 | wxPluginCategory cat) |
| 204 | { |
| 205 | wxString suffix; |
| 206 | if ( cat == wxDL_PLUGIN_GUI ) |
| 207 | { |
| 208 | suffix = wxPlatformInfo::Get().GetPortIdShortName(); |
| 209 | } |
| 210 | #if wxUSE_UNICODE |
| 211 | suffix << _T('u'); |
| 212 | #endif |
| 213 | #ifdef __WXDEBUG__ |
| 214 | suffix << _T('d'); |
| 215 | #endif |
| 216 | |
| 217 | if ( !suffix.empty() ) |
| 218 | suffix = wxString(_T("_")) + suffix; |
| 219 | |
| 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) |
| 224 | #else |
| 225 | #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z) |
| 226 | #endif |
| 227 | #else |
| 228 | #if (wxMINOR_VERSION % 2) == 0 |
| 229 | #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) |
| 230 | #else |
| 231 | #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z) |
| 232 | #endif |
| 233 | #endif |
| 234 | |
| 235 | suffix << wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION, wxMINOR_VERSION, |
| 236 | wxRELEASE_NUMBER)); |
| 237 | #undef wxDLLVER |
| 238 | #undef WXSTRINGIZE |
| 239 | |
| 240 | #ifdef __WINDOWS__ |
| 241 | // Add compiler identification: |
| 242 | #if defined(__GNUG__) |
| 243 | suffix << _T("_gcc"); |
| 244 | #elif defined(__VISUALC__) |
| 245 | suffix << _T("_vc"); |
| 246 | #elif defined(__WATCOMC__) |
| 247 | suffix << _T("_wat"); |
| 248 | #elif defined(__BORLANDC__) |
| 249 | suffix << _T("_bcc"); |
| 250 | #endif |
| 251 | #endif |
| 252 | |
| 253 | return CanonicalizeName(name + suffix, wxDL_MODULE); |
| 254 | } |
| 255 | |
| 256 | /*static*/ |
| 257 | wxString wxDynamicLibrary::GetPluginsDirectory() |
| 258 | { |
| 259 | #ifdef __UNIX__ |
| 260 | wxString format = wxGetInstallPrefix(); |
| 261 | wxString dir; |
| 262 | format << wxFILE_SEP_PATH |
| 263 | << wxT("lib") << wxFILE_SEP_PATH |
| 264 | << wxT("wx") << wxFILE_SEP_PATH |
| 265 | #if (wxMINOR_VERSION % 2) == 0 |
| 266 | << wxT("%i.%i"); |
| 267 | dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION); |
| 268 | #else |
| 269 | << wxT("%i.%i.%i"); |
| 270 | dir.Printf(format.c_str(), |
| 271 | wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER); |
| 272 | #endif |
| 273 | return dir; |
| 274 | |
| 275 | #else // ! __UNIX__ |
| 276 | return wxEmptyString; |
| 277 | #endif |
| 278 | } |
| 279 | |
| 280 | |
| 281 | #endif // wxUSE_DYNLIB_CLASS |