HMSBEAGLE  1.0.0
libhmsbeagle/plugin/WinSharedLibrary.h
00001 
00008 #ifndef __WINSHAREDLIBRARY_H__
00009 #define __WINSHAREDLIBRARY_H__
00010 
00011 #ifdef HAVE_CONFIG_H
00012 #include "libhmsbeagle/config.h"
00013 #endif
00014 
00015 #include "libhmsbeagle/plugin/SharedLibrary.h"
00016 #include <windows.h>
00017 #include <string>
00018 #include <iostream>
00019 
00020 namespace beagle {
00021 namespace plugin {
00022 
00023 using namespace std;
00024 
00025 class WinSharedLibrary : public SharedLibrary
00026 {
00027   public:
00028     WinSharedLibrary(const char* name)
00029     throw (SharedLibraryException);
00030     ~WinSharedLibrary();
00031 
00032     void* findSymbol(const char* name)
00033     throw (SharedLibraryException);
00034 
00035   private:
00036     HINSTANCE m_handle;
00037 };
00038 WinSharedLibrary::WinSharedLibrary(const char* name)
00039     throw (SharedLibraryException)
00040     : m_handle(0)
00041 {
00042         std::string libname = name;
00043 #ifdef _WIN64
00044 #ifdef _DEBUG
00045         libname += "64D";
00046 #else
00047         libname += "64";
00048 #endif
00049 #else
00050 #ifdef _DEBUG
00051         libname += "32D";
00052 #else
00053         libname += "32";
00054 #endif
00055 #endif
00056         UINT emode = SetErrorMode(SEM_FAILCRITICALERRORS);
00057     m_handle = LoadLibrary(libname.c_str());
00058         SetErrorMode(emode);
00059     if (m_handle == 0)
00060     {
00061     char buffer[255];
00062     strcpy(buffer,"Open Library Failure");
00063     FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),
00064         0, buffer,sizeof(buffer),0);
00065     throw SharedLibraryException(buffer);
00066     }
00067 }
00068 WinSharedLibrary::~WinSharedLibrary()
00069 {
00070     if (!FreeLibrary(m_handle))
00071     {
00072     char buffer[255];
00073     // format buffer as above
00074     cerr << buffer << endl;
00075     }
00076 }
00077 void* WinSharedLibrary::findSymbol(const char* name)
00078     throw (SharedLibraryException)
00079 {
00080     void* sym = GetProcAddress(m_handle,name);
00081     if (sym == 0)
00082     {
00083     char buffer[255];
00084     // format buffer as above
00085     throw SharedLibraryException(buffer);
00086     }
00087     else
00088    return sym;
00089 }
00090 
00091 } // namespace plugin
00092 } // namespace beagle
00093 
00094 
00095 #endif  // __WINSHAREDLIBRARY_H__
00096