📄 mdll.cpp
字号:
#include "MCRT/mdll.h"
#ifndef WIN32
# include <dlfcn.h>
#endif //end !WIN32
MDll::MDll()
:m_handle( M_INVALID_DLL_HANDLE )
{
}
MDll::MDll( const char* pDLLName )
:m_handle( M_INVALID_DLL_HANDLE )
{
// auto load the DLL
if ( pDLLName != NULL )
{
this->Load( pDLLName );
}
}
MDll::~MDll()
{
this->Unload();
}
mInt32 MDll::Load( const char* pDLLName )
{
// check parameter
if ( pDLLName == NULL )
{
return E_PARAM_POINTER_NULL;
}
// check self's status
if ( IsLoaded() )
{
this->Unload();
}
// get loadable full DLL name
std::string strFullDLLName = MDll::GetFullDLLName( pDLLName );
#ifdef WIN32
this->m_handle = ::LoadLibrary( strFullDLLName.c_str() );
#else //! WIN32
this->m_handle = ::dlopen( strFullDLLName.c_str(), RTLD_LAZY );
#endif //end WIN32
if ( this->m_handle != M_INVALID_DLL_HANDLE )
{
return E_SUCCESS;
}
else
{
return E_DLL_LOAD;
}
}
mInt32 MDll::Unload()
{
// unload DLL
// - dec the ref-count of the loaded DLL
// - unmap the DLL while the ref-count reaches 0
if ( this->m_handle != M_INVALID_DLL_HANDLE )
{
#ifdef WIN32
::FreeLibrary( this->m_handle );
#else //! WIN32
::dlclose( this->m_handle );
#endif //end WIN32
this->m_handle = M_INVALID_DLL_HANDLE;
}
return E_SUCCESS;
}
bool MDll::IsLoaded()
{
return (this->m_handle != M_INVALID_DLL_HANDLE);
}
void* MDll::GetSymbol( const char* pSymbolName )
{
// check parameter
if ( pSymbolName == NULL )
{
return NULL;
}
// get symbol with name pSymbolName from DLL
if ( this->m_handle != M_INVALID_DLL_HANDLE )
{
#ifdef WIN32
return (void*)( ::GetProcAddress( this->m_handle, pSymbolName ) );
#else //! WIN32
return ::dlsym( this->m_handle, pSymbolName );
#endif //end WIN32
}
return NULL;
}
std::string MDll::GetRawDLLName( const std::string& strDLLName )
{
std::string strRawDLLName = strDLLName;
// get rid of DLL suffix
{
std::string::size_type lenFull = strRawDLLName.length();
std::string::size_type lenSuffix = ::strlen( DLL_SUFFIX );
if ( lenFull > lenSuffix )
{
std::string strDLLSuffix( strRawDLLName, lenFull - lenSuffix );
if ( strDLLSuffix == DLL_SUFFIX )
{
strRawDLLName.resize( lenFull - lenSuffix );
}
}
}
// get rid of DLL prefix on UNIX-like platform
#ifndef WIN32 //! WIN32
{
std::string::size_type lenFull = strRawDLLName.length();
std::string::size_type lenPrefix = ::strlen( DLL_PREFIX );
if ( lenFull > lenPrefix )
{
std::string strDLLPrefix( strRawDLLName, 0, lenPrefix );
if ( strDLLPrefix == DLL_PREFIX )
{
strRawDLLName = strRawDLLName.c_str() + lenPrefix;
}
}
}
#endif //end !WIN32
return strRawDLLName;
}
std::string MDll::GetFullDLLName( const std::string& strDLLName )
{
std::string strFullDLLName = strDLLName;
// append DLL suffix
{
std::string::size_type lenRaw = strFullDLLName.length();
std::string::size_type lenSuffix = ::strlen( DLL_SUFFIX );
if ( lenRaw > lenSuffix ) // check if need append DLL suffix
{
std::string strDLLSuffix( strFullDLLName, lenRaw - lenSuffix );
if ( strDLLSuffix != DLL_SUFFIX )
{
strFullDLLName.append(DLL_SUFFIX);
}
}
else // no suffix, just append it
{
strFullDLLName.append(DLL_SUFFIX);
}
}
// append DLL prefix on UNIX-like platform
#ifndef WIN32 //! WIN32
{
std::string::size_type lenRaw = strFullDLLName.length();
std::string::size_type lenPrefix = ::strlen( DLL_PREFIX );
if ( lenRaw > lenPrefix ) // check if need append DLL prefix
{
std::string strDLLPrefix( strFullDLLName, 0, lenPrefix );
if ( strDLLPrefix != DLL_PREFIX )
{
strFullDLLName.insert( 0, DLL_PREFIX );
}
}
else
{
strFullDLLName.insert( 0, DLL_PREFIX );
}
}
#endif //end !WIN32
return strFullDLLName;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -