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 
26 #ifdef BOOST_WINDOWS
27 #include <windows.h>
28 #else
29 #include <dlfcn.h>
30 #endif
31 
32 namespace RCF {
33 
34  class DynamicLib
35  {
36  public:
37  DynamicLib(const std::string & dllName);
38  virtual ~DynamicLib();
39 
40  protected:
41 
42  std::string mDllName;
43 
44 #ifdef BOOST_WINDOWS
45 
46  template<typename Pfn>
47  void loadDllFunction(Pfn & pfn, const std::string & funcName)
48  {
49  pfn = NULL;
50  pfn = (Pfn) GetProcAddress(mhDll, funcName.c_str());
51  if (pfn == NULL)
52  {
53  DWORD dwErr = GetLastError();
54  Exception e(_RcfError_DllFuncLoad(mDllName, funcName), dwErr);
55  throw e;
56  }
57  }
58 
59  HMODULE mhDll;
60 
61 #else
62 
63  template<typename Pfn>
64  void loadDllFunction(Pfn & pfn, const std::string & funcName)
65  {
66  pfn = NULL;
67 
68  // Consume any existing error value.
69  const char * szErr = dlerror();
70  RCF_UNUSED_VARIABLE(szErr);
71 
72  pfn = (Pfn) dlsym(mhDll, funcName.c_str());
73  if (pfn == NULL)
74  {
75  std::string strErr;
76  const char * szErr = dlerror();
77  if (szErr)
78  {
79  strErr = szErr;
80  }
81  Exception e(_RcfError_UnixDllFuncLoad(mDllName, funcName, strErr));
82  throw e;
83  }
84  }
85 
86  void * mhDll;
87 
88 #endif
89 
90  };
91 
92 #define RCF_LOAD_DLL_FUNCTION(funcName) \
93  loadDllFunction<Pfn_##funcName>(pfn_##funcName, #funcName);
94 
95 } // namespace RCF
96 
97 #endif // ! INCLUDE_RCF_DYNAMICLIB_HPP