Created
May 26, 2015 08:16
-
-
Save soardex/226cca23abdb6fd2d370 to your computer and use it in GitHub Desktop.
Loading functions from `dll` or `so`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// compiling: | |
/// g++ -std=c++0x -o example example.cpp -I./libtool/include/ -L./libtool/lib/ -lltdl | |
/// | |
#include <cstdio> | |
#include <iostream> | |
#include <memory> | |
#include <ltdl.h> | |
int main() | |
{ | |
lt_dlinit(); | |
lt_dlhandle handle = lt_dlopen("example_dll.dll"); | |
if (handle == nullptr) | |
return 1; | |
const char* error; | |
lt_dlerror(); | |
std::function<void(const char*)> hello = reinterpret_cast<void(__stdcall *)(const char*)>(lt_dlsym(handle, "hello@4")); | |
if ((error = lt_dlerror()) != nullptr) | |
{ | |
std::cout << "Error: " << error << std::endl; | |
return 2; | |
} | |
hello("World"); | |
std::function<int(int)> Double = reinterpret_cast<int(*)(int)>(lt_dlsym(handle, "Double")); | |
if ((error = lt_dlerror()) != nullptr) | |
{ | |
std::cout << "Error: " << error << std::endl; | |
return 2; | |
} | |
std::cout << Double(333) << std::endl; | |
std::function<void(void)> CppFunc = reinterpret_cast<void(*)(void)>(lt_dlsym(handle, "_Z7CppFuncv")); | |
if ((error = lt_dlerror()) != nullptr) | |
{ | |
std::cout << "Error: " << error << std::endl; | |
return 2; | |
} | |
CppFunc(); | |
std::cout << "END OF TEST" << std::endl; | |
lt_dlclose(handle); | |
lt_dlexit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment