win32/coil/DynamicLib.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
19 #define TEST_DYNAMIC_LIB 1 // Test for DLL export.
20 #include <coil/DynamicLib.h>
21 
22 namespace coil
23 {
31  DynamicLib::DynamicLib(int close_handle_on_destruction)
32  : m_closeflag(close_handle_on_destruction)
33  {
34  }
35 
43  DynamicLib::DynamicLib(const char* dynlib_name,
44  int open_mode,
45  int close_handle_on_destruction)
46  : m_name(dynlib_name), m_mode(open_mode),
47  m_closeflag(close_handle_on_destruction)
48  {
49  if (open(m_name.c_str(), m_mode, m_closeflag) != 0)
50  {
51  throw std::bad_alloc();
52  }
53  }
54 
63  {
64  close();
65  }
66 
75  : m_name(""), m_mode(0), m_closeflag(0), m_handle(0)
76  {
77  if (!rhs.m_name.empty() &&
78  open(rhs.m_name.c_str(), rhs.m_mode, rhs.m_closeflag) == 0)
79  return;
80  throw std::bad_alloc();
81  }
82 
91  {
92  DynamicLib tmp(rhs);
93  std::swap(this->m_name, tmp.m_name);
94  std::swap(this->m_mode, tmp.m_mode);
95  std::swap(this->m_closeflag, tmp.m_closeflag);
96  std::swap(this->m_handle, tmp.m_handle);
97  return *this;
98  }
99 
107  int DynamicLib::open(const char* dll_name,
108  int open_mode,
109  int close_handle_on_destruction)
110  {
111  HINSTANCE handle = ::LoadLibraryEx(dll_name, NULL, open_mode);
112  if (handle == NULL)
113  {
114  return -1;
115  }
116  m_handle = handle;
117  m_name = dll_name;
118  return 0;
119  }
120 
128  int DynamicLib::close(void)
129  {
130  if (m_handle == NULL)
131  return -1;
132  ::FreeLibrary(m_handle);
133  return 0;
134  }
135 
143  void* DynamicLib::symbol (const char* symbol_name)
144  {
145  if (m_handle == NULL) return NULL;
146  return ::GetProcAddress(m_handle, symbol_name);
147  }
148 
156  const char* DynamicLib::error(void) const
157  {
158  DWORD dwcode;
159  static char cstr[256] = "";
160 
161  /* Get the error code. */
162  dwcode = ::GetLastError();
163 
164  /* Clear the error code. */
165  ::SetLastError(ERROR_SUCCESS);
166 
167  /* Because it is a normal termination when the error code is 0, */
168  /* return NULL. */
169  if(dwcode == 0 )
170  {
171  return NULL;
172  }
173 
174  /* Convert the error code into the message. */
175  ::FormatMessage(
176  FORMAT_MESSAGE_FROM_SYSTEM |
177  FORMAT_MESSAGE_IGNORE_INSERTS,
178  NULL,
179  dwcode,
180  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
181  (char *)cstr,
182  256,
183  NULL
184  );
185  return cstr;
186  }
187 };
188 
192 extern "C"
193 {
195 }
static int ForExternTest(void)
Unit Test.
void * symbol(const char *symbol_name)
Return an address of the memory where a symbol was loaded.
DynamicLib & operator=(const DynamicLib &rhs)
Assignment operator.
DynamicLib(int close_handle_on_destruction=1)
Constructor.
virtual ~DynamicLib()
Destructor.
virtual int close(void)
Unload of the Dynamic link library.
const char * error(void) const
Return the explanation message about the error.
virtual int open(const char *dll_name, int open_mode=COIL_DEFAULT_DYNLIB_MODE, int close_handle_on_destruction=1)
Load of the Dynamic link library.
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:51