Last active
February 2, 2023 06:36
-
-
Save roachsinai/14b247793fa3fc1d3c89aab7e911ef5e to your computer and use it in GitHub Desktop.
use shared object by dlopen|LoadLibrary
This file contains 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
# origin link: https://gist.github.com/roachsinai/faf8ac80860065a7e78adc0b25abb445 | |
# compilation process: https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Compiler/Linker | |
# Shared library concept: http://ehuss.com/shared/ | |
# 1. Creating the shared library | |
# Command reference: https://www.oreilly.com/library/view/c-cookbook/0596007612/ch01s05.html | |
g++ -dynamiclib -fPIC shared.cpp -o shared.so # MacOS | |
g++ -fPIC -shared shared.cpp -o shared.so # Linux | |
# 2. Creating the main executable file | |
g++ main.cpp -o main | |
# 3. Running main executable | |
./main |
This file contains 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
#include <iostream> | |
#include <dlfcn.h> // https://pubs.opengroup.org/onlinepubs/7908799/xsh/dlfcn.h.html | |
#include "shared.h" | |
int main() { | |
// declaration (template) of add function | |
// function typedef: http://www.iso-9899.info/wiki/Typedef_Function_Type | |
typedef int add_t(int, int); | |
// load shared library | |
// https://pubs.opengroup.org/onlinepubs/7908799/xsh/dlopen.html | |
void *handler = dlopen("./shared.so", RTLD_LAZY); | |
// extract `add` symbol (pointer) and convert it to function pointer | |
// https://pubs.opengroup.org/onlinepubs/7908799/xsh/dlsym.html | |
add_t *add = (add_t*) dlsym( handler, "add" ); | |
// execute function | |
std::cout << (*add)(5, 2) << std::endl; | |
} |
This file contains 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
#include <windows.h> | |
#include <iostream> | |
#include <thread> | |
#include <chrono> | |
/* Define a function pointer for our imported | |
* function. | |
* This reads as "introduce the new type add_t as the type: | |
* pointer to a function returning an int and | |
* taking no arguments. | |
* | |
* Make sure to use matching calling convention (__cdecl, __stdcall, ...) | |
* with the exported function. __stdcall is the convention used by the WinAPI | |
*/ | |
extern "C" typedef void(__stdcall* add_t)(int a, int b); | |
int main() | |
{ | |
#ifdef _DEBUG | |
//HINSTANCE hGetProcIDDLL = LoadLibrary(L"F:/office/VSProject/PoseNcnn/x64/Debug/PoseLib.dll"); | |
#else | |
HINSTANCE hGetProcIDDLL = LoadLibrary(L"./shared.dll"); | |
#endif | |
if (!hGetProcIDDLL) { | |
std::cout << "could not load the dynamic library" << std::endl; | |
return EXIT_FAILURE; | |
} | |
else | |
{ | |
std::cout << "load the dynamic library success" << std::endl; | |
} | |
char data[1000]; | |
// resolve function address here | |
add_t func = (add_t)GetProcAddress(hGetProcIDDLL, "ValidationLicense"); | |
if (!func) { | |
std::cout << "could not locate the function" << std::endl; | |
return EXIT_FAILURE; | |
} | |
else | |
{ | |
std::cout << func(5, 2) << "\n"; | |
} | |
//std::this_thread::sleep_for(std::chrono::milliseconds(200000)); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment