00001 #include <string> 00002 #include <mutex> 00003 #include <Windows.h> 00004 #include "behaviortree_cpp/utils/shared_library.h" 00005 #include "behaviortree_cpp/exceptions.h" 00006 00007 namespace BT 00008 { 00009 SharedLibrary::SharedLibrary() 00010 { 00011 _handle = nullptr; 00012 } 00013 00014 void SharedLibrary::load(const std::string& path, int) 00015 { 00016 std::unique_lock<std::mutex> lock(_mutex); 00017 00018 _handle = LoadLibrary(path.c_str()); 00019 if (!_handle) 00020 { 00021 throw RuntimeError("Could not load library: " + path); 00022 } 00023 _path = path; 00024 } 00025 00026 void SharedLibrary::unload() 00027 { 00028 std::unique_lock<std::mutex> lock(_mutex); 00029 00030 if (_handle) 00031 { 00032 FreeLibrary((HMODULE)_handle); 00033 _handle = 0; 00034 } 00035 _path.clear(); 00036 } 00037 00038 bool SharedLibrary::isLoaded() const 00039 { 00040 return _handle != nullptr; 00041 } 00042 00043 void* SharedLibrary::findSymbol(const std::string& name) 00044 { 00045 std::unique_lock<std::mutex> lock(_mutex); 00046 00047 if (_handle) 00048 { 00049 #if defined(_WIN32_WCE) 00050 std::wstring uname; 00051 UnicodeConverter::toUTF16(name, uname); 00052 return (void*)GetProcAddressW((HMODULE)_handle, uname.c_str()); 00053 #else 00054 return (void*)GetProcAddress((HMODULE)_handle, name.c_str()); 00055 #endif 00056 } 00057 00058 return 0; 00059 } 00060 00061 const std::string& SharedLibrary::getPath() const 00062 { 00063 return _path; 00064 } 00065 00066 std::string SharedLibrary::prefix() 00067 { 00068 return ""; 00069 } 00070 00071 std::string SharedLibrary::suffix() 00072 { 00073 #if defined(_DEBUG) 00074 return "d.dll"; 00075 #else 00076 return ".dll"; 00077 #endif 00078 } 00079 00080 } // namespace BT