WindowNativeLoader.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace NativeLibraryLoader.Windows
  6. {
  7. internal class WindowNativeLoader : INativeLoader
  8. {
  9. void INativeLoader.CoreFreeLibrary(IntPtr intPtr)
  10. {
  11. FreeLibrary(intPtr);
  12. }
  13. string INativeLoader.CoreGetLastError()
  14. {
  15. return Marshal.GetLastWin32Error()+"";
  16. }
  17. IntPtr INativeLoader.CoreGetProcAddress(IntPtr intPtr, string entryPoint)
  18. {
  19. return GetProcAddress(intPtr, entryPoint);
  20. }
  21. IntPtr INativeLoader.CoreLoadLibrary(string path)
  22. {
  23. return LoadLibrary(path);
  24. }
  25. [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)]
  26. static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName);
  27. [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
  28. static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string procName);
  29. [DllImport("kernel32.dll", SetLastError = true)]
  30. [return: MarshalAs(UnmanagedType.Bool)]
  31. static extern bool FreeLibrary(IntPtr hModule);
  32. }
  33. }