DLibrary.cpp
1 //
3 // Pluma - Plug-in Management Framework
4 // Copyright (C) 2010-2012 Gil Costa (gsaurus@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
27 // Headers
29 #include <Pluma/DLibrary.hpp>
30 #include <cstdio>
31 #include <string>
32 
33 
34 namespace pluma{
35 
37 DLibrary* DLibrary::load(const std::string& path){
38  if ( path.empty() ){
39  fprintf(stderr, "Failed to load library: Empty path\n");
40  return NULL;
41  }
42  void* handle = NULL;
43 
44  // load library - OS dependent operation
45  #ifdef PLUMA_SYS_WINDOWS
46  handle = ::LoadLibraryA(path.c_str());
47  if (!handle){
48  fprintf(stderr, "Failed to load library \"%s\".\n", path.c_str());
49  return NULL;
50  }
51  #else
52  handle = ::dlopen(path.c_str(), RTLD_NOW);
53  if (!handle){
54  const char* errorString = ::dlerror();
55  fprintf(stderr, "Failed to load library \"%s\".", path.c_str());
56  if(errorString) fprintf(stderr, " OS returned error: \"%s\".", errorString);
57  fprintf(stderr, "\n");
58  return NULL;
59  }
60  #endif
61  // return a DLibrary with the DLL handle
62  return new DLibrary(handle);
63 }
64 
65 
68  if (handle){
69  #ifdef PLUMA_SYS_WINDOWS
70  ::FreeLibrary( (HMODULE)handle );
71  #else
72  ::dlclose(handle);
73  #endif
74  }
75 }
76 
77 
79 void* DLibrary::getSymbol(const std::string& symbol){
80  if (!handle){
81  fprintf(stderr, "Cannot inspect library symbols, library isn't loaded.\n");
82  return NULL;
83  }
84  void* res;
85  #ifdef PLUMA_SYS_WINDOWS
86  res = (void*)(::GetProcAddress((HMODULE)handle, symbol.c_str()));
87  #else
88  res = (void*)(::dlsym(handle, symbol.c_str()));
89  #endif
90  if (!res){
91  fprintf(stderr, "Library symbol \"%s\" not found.\n", symbol.c_str());
92  return NULL;
93  }
94  return res;
95 }
96 
97 
99 DLibrary::DLibrary(void* handle):
100  handle(handle)
101 {
102  // Nothing to do
103 }
104 
105 } // namespace pluma
106 
static DLibrary * load(const std::string &path)
Load a library.
Definition: DLibrary.cpp:37
Manages a Dynamic Linking Library.
Definition: DLibrary.hpp:49
void * getSymbol(const std::string &symbol)
Get a symbol from the library.
Definition: DLibrary.cpp:79
~DLibrary()
Destructor.
Definition: DLibrary.cpp:67