Remote Call Framework 3.3
DynamicLib.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2022, Delta V Software. All rights reserved.
6 // https://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 under GPL terms.
12 //
13 // Version: 3.3
14 // Contact: support <at> deltavsoft.com
15 //
16 //******************************************************************************
17 
18 #ifndef INCLUDE_RCF_DYNAMICLIB_HPP
19 #define INCLUDE_RCF_DYNAMICLIB_HPP
20 
21 #include <string>
22 #include <memory>
23 
24 #include <RCF/Config.hpp>
25 #include <RCF/Exception.hpp>
26 
27 #ifdef RCF_WINDOWS
28 #include <windows.h>
29 #else
30 #include <dlfcn.h>
31 #endif
32 
33 namespace RCF {
34 
35  class DynamicLib;
36  typedef std::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 RCF_WINDOWS
49 
50  public:
51 
52  template<typename Pfn>
53  void loadDllFunction(Pfn & pfn, const std::string & funcName, bool ignoreMisingFunc = false)
54  {
55  pfn = NULL;
56  pfn = (Pfn) GetProcAddress(mhDll, funcName.c_str());
57  if ( pfn == NULL && !ignoreMisingFunc )
58  {
59  DWORD dwErr = GetLastError();
60  Exception e(RcfError_DllFuncLoad, mDllName, funcName, osError(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, bool ignoreMisingFunc = false)
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 && !ignoreMisingFunc )
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
Definition: AmiIoHandler.hpp:23