Add functions to load dynamic libraries from base-lib

This commit is contained in:
David Capello 2016-02-24 13:11:40 -03:00
parent 30672ba144
commit d2f1e2b6d2
5 changed files with 106 additions and 1 deletions

View File

@ -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()

17
src/base/dll.cpp Normal file
View File

@ -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

29
src/base/dll.h Normal file
View File

@ -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 <string>
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<typename T>
inline T get_dll_proc(dll lib, const char* procName) {
return reinterpret_cast<T>(get_dll_proc_base(lib, procName));
}
} // namespace base
#endif

27
src/base/dll_unix.h Normal file
View File

@ -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 <dlfcn.h>
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

29
src/base/dll_win32.h Normal file
View File

@ -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 <windows.h>
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<dll_proc>(
GetProcAddress((HMODULE)lib, procName));
}
} // namespace base