DynamicLib.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #define TEST_DYNAMIC_LIB  1   // Test for DLL export.
00020 #include <coil/DynamicLib.h>
00021 
00022 namespace coil
00023 {
00031   DynamicLib::DynamicLib(int close_handle_on_destruction)
00032     : m_closeflag(close_handle_on_destruction)
00033   {
00034   }
00035 
00043   DynamicLib::DynamicLib(const char* dynlib_name,
00044                          int open_mode,
00045                          int close_handle_on_destruction)
00046     : m_name(dynlib_name), m_mode(open_mode),
00047       m_closeflag(close_handle_on_destruction)
00048   {
00049     if (open(m_name.c_str(), m_mode, m_closeflag) != 0)
00050       {
00051         throw std::bad_alloc();
00052       }
00053   }
00054 
00062   DynamicLib::~DynamicLib()
00063   {
00064     close();
00065   }
00066 
00074   DynamicLib::DynamicLib(const DynamicLib& rhs)
00075     : m_name(""), m_mode(0), m_closeflag(0), m_handle(0)
00076   {
00077     if (!rhs.m_name.empty() &&
00078         open(rhs.m_name.c_str(), rhs.m_mode, rhs.m_closeflag) == 0)
00079       return;
00080     throw std::bad_alloc();
00081   }
00082 
00090   DynamicLib& DynamicLib::operator=(const DynamicLib& rhs)
00091   {
00092     DynamicLib tmp(rhs);
00093     std::swap(this->m_name, tmp.m_name);
00094     std::swap(this->m_mode, tmp.m_mode);
00095     std::swap(this->m_closeflag, tmp.m_closeflag);
00096     std::swap(this->m_handle, tmp.m_handle);
00097     return *this;
00098   }
00099 
00107   int DynamicLib::open(const char* dll_name,
00108                    int open_mode,
00109                    int close_handle_on_destruction)
00110   {
00111     HINSTANCE handle = ::LoadLibraryEx(dll_name, NULL, open_mode);
00112     if (handle == NULL)
00113       {
00114         return -1;
00115       }
00116     m_handle = handle;
00117     m_name = dll_name;
00118     return 0;
00119   }
00120 
00128   int DynamicLib::close(void)
00129   {
00130     if (m_handle == NULL)
00131       return -1;
00132     ::FreeLibrary(m_handle);
00133     return 0;
00134   }
00135 
00143   void* DynamicLib::symbol (const char* symbol_name)
00144   {
00145     if (m_handle == NULL) return NULL;
00146     return ::GetProcAddress(m_handle, symbol_name);
00147   }
00148 
00156   const char* DynamicLib::error(void) const
00157   {
00158     DWORD dwcode;
00159     static char cstr[256] = "";
00160 
00161     /* Get the error code. */
00162     dwcode = ::GetLastError();
00163 
00164     /* Clear the error code. */
00165     ::SetLastError(ERROR_SUCCESS);
00166 
00167     /* Because it is a normal termination when the error code is 0, */
00168     /* return NULL. */
00169     if(dwcode == 0 ) 
00170     {
00171       return NULL;
00172     }
00173 
00174     /* Convert the error code into the message. */
00175         ::FormatMessage(
00176                 FORMAT_MESSAGE_FROM_SYSTEM |
00177                 FORMAT_MESSAGE_IGNORE_INSERTS,
00178                 NULL,
00179                 dwcode,
00180                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00181                 (char *)cstr,
00182                 256,
00183                 NULL
00184         );
00185     return cstr;
00186   }
00187 };
00188 
00192 extern "C"
00193 {
00194   int ForExternTest(void) { return coil::DynamicLib::ForExternTest(); }
00195 }


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Aug 27 2015 14:16:37