HMSBEAGLE  1.0.0
WinSharedLibrary.h
1 
8 #ifndef __WINSHAREDLIBRARY_H__
9 #define __WINSHAREDLIBRARY_H__
10 
11 #ifdef HAVE_CONFIG_H
12 #include "libhmsbeagle/config.h"
13 #endif
14 
15 #include "libhmsbeagle/plugin/SharedLibrary.h"
16 #include <windows.h>
17 #include <string>
18 #include <iostream>
19 
20 namespace beagle {
21 namespace plugin {
22 
23 using namespace std;
24 
26 {
27  public:
28  WinSharedLibrary(const char* name)
29  throw (SharedLibraryException);
31 
32  void* findSymbol(const char* name)
33  throw (SharedLibraryException);
34 
35  private:
36  HINSTANCE m_handle;
37 };
38 WinSharedLibrary::WinSharedLibrary(const char* name)
40  : m_handle(0)
41 {
42  std::string libname = name;
43 #ifdef _WIN64
44 #ifdef _DEBUG
45  libname += "64D";
46 #else
47  libname += "64";
48 #endif
49 #else
50 #ifdef _DEBUG
51  libname += "32D";
52 #else
53  libname += "32";
54 #endif
55 #endif
56  UINT emode = SetErrorMode(SEM_FAILCRITICALERRORS);
57  m_handle = LoadLibrary(libname.c_str());
58  SetErrorMode(emode);
59  if (m_handle == 0)
60  {
61  char buffer[255];
62  strcpy(buffer,"Open Library Failure");
63  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),
64  0, buffer,sizeof(buffer),0);
65  throw SharedLibraryException(buffer);
66  }
67 }
68 WinSharedLibrary::~WinSharedLibrary()
69 {
70  if (!FreeLibrary(m_handle))
71  {
72  char buffer[255];
73  // format buffer as above
74  cerr << buffer << endl;
75  }
76 }
77 void* WinSharedLibrary::findSymbol(const char* name)
78  throw (SharedLibraryException)
79 {
80  void* sym = GetProcAddress(m_handle,name);
81  if (sym == 0)
82  {
83  char buffer[255];
84  // format buffer as above
85  throw SharedLibraryException(buffer);
86  }
87  else
88  return sym;
89 }
90 
91 } // namespace plugin
92 } // namespace beagle
93 
94 
95 #endif // __WINSHAREDLIBRARY_H__
96