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