Version: 9.16.0
DynLibLoaderWin.cxx
Go to the documentation of this file.
1// Copyright (C) 2006-2026 CEA, EDF
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
24using namespace YACS::BASES;
25
26const char DynLibLoaderWin::_extForDynLib[]=".dll";
27
28DynLibLoaderWin::DynLibLoaderWin(const std::string& libNameWithoutExtension):_libName(libNameWithoutExtension),
29 _handleOnLoadedLib(0)
30{
31}
32
34{
36 FreeLibrary(_handleOnLoadedLib);
37}
38
40{
41 return true;
42}
43
45{
46 return _libName;
47}
48
55int DynLibLoaderWin::appendDirInSearchPath(const std::string& dirName)
56{
57 return 0;
58}
59
66int DynLibLoaderWin::removeDirInSearchPath(const std::string& dirName)
67{
68 return 0;
69}
70
71void *DynLibLoaderWin::getHandleOnSymbolWithName(const std::string& symbName, bool stopOnError)
72{
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
86{
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);
94#else
95 const char* aPath = fullLibName.c_str();
96#endif
97
98 _handleOnLoadedLib=LoadLibrary(aPath);
99 return _handleOnLoadedLib != NULL;
100}
101
103{
105 {
106 FreeLibrary(_handleOnLoadedLib);
107 _handleOnLoadedLib = NULL;
108 }
109 return 0;
110}
111
113{
114 unload();
115 return load();
116}
117
118void *DynLibLoaderWin::resolveSymb(const std::string& symbName, bool stopOnError)
119{
120 void *ret=(void*)GetProcAddress(_handleOnLoadedLib,symbName.c_str());
121 char *message="Not available here !";
122 if(stopOnError && (NULL != message))
123 {
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;
127 return 0;
128 }
129 else
130 return ret;
131}
132
134{
135 return _extForDynLib+1;
136}
137
void * resolveSymb(const std::string &symbName, bool stopOnError)
load lib without regarding that _libName is reachable
int appendDirInSearchPath(const std::string &dirName)
void * getHandleOnSymbolWithName(const std::string &symbName, bool stopOnError=true)
int removeDirInSearchPath(const std::string &dirName)
static const char _extForDynLib[]
std::string getLibNameWithoutExt() const
static const char * getExtensionForDynLib()