loadlib.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "precomp_wasabi_bfc.h"
  2. #include "loadlib.h"
  3. #if !defined(WIN32) && !defined(LINUX)
  4. #error port me
  5. #endif
  6. Library::Library(const wchar_t *filename) : NamedW(filename)
  7. {
  8. lib = NULL;
  9. }
  10. Library::~Library()
  11. {
  12. unload();
  13. }
  14. int Library::load(const wchar_t *newname)
  15. {
  16. if (lib != NULL && newname == NULL)
  17. return 1;
  18. unload();
  19. if (newname != NULL)
  20. setName(newname);
  21. const wchar_t *n = getName();
  22. ASSERT(n != NULL);
  23. #ifdef WIN32
  24. __try {
  25. lib = LoadLibraryW(n);
  26. } __except(EXCEPTION_EXECUTE_HANDLER)
  27. {
  28. // stupid DLL
  29. lib = NULL;
  30. OutputDebugString(L"exception while loading dll");
  31. OutputDebugStringW(newname);
  32. OutputDebugString(L"\n");
  33. }
  34. #elif defined(LINUX)
  35. // Not using string to try to not use common/wasabi in Studio.exe
  36. char *conv = _strdup( getName() );
  37. int len = strlen( conv );
  38. if ( ! strcasecmp( conv + len - 4, ".dll" ) )
  39. {
  40. strcpy( conv + len - 4, ".so" );
  41. }
  42. lib = dlopen(conv, RTLD_LAZY);
  43. free( conv );
  44. #else
  45. #error port me!
  46. #endif
  47. if (lib == NULL) return 0;
  48. return 1;
  49. }
  50. void Library::unload()
  51. {
  52. if (lib != NULL)
  53. {
  54. #ifdef WIN32
  55. FreeLibrary(lib);
  56. #elif defined(LINUX)
  57. dlclose(lib);
  58. #else
  59. #error port me!
  60. #endif
  61. }
  62. lib = NULL;
  63. }
  64. void *Library::getProcAddress(const char *procname)
  65. {
  66. ASSERT(procname != NULL);
  67. #if defined(WIN32)
  68. return GetProcAddress(lib, procname);
  69. #elif defined(LINUX)
  70. return dlsym(lib, procname);
  71. #else
  72. #error port me!
  73. #endif
  74. }