// Decompiled with JetBrains decompiler // Type: iMobileDevice.NativeLibraries // Assembly: iMobileDevice-net, Version=1.2.0.0, Culture=neutral, PublicKeyToken=040ae19651fac98a using System; using System.ComponentModel; using System.IO; using System.Runtime.InteropServices; namespace Software4u.MobileDevice { public static class NativeLibraries { //private const string WindowsRuntime64Bit = "win-x64"; //private const string WindowsRuntime32Bit = "win-x86"; public static bool LibraryFound { get; private set; } //public static void Load() //{ // if (LibraryFound) // return; // Load(Path.GetDirectoryName(typeof(NativeLibraries).GetTypeInfo().Assembly.Location)); //} public static void Load(string directory) { if (directory == null) throw new ArgumentNullException(nameof(directory)); if (!Directory.Exists(directory)) throw new ArgumentOutOfRangeException(nameof(directory), "The directory '" + directory + "' does not exist."); bool flag1 = false; bool flag2 = false; switch (Environment.OSVersion.Platform) { case PlatformID.Win32NT: flag1 = true; break; case PlatformID.Unix: flag2 = true; break; } if (flag1) { string[] strArray = { "imobiledevice", "ideviceactivation" }; string str1 = directory; if (Environment.Is64BitProcess && Directory.Exists(Path.Combine(directory, "win-x64"))) str1 = Path.Combine(directory, "win-x64"); else if (Directory.Exists(Path.Combine(directory, "win-x86"))) str1 = Path.Combine(directory, "win-x86"); if (!Directory.Exists(str1)) throw new ArgumentOutOfRangeException(nameof(directory), "The directory '" + str1 + "' does not exist."); foreach (string str2 in strArray) { string str3 = Path.Combine(str1, $"{str2}.dll"); if (!File.Exists(str3)) throw new FileNotFoundException("Could not load the library '" + str2 + "' at '" + str3 + "', because the file does not exist", str3); } #if !WINDOWS_UWP NativeMethods.SetDllDirectory(str1); #endif foreach (string str2 in strArray) { string dllToLoad = Path.Combine(str1, $"{str2}.dll"); bool libraryFound; #if WINDOWS_UWP libraryFound = NativeMethods.LoadPackagedLibrary(dllToLoad) == IntPtr.Zero; #else libraryFound = NativeMethods.LoadLibrary(dllToLoad) != IntPtr.Zero; #endif if (!libraryFound) { int lastWin32Error = Marshal.GetLastWin32Error(); throw new Win32Exception(lastWin32Error, $"Could not load the library '{str2}' at '{dllToLoad}'. The error code was {lastWin32Error}"); } } LibraryFound = true; } else { if (!(flag2 | false)) throw new NotSupportedException("imobiledevice-net is supported on Windows (.NET FX, .NET Core), Linux (.NET Core) and OS X (.NET Core)"); string str1 = "so"; string[] strArray = { "libimobiledevice" }; NativeMethods.dlerror(); foreach (string str2 in strArray) { if (NativeMethods.dlopen(str2 + "." + str1, DlOpenFlags.RTLD_NOW) == IntPtr.Zero) { Marshal.PtrToStringAnsi(NativeMethods.dlerror()); return; } } LibraryFound = true; } } } internal static class NativeMethods { private const string Kernel32 = "kernel32"; [DllImport("api-ms-win-core-libraryloader-l2-1-0.dll", SetLastError = true)] public static extern IntPtr LoadPackagedLibrary([MarshalAs(UnmanagedType.LPWStr)]string dllToLoad, int reserved = 0); [DllImport(Kernel32, SetLastError = true)] public static extern IntPtr LoadLibrary(string dllToLoad); [DllImport(Kernel32, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetDllDirectory(string lpPathName); [DllImport("dl")] public static extern IntPtr dlopen(string filename, DlOpenFlags flags); [DllImport("dl")] public static extern IntPtr dlerror(); } internal enum DlOpenFlags { RTLD_LOCAL = 0, RTLD_LAZY = 1, RTLD_NOW = 2, RTLD_NOLOAD = 4, RTLD_DEEPBIND = 8, RTLD_GLOBAL = 256, // 0x00000100 RTLD_NODELETE = 4096, // 0x00001000 } }