1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "DynLibLoaderWin.hxx"
24 using namespace YACS::BASES;
26 const char DynLibLoaderWin::_extForDynLib[]=".dll";
28 DynLibLoaderWin::DynLibLoaderWin(const std::string& libNameWithoutExtension):_libName(libNameWithoutExtension),
33 DynLibLoaderWin::~DynLibLoaderWin()
35 if(_handleOnLoadedLib)
36 FreeLibrary(_handleOnLoadedLib);
39 bool DynLibLoaderWin::isLibFileFindable() const
44 std::string DynLibLoaderWin::getLibNameWithoutExt() const
50 Append a directory with name \b dirName to the searching paths.
51 \return If append succeeds 0 is returned.
52 If the directory does not exists 1 is returned.
53 If the addition of directory causes some troubles due to seach paths name 2 is returned.
55 int DynLibLoaderWin::appendDirInSearchPath(const std::string& dirName)
61 Removes a directory with name \b dirName from the searching paths.
62 \return If removal succeeds 0 is returned.
63 If the directory does not exists 1 is returned.
64 If the path were not already in existing paths 2 is returned.
66 int DynLibLoaderWin::removeDirInSearchPath(const std::string& dirName)
71 void *DynLibLoaderWin::getHandleOnSymbolWithName(const std::string& symbName, bool stopOnError)
73 if(!_handleOnLoadedLib)
74 if(!isLibFileFindable())
76 std::cerr << "Dynamic library with name " << symbName << _extForDynLib;
77 std::cerr << " not existing in paths specified" << std::endl;
82 return resolveSymb(symbName, stopOnError);
85 bool DynLibLoaderWin::load()
87 std::string fullLibName(_libName);
88 fullLibName+=_extForDynLib;
89 #if defined(UNICODE) && defined(WIN32)
90 size_t length = strlen(fullLibName.c_str()) + sizeof(char);
91 wchar_t* aPath = new wchar_t[length + 1];
92 memset(aPath, '\0', length);
93 mbstowcs(aPath, fullLibName.c_str(), length);
95 const char* aPath = fullLibName.c_str();
98 _handleOnLoadedLib=LoadLibrary(aPath);
99 return _handleOnLoadedLib != NULL;
102 bool DynLibLoaderWin::unload()
104 if (_handleOnLoadedLib)
106 FreeLibrary(_handleOnLoadedLib);
107 _handleOnLoadedLib = NULL;
112 bool DynLibLoaderWin::reload()
118 void *DynLibLoaderWin::resolveSymb(const std::string& symbName, bool stopOnError)
120 void *ret=(void*)GetProcAddress(_handleOnLoadedLib,symbName.c_str());
121 char *message="Not available here !";
122 if(stopOnError && (NULL != message))
124 std::cerr << "Error detected on symbol " << symbName << " search in library with name " << _libName << _extForDynLib;
125 std::cerr << " with the following internal message"<< std::endl;
126 std::cerr << message << std::endl;
133 const char *DynLibLoaderWin::getExtensionForDynLib()
135 return _extForDynLib+1;