From 00c6e3575d56e982e3528c43d73eb588ff3ff7ac Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sun, 13 Jun 2010 15:27:29 +0200 Subject: renamed wolf_library_get_func to wolf_library_get as we can load variables in DLLs added tests for variables and structs in DLLs (Windows) --- tests/library/Makefile.W32 | 2 +- tests/library/test_loader.c | 41 ++++++++++++++++++++++++++++++++++++++++- tests/library/testlib.c | 14 ++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/library/Makefile.W32 b/tests/library/Makefile.W32 index 168705d..0ef5a2f 100644 --- a/tests/library/Makefile.W32 +++ b/tests/library/Makefile.W32 @@ -35,4 +35,4 @@ local_distclean: local_test: $(LIBRARIES) @echo Testing loading of a library and executing a function therein.. - @test_loader 2>NUL + @test_loader 1>NUL 2>NUL diff --git a/tests/library/test_loader.c b/tests/library/test_loader.c index 37b549b..60f2514 100644 --- a/tests/library/test_loader.c +++ b/tests/library/test_loader.c @@ -27,6 +27,16 @@ #define LIBRARY_NAME "./testlib.so.0.0.0" #endif #define LIBRARY_FUNC "multiply_by_two" +#define LIBRARY_VAR "seven" +#define LIBRARY_STRUCT "mod_descr" + +typedef int (*multiply_by_two_func)( int ); + +typedef struct { + int major; + int minor; + multiply_by_two_func func; +} module_descriptor_t; int main( void ) { wolf_library_p library; @@ -36,6 +46,8 @@ int main( void ) { WOLF_LIBRARY_FUNCPTR symbol; multiply_by_two_func func; int res = 0; + int *var; + module_descriptor_t *mod_descr; /* open the libray */ library = wolf_library_load( LIBRARY_NAME, &error ); @@ -49,7 +61,7 @@ int main( void ) { } /* fetch a known function symbol (multiply_by_two) */ - symbol = wolf_library_get_func( library, LIBRARY_FUNC, &error ); + symbol = wolf_library_get( library, LIBRARY_FUNC, &error ); if( error != WOLF_OK ) { fprintf( stderr, "Error %d (%s) fetching function '%s' from the library\n", error, wolf_error_msg( error, errbuf, 512 ), LIBRARY_FUNC ); @@ -62,8 +74,35 @@ int main( void ) { /* call it */ res = func( 7 ); + printf( "func(7) = %d\n", res ); assert( res == 14 ); + /* fetch address as address to a variable (seven) */ + var = (int *)wolf_library_get( library, LIBRARY_VAR, &error ); + if( error != WOLF_OK ) { + fprintf( stderr, "Error %d (%s) fetching variable '%s' from the library\n", + error, wolf_error_msg( error, errbuf, 512 ), LIBRARY_VAR ); + fprintf( stderr, "Internal loader error: %s\n", + wolf_library_error_msg( library, errbuf, 512 ) ); + (void)wolf_library_unload( library ); + return EXIT_FAILURE; + } + printf( "var = %d\n", *var ); + assert( *var == 7 ); + + /* fetch something complex (in this case something like a module descriptor) */ + mod_descr = (module_descriptor_t *)wolf_library_get( library, LIBRARY_STRUCT, &error ); + if( error != WOLF_OK ) { + fprintf( stderr, "Error %d (%s) fetching structure '%s' from the library\n", + error, wolf_error_msg( error, errbuf, 512 ), LIBRARY_STRUCT ); + fprintf( stderr, "Internal loader error: %s\n", + wolf_library_error_msg( library, errbuf, 512 ) ); + (void)wolf_library_unload( library ); + return EXIT_FAILURE; + } + printf( "module version: %d.%d\n", mod_descr->major, mod_descr->minor ); + assert( mod_descr->major == 1 && mod_descr->minor == 0 ); + /* close library */ error = wolf_library_unload( library ); if( error != WOLF_OK ) { diff --git a/tests/library/testlib.c b/tests/library/testlib.c index b240819..cd42d1a 100644 --- a/tests/library/testlib.c +++ b/tests/library/testlib.c @@ -4,8 +4,22 @@ #define DLLEXPORT #endif +typedef int (*multiply_by_two_func)( int ); + DLLEXPORT int multiply_by_two( int a ); int multiply_by_two( int a ) { return a * 2; } + +extern int DLLEXPORT seven = 7; + +typedef struct { + int major; + int minor; + multiply_by_two_func func; +} module_descriptor_t; + +extern DLLEXPORT module_descriptor_t mod_descr = { + 1, 0, &multiply_by_two +}; -- cgit v1.2.3-54-g00ecf