Library of Assembled Shared Sources
dynamic_library.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "dynamic_library.h"
45#include "impl/lass_errno.h"
47
48#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
49
50#include <windows.h>
51
52namespace lass
53{
54namespace util
55{
56
57DynamicLibrary::DynamicLibrary(const std::string& path):
58 pimpl_(0)
59{
60 pimpl_ = ::LoadLibrary(path.c_str());
61 if (pimpl_ == NULL && !stde::ends_with(path, ".dll") && !stde::ends_with(path, ".pyd"))
62 {
63 pimpl_ = ::LoadLibrary( (path + ".dll").c_str() );
64 if (pimpl_ == NULL)
65 {
66 pimpl_ = ::LoadLibrary( (path + ".pyd").c_str() );
67 }
68 }
69 if (pimpl_ == NULL)
70 {
71 LASS_THROW_EX(DynamicLibraryError, "Unable to load '" << path << "': " << impl::lass_FormatMessage(impl::lass_GetLastError()));
72 }
73}
74
75DynamicLibrary::~DynamicLibrary()
76{
77 LASS_ASSERT(pimpl_);
78 ::FreeLibrary(static_cast<HMODULE>(pimpl_));
79}
80
81DynamicLibrary::TFunctionPtr DynamicLibrary::resolveFunctionImpl(const std::string& functionName) const
82{
83 LASS_ASSERT(pimpl_);
84 TFunctionPtr fun = TFunctionPtr(::GetProcAddress(static_cast<HMODULE>(pimpl_), functionName.c_str()));
85 if (!fun)
86 {
87 LASS_THROW_EX(DynamicLibraryError, "Unable to resolve '" << functionName << "': " << impl::lass_FormatMessage(impl::lass_GetLastError()));
88 }
89 return fun;
90}
91
92}
93}
94
95#elif LASS_HAVE_DLOPEN
96
97#include <dlfcn.h>
98
99namespace lass
100{
101namespace util
102{
103
104DynamicLibrary::DynamicLibrary(const std::string& path):
105 pimpl_(0)
106{
107 const int flags = RTLD_NOW | RTLD_GLOBAL;
108 pimpl_ = ::dlopen(path.c_str(), flags);
109 if (pimpl_ == NULL && !stde::ends_with(path, ".so"))
110 {
111 pimpl_ = ::dlopen( (path + ".so").c_str(), flags );
112 }
113 if (pimpl_ == NULL)
114 {
115 const char* msg = ::dlerror();
116 if (msg)
117 {
118 LASS_THROW_EX(DynamicLibraryError, msg);
119 }
120 else
121 {
122 LASS_THROW_EX(DynamicLibraryError, "Unable to load " << path);
123 }
124 }
125}
126
127DynamicLibrary::~DynamicLibrary()
128{
129 LASS_ASSERT(pimpl_);
130 ::dlclose(pimpl_);
131}
132
133DynamicLibrary::TFunctionPtr DynamicLibrary::resolveFunctionImpl(const std::string& functionName) const
134{
135 LASS_ASSERT(pimpl_);
136 TFunctionPtr fun = ::dlsym(pimpl_, functionName.c_str());
137 if (!fun)
138 {
139 const char* msg = ::dlerror();
140 if (msg)
141 {
142 LASS_THROW_EX(DynamicLibraryError, msg);
143 }
144 else
145 {
146 LASS_THROW_EX(DynamicLibraryError, "Unable to resolve " << functionName);
147 }
148 }
149 return fun;
150}
151
152}
153}
154
155#else
156
157# pragma LASS_NOTE("no implementation for DynamicLibraryError, it will be unavailable.")
158
159#endif
160
161// EOF
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53