slouken@5535: /* slouken@5535: Copyright (C) 1997-2011 Sam Lantinga slouken@5535: slouken@5535: This software is provided 'as-is', without any express or implied slouken@5535: warranty. In no event will the authors be held liable for any damages slouken@5535: arising from the use of this software. slouken@5535: slouken@5535: Permission is granted to anyone to use this software for any purpose, slouken@5535: including commercial applications, and to alter it and redistribute it slouken@5535: freely. slouken@5535: */ icculus@2067: slouken@3338: /* Test program to test dynamic loading with the loadso subsystem. icculus@2067: */ icculus@2067: icculus@2067: #include icculus@2067: #include slouken@3338: #include icculus@2067: icculus@2067: #include "SDL.h" icculus@2067: slouken@2120: typedef int (*fntype) (const char *); icculus@2067: slouken@2120: int slouken@2120: main(int argc, char *argv[]) icculus@2067: { slouken@2120: int retval = 0; slouken@2120: int hello = 0; slouken@2120: const char *libname = NULL; slouken@2120: const char *symname = NULL; slouken@2120: void *lib = NULL; slouken@2120: fntype fn = NULL; icculus@2067: slouken@2120: if (argc != 3) { slouken@2120: const char *app = argv[0]; slouken@2120: fprintf(stderr, "USAGE: %s \n", app); slouken@2120: fprintf(stderr, " %s --hello \n", app); slouken@2120: return 1; slouken@2120: } icculus@2067: slouken@2120: /* Initialize SDL */ slouken@2120: if (SDL_Init(0) < 0) { slouken@2120: fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); slouken@2120: return 2; slouken@2120: } icculus@2067: slouken@2120: if (strcmp(argv[1], "--hello") == 0) { slouken@2120: hello = 1; slouken@2120: libname = argv[2]; slouken@2120: symname = "puts"; slouken@2120: } else { slouken@2120: libname = argv[1]; slouken@2120: symname = argv[2]; slouken@2120: } icculus@2067: slouken@2120: lib = SDL_LoadObject(libname); slouken@2120: if (lib == NULL) { slouken@2120: fprintf(stderr, "SDL_LoadObject('%s') failed: %s\n", slouken@2120: libname, SDL_GetError()); slouken@2120: retval = 3; slouken@2120: } else { slouken@2120: fn = (fntype) SDL_LoadFunction(lib, symname); slouken@2120: if (fn == NULL) { slouken@2120: fprintf(stderr, "SDL_LoadFunction('%s') failed: %s\n", slouken@2120: symname, SDL_GetError()); slouken@2120: retval = 4; slouken@2120: } else { slouken@2120: printf("Found %s in %s at %p\n", symname, libname, fn); slouken@2120: if (hello) { slouken@2120: printf("Calling function...\n"); slouken@2120: fflush(stdout); slouken@2120: fn(" HELLO, WORLD!\n"); slouken@2120: printf("...apparently, we survived. :)\n"); slouken@2120: printf("Unloading library...\n"); slouken@2120: fflush(stdout); slouken@2120: } slouken@2120: } slouken@2120: SDL_UnloadObject(lib); slouken@2120: } slouken@2120: SDL_Quit(); slouken@2120: return (0); icculus@2067: }