Claw  1.7.3
dynamic_library_traits_win32.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #ifndef __CLAW_DYNAMIC_LIBRARY_TRAITS_WIN32_HPP__
31 #define __CLAW_DYNAMIC_LIBRARY_TRAITS_WIN32_HPP__
32 
33 #include <string>
34 //#include <cstdarg>
35 //#include <windef.h>
36 //#include <winbase.h>
37 #include <windows.h>
38 
39 namespace claw
40 {
46  {
47  public:
49  typedef HMODULE handle;
50 
51  public:
52  /*------------------------------------------------------------------------*/
58  static handle open( const std::string& name )
59  {
60  return LoadLibrary( name.c_str() );
61  } // dynamic_library_traits_win32::open()
62 
63  /*------------------------------------------------------------------------*/
69  static handle auto_open( const std::string& name )
70  {
71  return LoadLibrary( name.c_str() );
72  } // dynamic_library_traits_win32::auto_open()
73 
74  /*------------------------------------------------------------------------*/
79  static void close( handle h )
80  {
81  FreeLibrary(h);
82  } // dynamic_library_traits_win32::close()
83 
84  /*------------------------------------------------------------------------*/
90  template<class T>
91  static T get_symbol( handle h, const std::string& name )
92  {
93  /* HACK : ISO standard doesn't allow to cast from a pointer to an object
94  to a pointer to a function. */
95  T result;
96  *(FARPROC*)(&result) = GetProcAddress( h, name.c_str() );
97 
98  return result;
99  } // dynamic_library_traits_win32::get_symbol()
100 
101  /*------------------------------------------------------------------------*/
107  static bool have_symbol( handle h, const std::string& name )
108  {
109  return GetProcAddress( h, name.c_str() ) != NULL;
110  } // dynamic_library_traits_win32::have_symbol()
111 
112  /*------------------------------------------------------------------------*/
117  static bool valid_handle( handle h )
118  {
119  return h != NULL;
120  } // dynamic_library_traits_win32::valid_handle()
121 
122  }; // class dynamic_library_traits_win32
123 
124  typedef dynamic_library_traits_win32 dynamic_library_traits;
125 } // namespace claw
126 
127 #endif // __CLAW_DYNAMIC_LIBRARY_TRAITS_WIN32_HPP__