00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 #include "dynamic_library.h" 00012 00013 #include <opc/common/addons_core/errors.h> 00014 00015 #include <windows.h> 00016 00017 namespace 00018 { 00019 00020 void* LoadDll(const char* path) 00021 { 00022 // TODO Use SetDllDirctory to search inside installation path. 00023 void* library = LoadLibrary(path); 00024 if (!library) 00025 { 00026 std::string msg; 00027 // TODO Get windows error string. 00028 THROW_ERROR2(UnableToLoadDynamicLibrary, path, msg); 00029 } 00030 return library; 00031 } 00032 00033 } 00034 00035 namespace Common 00036 { 00037 00038 DynamicLibrary::DynamicLibrary(const std::string& libraryPath) 00039 : Path(libraryPath) 00040 , Library(0) 00041 { 00042 } 00043 00044 DynamicLibrary::~DynamicLibrary() 00045 { 00046 if (Library) 00047 { 00048 FreeLibrary((HMODULE)Library); 00049 } 00050 } 00051 00052 void* DynamicLibrary::FindSymbol(const std::string& funcName) 00053 { 00054 if (!Library) 00055 { 00056 Library = LoadDll(Path.c_str()); 00057 } 00058 00059 void* func = (void*)GetProcAddress((HMODULE)Library, funcName.c_str()); 00060 if (!func) 00061 { 00062 std::string msg; 00063 // TODO GetWindows error string. 00064 THROW_ERROR3(UnableToFundSymbolInTheLibrary, funcName, Path, msg); 00065 } 00066 return func; 00067 } 00068 00069 }