NativeMethods.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. namespace SevenZip
  2. {
  3. using System;
  4. using System.Runtime.InteropServices;
  5. #if UNMANAGED
  6. internal static class NativeMethods
  7. {
  8. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  9. public delegate int CreateObjectDelegate(
  10. [In] ref Guid classID,
  11. [In] ref Guid interfaceID,
  12. [MarshalAs(UnmanagedType.Interface)] out object outObject);
  13. [DllImport("kernel32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true)]
  14. public static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string fileName);
  15. [DllImport("kernel32.dll")]
  16. [return: MarshalAs(UnmanagedType.Bool)]
  17. public static extern bool FreeLibrary(IntPtr hModule);
  18. [DllImport("kernel32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true)]
  19. public static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string procName);
  20. public static T SafeCast<T>(PropVariant var, T def)
  21. {
  22. object obj;
  23. try
  24. {
  25. obj = var.Object;
  26. }
  27. catch (Exception)
  28. {
  29. return def;
  30. }
  31. if (obj is T expected)
  32. {
  33. return expected;
  34. }
  35. return def;
  36. }
  37. }
  38. #endif
  39. }