1 ////////////////////////////////////////////////////////////
\r
3 // Pluma - Plug-in Management Framework
\r
4 // Copyright (C) 2010-2012 Gil Costa (gsaurus@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
26 ////////////////////////////////////////////////////////////
\r
28 ////////////////////////////////////////////////////////////
\r
29 #include <Pluma/DLibrary.hpp>
\r
32 #include <stdexcept>
\r
37 ////////////////////////////////////////////////////////////
\r
38 DLibrary* DLibrary::load(const std::string& path){
\r
39 if ( path.empty() ){
\r
40 // fprintf(stderr, "Failed to load library: Empty path\n");
\r
41 throw std::runtime_error( "Failed to load library: Empty path." );
\r
44 void* handle = NULL;
\r
46 // load library - OS dependent operation
\r
47 #ifdef PLUMA_SYS_WINDOWS
\r
48 handle = ::LoadLibraryA(path.c_str());
\r
50 //fprintf(stderr, "Failed to load library \"%s\".\n", path.c_str());
\r
51 throw std::runtime_error(
\r
52 std::string( "Failed to load library \"" ) +
\r
54 std::string( "\"." )
\r
59 handle = ::dlopen(path.c_str(), RTLD_NOW);
\r
61 const char* errorString = ::dlerror();
\r
62 std::string error_text = "Failed to load library \"";
\r
63 // fprintf(stderr, "Failed to load library \"%s\".", path.c_str());
\r
64 if(errorString) error_text += std::string( errorString ); // fprintf(stderr, " OS returned error: \"%s\".", errorString);
\r
65 // fprintf(stderr, "\n");
\r
66 throw std::runtime_error( error_text + std::string( "\"." ) );
\r
70 // return a DLibrary with the DLL handle
\r
71 return new DLibrary(handle);
\r
75 ////////////////////////////////////////////////////////////
\r
76 DLibrary::~DLibrary(){
\r
78 #ifdef PLUMA_SYS_WINDOWS
\r
79 ::FreeLibrary( (HMODULE)handle );
\r
87 ////////////////////////////////////////////////////////////
\r
88 void* DLibrary::getSymbol(const std::string& symbol){
\r
90 //fprintf(stderr, "Cannot inspect library symbols, library isn't loaded.\n");
\r
91 throw std::runtime_error( "Cannot inspect library symbols, library isn't loaded." );
\r
95 #ifdef PLUMA_SYS_WINDOWS
\r
96 res = (void*)(::GetProcAddress((HMODULE)handle, symbol.c_str()));
\r
98 res = (void*)(::dlsym(handle, symbol.c_str()));
\r
101 // fprintf(stderr, "Library symbol \"%s\" not found.\n", symbol.c_str());
\r
102 throw std::runtime_error(
\r
103 std::string( "Library symbol \"" ) +
\r
105 std::string( "\" not found." )
\r
113 ////////////////////////////////////////////////////////////
\r
114 DLibrary::DLibrary(void* handle):
\r
120 } // namespace pluma
\r