UnixNativeLoader.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace NativeLibraryLoader.Linux
  6. {
  7. internal class UnixNativeLoader : INativeLoader
  8. {
  9. void INativeLoader.CoreFreeLibrary(IntPtr intPtr)
  10. {
  11. dlclose(intPtr);
  12. }
  13. IntPtr INativeLoader.CoreGetProcAddress(IntPtr intPtr, string entryPoint)
  14. {
  15. return dlsym(intPtr, entryPoint);
  16. }
  17. IntPtr INativeLoader.CoreLoadLibrary(string path)
  18. {
  19. return dlopen(path, RTLD_NOW);
  20. }
  21. private const string LibName = "libdl";
  22. const int RTLD_NOW = 0x002;
  23. [DllImport(LibName)]
  24. static extern IntPtr dlopen(string fileName, int flags);
  25. [DllImport(LibName)]
  26. static extern IntPtr dlsym(IntPtr handle, string name);
  27. [DllImport(LibName)]
  28. static extern int dlclose(IntPtr handle);
  29. [DllImport(LibName)]
  30. static extern string dlerror();
  31. public string CoreGetLastError()
  32. {
  33. return dlerror();
  34. }
  35. }
  36. }