1 #include "core/loadlib.hpp"
2 #include "core/command.hpp"
7 function_ptr_command
<arg_filename
> load_lib("load-library", "Load a library",
8 "Syntax: load-library <file>\nLoad library <file>\n",
9 [](arg_filename args
) throw(std::bad_alloc
, std::runtime_error
) {
12 } catch(std::exception
& e
) {
14 x
<< "Can't load '" << std::string(args
) << "': " << e
.what();
15 throw std::runtime_error(x
.str());
20 #if !defined(NO_DLFCN) && !defined(_WIN32) && !defined(_WIN64)
23 void load_library(const std::string
& filename
)
26 getcwd(buffer
, 16383);
27 std::string _filename
= filename
;
28 if(filename
.find_first_of("/") >= filename
.length())
29 _filename
= buffer
+ std::string("/") + filename
;
30 void* h
= dlopen(_filename
.c_str(), RTLD_LOCAL
| RTLD_NOW
);
32 throw std::runtime_error(dlerror());
34 extern const bool load_library_supported
= true;
36 const char* library_is_called
= "Dynamic Library";
37 const char* library_extension
= "bundle";
39 const char* library_is_called
= "Shared Object";
40 const char* library_extension
= "so";
42 #elif defined(_WIN32) || defined(_WIN64)
44 void load_library(const std::string
& filename
)
47 GetCurrentDirectory(16383, buffer
);
48 std::string _filename
= filename
;
49 if(filename
.find_first_of("/\\") >= filename
.length())
50 _filename
= buffer
+ std::string("/") + filename
;
51 HMODULE h
= LoadLibraryA(_filename
.c_str());
53 int errcode
= GetLastError();
54 char errorbuffer
[1024];
55 if(FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, errcode
, 0,
56 errorbuffer
, sizeof(errorbuffer
), NULL
))
57 throw std::runtime_error(errorbuffer
);
59 std::ostringstream str
;
60 str
<< "Unknown system error (code " << errcode
<< ")";
61 throw std::runtime_error(str
.str());
65 extern const bool load_library_supported
= true;
66 const char* library_is_called
= "Dynamic Link Library";
67 const char* library_extension
= "dll";
69 void load_library(const std::string
& filename
)
71 throw std::runtime_error("Library loader not supported on this platform");
73 extern const bool load_library_supported
= false;
74 const char* library_is_called
= NULL
;
75 const char* library_extension
= NULL
;