Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ for file in *.cpp *.h *.bat ; do echo "--------------------- $file -----------------" ; cat "$file" ; echo "" ; done
- --------------------- A.cpp -----------------
- #include "stuff.h"
- printFunc_t print=nullptr;
- extern "C" void loadA( printFunc_t func ) {
- print = func;
- (*print)("A: loading libB\n");
- auto B = dlopen("./libB.dll", RTLD_LAZY);
- if ( CheckDlError() != 0 )
- return;
- (*print)("A: getting libB::loadB()\n");
- auto loadB = reinterpret_cast< loaderFunc_t >( dlsym( B, "loadB" ) );
- if ( CheckDlError() != 0 )
- return;
- (*print)("A: calling libB::loadB()\n");
- (*loadB)( func );
- (*print)("A: closing libB\n");
- dlclose(B);
- int a = 0;
- }
- --------------------- B.cpp -----------------
- #include "stuff.h"
- extern "C" void loadB( printFunc_t func ) {
- (*func)("B: I am loaded\n");
- }
- --------------------- stub.cpp -----------------
- #include "stuff.h"
- void Print( const char * msg ) {
- printf("%s", msg);
- fflush(stdout);
- }
- int main() {
- Print("stub: loading libA\n");
- auto A = dlopen("./libA.dll", RTLD_LAZY);
- if ( CheckDlError() != 0 )
- return -1;
- Print("stub: getting libA::loadA()\n");
- auto loadA = reinterpret_cast< loaderFunc_t >( dlsym( A, "loadA" ) );
- if ( CheckDlError() != 0 )
- return -1;
- Print("stub: calling libA::loadA()\n");
- (*loadA)( &Print );
- Print("stub: closing libA\n");
- dlclose(A);
- }
- --------------------- stuff.h -----------------
- #include <cstdio>
- #include <cassert>
- #ifdef _WIN32
- #include "windows.h"
- #define dlopen( file, flag ) LoadLibrary( file )
- #define dlsym( module, resource ) reinterpret_cast<void*>( GetProcAddress( module, resource ) )
- #define dlclose( module ) FreeLibrary( module )
- #elif defined(__linux)
- #include <dlfcn.h>
- #endif
- bool CheckDlError( void );
- inline bool CheckDlError( void ) {
- #ifdef _WIN32
- return GetLastError() == 0;
- #elif defined(__linux)
- const char *error = dlerror();
- if ( error ) {
- printf("Error linking library: %s\n", error);
- return true;
- }
- #endif
- return false;
- }
- typedef void ( *printFunc_t )( const char* msg );
- typedef void ( *loaderFunc_t )( printFunc_t );
- typedef void ( *voidFunc_t )( void );
- --------------------- build.bat -----------------
- rm A.dll B.dll stub.exe 2>>/dev/null
- g++ B.cpp -shared -g -ggdb -Wl,--export-all-symbols -o libB.dll
- g++ A.cpp -shared -g -ggdb -Wl,--export-all-symbols -o libA.dll
- g++ stub.cpp -g -ggdb -Wl,--export-all-symbols -o stub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement