fw4spl
Win32.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifdef _WIN32
8 
9 #include "fwRuntime/dl/Win32.hpp"
10 
11 #include <fwCore/base.hpp>
12 
13 namespace fwRuntime
14 {
15 
16 namespace dl
17 {
18 
19 //------------------------------------------------------------------------------
20 
21 Win32::Win32( const boost::filesystem::path & modulePath ) noexcept :
22  Native(modulePath),
23  m_handle ( 0 )
24 {
25 }
26 
27 //------------------------------------------------------------------------------
28 
29 bool Win32::isLoaded() const noexcept
30 {
31  return m_handle != 0;
32 }
33 
34 //------------------------------------------------------------------------------
35 
36 void * Win32::getSymbol( const std::string& name ) const
37 {
38  FARPROC symbol;
39 
40  symbol = GetProcAddress( m_handle, name.c_str() );
41  if(symbol == 0)
42  {
43  throw RuntimeException("'" + name + "': symbol retrieval failed.");
44  }
45  return symbol;
46 }
47 
48 //------------------------------------------------------------------------------
49 
50 void Win32::load()
51 {
52  if(m_handle == 0)
53  {
54  // Opens the dynamic library.
55  std::string lib(getFullPath(true).string());
56  OSLM_TRACE("Opens the dynamic library " << lib);
57  m_handle = LoadLibrary( lib.c_str() );
58  if(m_handle == 0)
59  {
60  // Retrieves the last error message.
61  DWORD lastError = GetLastError();
62  char buffer[1024];
63  FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, lastError, 0, buffer, 1024, 0 );
64 
65  // Builds the error message and throws the exception.
66  std::string message( buffer );
67  throw RuntimeException( message );
68  }
69  }
70 }
71 
72 //------------------------------------------------------------------------------
73 
74 void Win32::unload()
75 {
76  if(m_handle != 0)
77  {
78  BOOL result;
79  result = FreeLibrary(m_handle);
80  if(!result)
81  {
82  throw RuntimeException("Module unload failed.");
83  }
84  m_handle = 0;
85  }
86 }
87 
88 //------------------------------------------------------------------------------
89 
90 } // namespace dl
91 
92 } // namespace fwRuntime
93 
94 
95 #endif // #ifdef _WIN32
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...