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