Main Page   Class Hierarchy   Compound List   File List   Compound Members  

DynLib.cpp

00001 #include "DynLib.h"
00002 #include <iostream>
00003 
00004 using namespace SOME;
00005 
00006 DynLib::DynLib():
00007         lib(NULL)
00008 {}
00009 
00010 
00011 
00012 bool DynLib::loadLibrary(const char* filepath)
00013 {
00014     unloadLibrary();
00015 
00016     lib = LoadLibrary(filepath);
00017     if (lib == NULL)
00018     {
00019         LPVOID lpMsgBuf;
00020         FormatMessage(
00021             FORMAT_MESSAGE_ALLOCATE_BUFFER |
00022             FORMAT_MESSAGE_FROM_SYSTEM |
00023             FORMAT_MESSAGE_IGNORE_INSERTS,
00024             NULL,
00025             GetLastError(),
00026             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   // Default language
00027             (LPTSTR) &lpMsgBuf,
00028             0,
00029             NULL
00030         );
00031         // Process any inserts in lpMsgBuf.
00032         // ...
00033         // Display the string.
00034         std::cerr << "Error Loading " << filepath << ": " << (LPCTSTR)lpMsgBuf << std::endl;
00035         // Free the buffer.
00036         LocalFree( lpMsgBuf );
00037 
00038         return valid_lib = false;
00039     }
00040 
00041     file_path = filepath;
00042     return valid_lib = true;
00043 }
00044 
00045 void* DynLib::getFunction(const char* func)
00046 {
00047     if (!valid_lib)
00048     {
00049         std::cerr << "Cannot getFunction out of invalid lib\n";
00050         return 0;
00051     }
00052 
00053     void* func_ptr = (void*)GetProcAddress(lib, func);
00054 
00055     if (!func_ptr)
00056     {
00057         LPVOID lpMsgBuf;
00058         FormatMessage(
00059             FORMAT_MESSAGE_ALLOCATE_BUFFER |
00060             FORMAT_MESSAGE_FROM_SYSTEM |
00061             FORMAT_MESSAGE_IGNORE_INSERTS,
00062             NULL,
00063             GetLastError(),
00064             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   // Default language
00065             (LPTSTR) &lpMsgBuf,
00066             0,
00067             NULL
00068         );
00069         std::cerr << "Could not load function: " << func << ". Error message: " << (LPCTSTR)lpMsgBuf << std::endl;
00070         LocalFree(lpMsgBuf);
00071         valid_lib = false;
00072         return 0;
00073     }
00074     return func_ptr;
00075 }
00076 
00077 void DynLib::unloadLibrary()
00078 {
00079     if (lib)
00080     {
00081         if (FreeLibrary(lib) == 0)
00082         {
00083             LPVOID lpMsgBuf;
00084             FormatMessage(
00085                 FORMAT_MESSAGE_ALLOCATE_BUFFER |
00086                 FORMAT_MESSAGE_FROM_SYSTEM |
00087                 FORMAT_MESSAGE_IGNORE_INSERTS,
00088                 NULL,
00089                 GetLastError(),
00090                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),   // Default language
00091                 (LPTSTR) &lpMsgBuf,
00092                 0,
00093                 NULL
00094             );
00095             // Process any inserts in lpMsgBuf.
00096             // ...
00097             // Display the string.
00098             std::cerr << (LPCTSTR)lpMsgBuf << std::endl;
00099             // Free the buffer.
00100             LocalFree( lpMsgBuf );
00101         }
00102     }
00103     lib = NULL;
00104     valid_lib = false;
00105 }
00106 
00107 bool DynLib::parsePath( const std::string &path, std::string &return_dir, std::string &return_pattern )
00108 {
00109     return false;
00110 }
00111 
00112 std::list < std::string > DynLib::enumPlatformDirectory( const std::string& path )
00113 {
00114     std::list < std::string > willis;
00115     return willis;
00116 }

Generated at Fri Dec 8 14:24:48 2000 for SOMELib by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000