testloadso.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Test program to test dynamic loading with the loadso subsystem.
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "SDL.h"
  16. typedef int (*fntype) (const char *);
  17. int
  18. main(int argc, char *argv[])
  19. {
  20. int retval = 0;
  21. int hello = 0;
  22. const char *libname = NULL;
  23. const char *symname = NULL;
  24. void *lib = NULL;
  25. fntype fn = NULL;
  26. if (argc != 3) {
  27. const char *app = argv[0];
  28. SDL_Log("USAGE: %s <library> <functionname>\n", app);
  29. SDL_Log(" %s --hello <lib with puts()>\n", app);
  30. return 1;
  31. }
  32. /* Initialize SDL */
  33. if (SDL_Init(0) < 0) {
  34. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  35. return 2;
  36. }
  37. if (strcmp(argv[1], "--hello") == 0) {
  38. hello = 1;
  39. libname = argv[2];
  40. symname = "puts";
  41. } else {
  42. libname = argv[1];
  43. symname = argv[2];
  44. }
  45. lib = SDL_LoadObject(libname);
  46. if (lib == NULL) {
  47. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadObject('%s') failed: %s\n",
  48. libname, SDL_GetError());
  49. retval = 3;
  50. } else {
  51. fn = (fntype) SDL_LoadFunction(lib, symname);
  52. if (fn == NULL) {
  53. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_LoadFunction('%s') failed: %s\n",
  54. symname, SDL_GetError());
  55. retval = 4;
  56. } else {
  57. SDL_Log("Found %s in %s at %p\n", symname, libname, fn);
  58. if (hello) {
  59. SDL_Log("Calling function...\n");
  60. fflush(stdout);
  61. fn(" HELLO, WORLD!\n");
  62. SDL_Log("...apparently, we survived. :)\n");
  63. SDL_Log("Unloading library...\n");
  64. fflush(stdout);
  65. }
  66. }
  67. SDL_UnloadObject(lib);
  68. }
  69. SDL_Quit();
  70. return retval;
  71. }