Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / bases / DynLibLoaderWin.cxx
1 //  Copyright (C) 2006-2008  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.
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 #include "DynLibLoaderWin.hxx"
20 #include <iostream>
21 #include <Windows.h>
22
23 using namespace YACS::BASES;
24
25 const char DynLibLoaderWin::_extForDynLib[]=".dll";
26
27 DynLibLoaderWin::DynLibLoaderWin(const std::string& libNameWithoutExtension):_libName(libNameWithoutExtension),
28                                                                              _handleOnLoadedLib(0)
29 {
30 }
31
32 DynLibLoaderWin::~DynLibLoaderWin()
33 {
34   if(_handleOnLoadedLib)
35     dlclose(_handleOnLoadedLib);
36 }
37
38 bool DynLibLoaderWin::isLibFileFindable() const
39 {
40   return true;
41 }
42
43 /*!
44   Append a directory with name \b dirName to the searching paths.
45   \return If append succeeds 0 is returned.
46   If the directory does not exists 1 is returned.
47   If the addition of directory causes some troubles due to seach paths name 2 is returned.
48  */
49 int DynLibLoaderWin::appendDirInSearchPath(const std::string& dirName)
50 {
51   return 0;
52 }
53
54 /*!
55   Removes a directory with name \b dirName from the searching paths.
56   \return If removal succeeds 0 is returned.
57   If the directory does not exists 1 is returned.
58   If the path were not already in existing paths 2 is returned.
59  */
60 int DynLibLoaderWin::removeDirInSearchPath(const std::string& dirName)
61 {
62   return 0;
63 }
64
65 void *DynLibLoaderWin::getHandleOnSymbolWithName(const std::string& symbName)
66 {
67   if(!_handleOnLoadedLib)
68     if(!isLibFileFindable())
69       {
70         std::cerr << "Dynamic library with name " << symbName << _extForDynLib;
71         std::cerr << " not existing in paths specified" << std::endl;
72         return 0;
73       }
74     else
75       loadLib();
76   return resolveSymb(symbName);
77 }
78
79 void DynLibLoaderWin::loadLib()
80 {
81   std::string fullLibName(_libName);
82   fullLibName+=_extForDynLib;
83   _handleOnLoadedLib=LoadLibrary(fullLibName.c_str());
84 }
85
86 void *DynLibLoaderWin::resolveSymb(const std::string& symbName)
87 {
88   void *ret=GetProcAddress(_handleOnLoadedLib,symbName.c_str());
89   return ret;
90 }
91
92 const char *DynLibLoaderWin::getExtensionForDynLib()
93 {
94   return _extForDynLib+1;
95 }
96