Salome HOME
Merge branch 'V7_dev'
[modules/yacs.git] / src / bases / DynLibLoaderWin.cxx
1 // Copyright (C) 2006-2016  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "DynLibLoaderWin.hxx"
21 #include <iostream>
22 #include <Windows.h>
23
24 using namespace YACS::BASES;
25
26 const char DynLibLoaderWin::_extForDynLib[]=".dll";
27
28 DynLibLoaderWin::DynLibLoaderWin(const std::string& libNameWithoutExtension):_libName(libNameWithoutExtension),
29                                                                              _handleOnLoadedLib(0)
30 {
31 }
32
33 DynLibLoaderWin::~DynLibLoaderWin()
34 {
35   if(_handleOnLoadedLib)
36     FreeLibrary(_handleOnLoadedLib);
37 }
38
39 bool DynLibLoaderWin::isLibFileFindable() const
40 {
41   return true;
42 }
43
44 std::string DynLibLoaderWin::getLibNameWithoutExt() const
45 {
46   return _libName;
47 }
48
49 /*!
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.
54  */
55 int DynLibLoaderWin::appendDirInSearchPath(const std::string& dirName)
56 {
57   return 0;
58 }
59
60 /*!
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.
65  */
66 int DynLibLoaderWin::removeDirInSearchPath(const std::string& dirName)
67 {
68   return 0;
69 }
70
71 void *DynLibLoaderWin::getHandleOnSymbolWithName(const std::string& symbName, bool stopOnError)
72 {
73   if(!_handleOnLoadedLib)
74     if(!isLibFileFindable())
75       {
76         std::cerr << "Dynamic library with name " << symbName << _extForDynLib;
77         std::cerr << " not existing in paths specified" << std::endl;
78         return 0;
79       }
80     else
81       loadLib();
82   return resolveSymb(symbName, stopOnError);
83 }
84
85 bool DynLibLoaderWin::load()
86 {
87   std::string fullLibName(_libName);
88   fullLibName+=_extForDynLib;
89   _handleOnLoadedLib=LoadLibrary(fullLibName.c_str());
90   return _handleOnLoadedLib != NULL;
91 }
92
93 bool DynLibLoaderWin::unload()
94 {
95   if (_handleOnLoadedLib) 
96     {
97       FreeLibrary(_handleOnLoadedLib);
98       _handleOnLoadedLib = NULL;
99     }
100   return 0;
101 }
102
103 bool DynLibLoaderWin::reload()
104 {
105   unload();
106   return load();
107 }
108
109 void *DynLibLoaderWin::resolveSymb(const std::string& symbName, bool stopOnError)
110 {
111   void *ret=(void*)GetProcAddress(_handleOnLoadedLib,symbName.c_str());
112   char *message="Not available here !";
113   if(stopOnError && (NULL != message))
114     {
115       std::cerr << "Error detected on symbol " << symbName << " search in library with name " << _libName << _extForDynLib;
116       std::cerr << " with the following internal message"<<  std::endl; 
117       std::cerr << message << std::endl;
118       return 0;
119     }
120   else
121     return ret;
122 }
123
124 const char *DynLibLoaderWin::getExtensionForDynLib()
125 {
126   return _extForDynLib+1;
127 }
128