Remote Call Framework 3.2
DynamicLib.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2020, 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: 3.2
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 #include <memory>
24 
25 #include <RCF/Config.hpp>
26 #include <RCF/Exception.hpp>
27 
28 #ifdef RCF_WINDOWS
29 #include <windows.h>
30 #else
31 #include <dlfcn.h>
32 #endif
33 
34 namespace RCF {
35 
36  class DynamicLib;
37  typedef std::shared_ptr<DynamicLib> DynamicLibPtr;
38 
39  class DynamicLib
40  {
41  public:
42  DynamicLib(const std::string & dllName);
43  virtual ~DynamicLib();
44 
45  private:
46 
47  std::string mDllName;
48 
49 #ifdef RCF_WINDOWS
50 
51  public:
52 
53  template<typename Pfn>
54  void loadDllFunction(Pfn & pfn, const std::string & funcName)
55  {
56  pfn = NULL;
57  pfn = (Pfn) GetProcAddress(mhDll, funcName.c_str());
58  if (pfn == NULL)
59  {
60  DWORD dwErr = GetLastError();
61  Exception e(RcfError_DllFuncLoad, mDllName, funcName, osError(dwErr));
62  throw e;
63  }
64  }
65 
66  private:
67 
68  HMODULE mhDll;
69 
70 #else
71 
72  public:
73 
74  template<typename Pfn>
75  void loadDllFunction(Pfn & pfn, const std::string & funcName)
76  {
77  pfn = NULL;
78 
79  // Consume any existing error value.
80  const char * szErr = dlerror();
81  RCF_UNUSED_VARIABLE(szErr);
82 
83  pfn = (Pfn) dlsym(mhDll, funcName.c_str());
84  if (pfn == NULL)
85  {
86  std::string strErr;
87  const char * szErr = dlerror();
88  if (szErr)
89  {
90  strErr = szErr;
91  }
92  Exception e(RcfError_UnixDllFuncLoad, mDllName, funcName, strErr);
93  throw e;
94  }
95  }
96 
97  private:
98 
99  void * mhDll;
100 
101 #endif
102 
103  };
104 
105 #define RCF_LOAD_DLL_FUNCTION(funcName) \
106  mDynamicLibPtr->loadDllFunction<Pfn_##funcName>(pfn_##funcName, #funcName);
107 
108 #define RCF_LOAD_LIB_FUNCTION(funcName) \
109  pfn_##funcName = & funcName;
110 
111 } // namespace RCF
112 
113 #endif // ! INCLUDE_RCF_DYNAMICLIB_HPP
Definition: AmiIoHandler.hpp:24