diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt index ca43bb736..77c1424fd 100644 --- a/src/base/CMakeLists.txt +++ b/src/base/CMakeLists.txt @@ -1,5 +1,5 @@ # Aseprite Base Library -# Copyright (c) 2001-2015 David Capello +# Copyright (c) 2001-2016 David Capello include(CheckCSourceCompiles) include(CheckCXXSourceCompiles) @@ -48,6 +48,7 @@ set(BASE_SOURCES connection.cpp convert_to.cpp debug.cpp + dll.cpp errno_string.cpp exception.cpp file_handle.cpp @@ -83,4 +84,6 @@ target_link_libraries(base-lib modp_b64) if(WIN32) target_link_libraries(base-lib dbghelp shlwapi) +else() + target_link_libraries(base-lib dl) endif() diff --git a/src/base/dll.cpp b/src/base/dll.cpp new file mode 100644 index 000000000..e4b52d3fc --- /dev/null +++ b/src/base/dll.cpp @@ -0,0 +1,17 @@ +// Aseprite Base Library +// Copyright (c) 2016 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "base/dll.h" + +#ifdef _WIN32 + #include "base/dll_win32.h" +#else + #include "base/dll_unix.h" +#endif diff --git a/src/base/dll.h b/src/base/dll.h new file mode 100644 index 000000000..e69975827 --- /dev/null +++ b/src/base/dll.h @@ -0,0 +1,29 @@ +// Aseprite Base Library +// Copyright (c) 2016 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifndef BASE_DLL_H_INCLUDED +#define BASE_DLL_H_INCLUDED +#pragma once + +#include + +namespace base { + +typedef void* dll; +typedef void* dll_proc; + +dll load_dll(const std::string& filename); +void unload_dll(dll lib); +dll_proc get_dll_proc_base(dll lib, const char* procName); + +template +inline T get_dll_proc(dll lib, const char* procName) { + return reinterpret_cast(get_dll_proc_base(lib, procName)); +} + +} // namespace base + +#endif diff --git a/src/base/dll_unix.h b/src/base/dll_unix.h new file mode 100644 index 000000000..127b28366 --- /dev/null +++ b/src/base/dll_unix.h @@ -0,0 +1,27 @@ +// Aseprite Base Library +// Copyright (c) 2016 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#include "base/string.h" +#include + +namespace base { + +dll load_dll(const std::string& filename) +{ + return dlopen(filename.c_str(), RTLD_LAZY | RTLD_GLOBAL); +} + +void unload_dll(dll lib) +{ + dlclose(lib); +} + +dll_proc get_dll_proc_base(dll lib, const char* procName) +{ + return dlsym(lib, procName); +} + +} // namespace base diff --git a/src/base/dll_win32.h b/src/base/dll_win32.h new file mode 100644 index 000000000..537832104 --- /dev/null +++ b/src/base/dll_win32.h @@ -0,0 +1,29 @@ +// Aseprite Base Library +// Copyright (c) 2016 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#include "base/string.h" +#include + +namespace base { + +dll load_dll(const std::string& filename) +{ + return LoadLibrary(base::from_utf8(filename).c_str()); +} + +void unload_dll(dll lib) +{ + FreeLibrary((HMODULE)lib); +} + +dll_proc get_dll_proc_base(dll lib, const char* procName) +{ + return reinterpret_cast( + GetProcAddress((HMODULE)lib, procName)); +} + + +} // namespace base