prog.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #ifdef _WIN32
  4. #include <windows.h>
  5. #else
  6. #include <dlfcn.h>
  7. #endif
  8. #if defined _WIN32 || defined __CYGWIN__
  9. #define DLL_PUBLIC __declspec(dllexport)
  10. #else
  11. #if defined __GNUC__
  12. #define DLL_PUBLIC __attribute__ ((visibility("default")))
  13. #else
  14. #pragma message ("Compiler does not support symbol visibility.")
  15. #define DLL_PUBLIC
  16. #endif
  17. #endif
  18. typedef int (*fptr) (void);
  19. int DLL_PUBLIC
  20. func_from_executable(void)
  21. {
  22. return 42;
  23. }
  24. int
  25. main (int argc, char **argv)
  26. {
  27. int expected, actual;
  28. fptr importedfunc;
  29. #ifdef _WIN32
  30. HMODULE h = LoadLibraryA(argv[1]);
  31. #else
  32. void *h = dlopen(argv[1], RTLD_NOW);
  33. #endif
  34. assert(h != NULL);
  35. #ifdef _WIN32
  36. importedfunc = (fptr) GetProcAddress (h, "func");
  37. #else
  38. importedfunc = (fptr) dlsym(h, "func");
  39. #endif
  40. assert(importedfunc != NULL);
  41. assert(importedfunc != func_from_executable);
  42. actual = (*importedfunc)();
  43. expected = func_from_executable();
  44. assert(actual == expected);
  45. #ifdef _WIN32
  46. FreeLibrary(h);
  47. #else
  48. dlclose(h);
  49. #endif
  50. return 0;
  51. }