Advertisement
Guest User

shared lib loader wrapped

a guest
Jan 10th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.98 KB | None | 0 0
  1. /*
  2. This file is a part of my game engine,
  3. but you can use it in your projects
  4. without asking permission under MIT license terms.
  5.  
  6. Copyright (C) 2012 evilrat
  7. */
  8.  
  9. module util.sharedlib;
  10.  
  11. // =========================
  12. // LOADER
  13. // =========================
  14.  
  15. // this is a class to allow reference counting and easy sharing library handle,
  16. // if you don't like the idea of class overhead just remove unload method from
  17. // destructor and make it struct, but remember about RAII.
  18. //
  19. // note: loading D libraries should work properly only on windows
  20. //   at this moment(DMD 2.061)
  21. final class SharedLib
  22. {
  23. public:
  24.     ~this()
  25.     {
  26.         Unload();
  27.     }
  28.  
  29.     void Load(string libName)
  30.     {
  31.         version(Posix)
  32.             _handle = dlopen(libName.toStringz(), RTLD_NOW);
  33.         else
  34.             _handle = Runtime.loadLibrary(libName);
  35.     }
  36.  
  37.     void Unload()
  38.     {
  39.         if ( _handle !is null )
  40.         {
  41.             version(Posix)
  42.                 dlclose(_handle);
  43.             else
  44.                 Runtime.unloadLibrary(_handle);
  45.         }
  46.     }
  47.  
  48.     T GetSymbol(T = void*)(string symbolName)
  49.     {
  50.         version(Posix)
  51.             return cast(T)dlsym(_handle, symbolName.toStringz());
  52.         else
  53.             return cast(T)GetProcAddress(cast(HMODULE)_handle, symbolName.toStringz());
  54.  
  55.         // should never reach this
  56.         return null;
  57.     }
  58.    
  59.     @property public SharedLibHandle handle() { return _handle; }
  60.     private SharedLibHandle _handle;
  61. }
  62.  
  63. // =========================
  64. // PRIVATE IMPORTS AND DECLARATIONS
  65. // =========================
  66. private
  67. {
  68.     import core.runtime;
  69.  
  70.     // handle
  71.     alias void* SharedLibHandle;
  72.  
  73.     version(Posix)
  74.     {
  75.         version(Linux) {
  76.             private import std.c.linux.linux;
  77.         }
  78.         else
  79.         {
  80.             extern(C)
  81.             {
  82.                 // there are more constants but i use only this.
  83.                 enum RTLD_NOW = 2;
  84.  
  85.                 // prototypes for dynamic loader
  86.                 void* dlopen(const(char)* file, int mode);
  87.                 void* dlsym(void* handle, const(char*) name);
  88.                 const(char)* dlerror();
  89.                 int dlclose(void* handle);
  90.             }
  91.         }
  92.     }
  93.     else version(Windows) {
  94.         import std.c.windows.windows;
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement