Skip to content

Instantly share code, notes, and snippets.

@JamesxX
Last active July 31, 2016 19:13
Show Gist options
  • Save JamesxX/03ca80bd3fd4c251a9bd93a00688b013 to your computer and use it in GitHub Desktop.
Save JamesxX/03ca80bd3fd4c251a9bd93a00688b013 to your computer and use it in GitHub Desktop.
#include "library.cpp"
#include <iostream>
int main() {
std::cout << "Start of program" << std::endl;
typedef int(*myfunc)(int);
JLib::Library MyLibrary("xyz");
myfunc MyFunctionExport = MyLibrary.GetExport<myfunc>("MyFunctionExport");
while (true) {}
return 0;
}
#include "library.hpp"
#ifdef _DEBUG
#include <iostream>
#endif
#ifdef _WIN32
wchar_t * JLib::_WINDOWS_wchar_t(const char* normal) {
size_t insize = strlen(normal);
size_t outSize;
wchar_t *wcstr = new wchar_t[strlen(normal) + 1];
mbstowcs_s(&outSize, wcstr, insize + 1, normal, insize);
return wcstr;
}
#endif
JLib::Library::Library(std::string File) {
#ifdef _WIN32
File += ".dll";
#elif defined __unix__
File += ".so";
#elif defined __APPLE__
File += ".dylib";
#endif
#ifdef _DEBUG
std::cout << "Loading library " << File << std::endl;
#endif
#ifdef _WIN32
wchar_t *wcstr = JLib::_WINDOWS_wchar_t(File.c_str());
this->Module = JLIB_LIBRARY_OPEN(wcstr);
delete[] wcstr;
#else
this->Module = JLIB_LIBRARY_OPEN(File.c_str());
#endif
if (this->Module == NULL) {
#ifdef _DEBUG
std::cout << "Loading library " << File << " failed!" << std::endl;
#endif
throw std::exception("Could not load library");
}
}
JLib::library_export JLib::Library::GetExportRaw(std::string export_name) {
#ifdef _DEBUG
std::cout << "Loading export symbol " << export_name << std::endl;
#endif
if (this->Module != NULL) {
JLib::library_export Export = JLIB_LIBRARY_REQS(this->Module, export_name.c_str());
if (Export == NULL) {
#ifdef _DEBUG
std::cout << "Loading export symbol " << export_name << " failed!" << std::endl;
#endif
throw std::exception( "Could not load export" );
}
else {
return Export;
}
}
return (JLib::library_export)nullptr;
}
JLib::Library::~Library() {
JLIB_LIBRARY_CLOSE(this->Module);
}
#pragma once
#ifdef _WIN32
#include <windows.h>
#define JLIB_LIBRARY_OPEN LoadLibrary
#define JLIB_LIBRARY_REQS(x,y) GetProcAddress(x, (LPSTR) y )
#define JLIB_LIBRARY_CLOSE FreeLibrary
#else
#include <dlfcn.h>
#define JLIB_LIBRARY_OPEN(x) dlopen(x, RTLD_LAZY)
#define JLIB_LIBRARY_REQS dlsym
#define JLIB_LIBRARY_CLOSE dlclose
#endif
#ifdef __APPLE__
#define JLIB_LIBRARY_OPEN(x) dlopen(x, RTLD_LAZY)
#endif
#include <string>
#include <exception>
namespace JLib {
#ifdef _WIN32
typedef HMODULE library_handle;
typedef FARPROC library_export;
#else
typedef void* library_handle;
typedef void* library_export;
#endif
#ifdef _WIN32
wchar_t * _WINDOWS_wchar_t(const char* normal);
#endif
class Library {
public:
Library(std::string File);
library_export GetExportRaw(std::string export_name);
template<typename functype>
functype GetExport(std::string export_name) {
return (functype)GetExportRaw(export_name);
}
~Library();
private:
library_handle Module;
};
} // namespace JLib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment