RCFProto
 All Classes Functions Typedefs
DynamicLib.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_RCF_DYNAMICLIB_HPP
20 #define INCLUDE_RCF_DYNAMICLIB_HPP
21 
22 #include <string>
23 
24 #include <boost/config.hpp>
25 #include <boost/shared_ptr.hpp>
26 
27 #ifdef BOOST_WINDOWS
28 #include <windows.h>
29 #else
30 #include <dlfcn.h>
31 #endif
32 
33 namespace RCF {
34 
35  class DynamicLib;
36  typedef boost::shared_ptr<DynamicLib> DynamicLibPtr;
37 
38  class DynamicLib
39  {
40  public:
41  DynamicLib(const std::string & dllName);
42  virtual ~DynamicLib();
43 
44  private:
45 
46  std::string mDllName;
47 
48 #ifdef BOOST_WINDOWS
49 
50  public:
51 
52  template<typename Pfn>
53  void loadDllFunction(Pfn & pfn, const std::string & funcName)
54  {
55  pfn = NULL;
56  pfn = (Pfn) GetProcAddress(mhDll, funcName.c_str());
57  if (pfn == NULL)
58  {
59  DWORD dwErr = GetLastError();
60  Exception e(_RcfError_DllFuncLoad(mDllName, funcName), dwErr);
61  throw e;
62  }
63  }
64 
65  private:
66 
67  HMODULE mhDll;
68 
69 #else
70 
71  public:
72 
73  template<typename Pfn>
74  void loadDllFunction(Pfn & pfn, const std::string & funcName)
75  {
76  pfn = NULL;
77 
78  // Consume any existing error value.
79  const char * szErr = dlerror();
80  RCF_UNUSED_VARIABLE(szErr);
81 
82  pfn = (Pfn) dlsym(mhDll, funcName.c_str());
83  if (pfn == NULL)
84  {
85  std::string strErr;
86  const char * szErr = dlerror();
87  if (szErr)
88  {
89  strErr = szErr;
90  }
91  Exception e(_RcfError_UnixDllFuncLoad(mDllName, funcName, strErr));
92  throw e;
93  }
94  }
95 
96  private:
97 
98  void * mhDll;
99 
100 #endif
101 
102  };
103 
104 #define RCF_LOAD_DLL_FUNCTION(funcName) \
105  mDynamicLibPtr->loadDllFunction<Pfn_##funcName>(pfn_##funcName, #funcName);
106 
107 #define RCF_LOAD_LIB_FUNCTION(funcName) \
108  pfn_##funcName = & funcName;
109 
110 } // namespace RCF
111 
112 #endif // ! INCLUDE_RCF_DYNAMICLIB_HPP