|
| 1 | +// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET. |
| 2 | +// Licensed under the Apache License, Version 2.0. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Text; |
| 7 | +using ImageMagick; |
| 8 | +using ImageMagick.Configuration; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace Magick.NET.Tests.Configuration.ConfigurationFilesTests; |
| 12 | + |
| 13 | +public partial class ConfigurationFilesTests |
| 14 | +{ |
| 15 | + public class TheNativeResources |
| 16 | + { |
| 17 | + [Fact] |
| 18 | + public void ShouldBeEmbeddedInTheNativeLibrary() |
| 19 | + { |
| 20 | + if (!Runtime.IsWindows) |
| 21 | + Assert.Skip("The embedded resources are only available on Windows."); |
| 22 | + |
| 23 | +#if PLATFORM_x64 || PLATFORM_AnyCPU |
| 24 | + var module = GetModuleHandle(ImageMagick.NativeLibrary.X64Name); |
| 25 | +#elif PLATFORM_arm64 |
| 26 | + var module = GetModuleHandle(ImageMagick.NativeLibrary.Arm64Name); |
| 27 | +#else |
| 28 | + var module = GetModuleHandle(ImageMagick.NativeLibrary.X86Name); |
| 29 | +#endif |
| 30 | + Assert.NotEqual(IntPtr.Zero, module); |
| 31 | + |
| 32 | + foreach (var configurationFile in ConfigurationFiles.Default.All) |
| 33 | + { |
| 34 | + var resource = FindResource(module, configurationFile.FileName, "IMAGEMAGICK"); |
| 35 | + Assert.NotEqual(IntPtr.Zero, resource); |
| 36 | + |
| 37 | + var resourceData = LoadResource(module, resource); |
| 38 | + Assert.NotEqual(IntPtr.Zero, resourceData); |
| 39 | + |
| 40 | + var resourcePointer = LockResource(resourceData); |
| 41 | + Assert.NotEqual(IntPtr.Zero, resourcePointer); |
| 42 | + |
| 43 | + var size = SizeofResource(module, resource); |
| 44 | + Assert.NotEqual(0U, size); |
| 45 | + |
| 46 | + var bytes = new byte[size]; |
| 47 | + Marshal.Copy(resourcePointer, bytes, 0, (int)size); |
| 48 | + |
| 49 | + var data = Encoding.UTF8.GetString(bytes); |
| 50 | + Assert.Equal(configurationFile.Data, data); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 55 | + private static extern IntPtr FindResource(IntPtr hModule, string lpName, string lpType); |
| 56 | + |
| 57 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 58 | + private static extern IntPtr GetModuleHandle(string lpModuleName); |
| 59 | + |
| 60 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 61 | + private static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo); |
| 62 | + |
| 63 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 64 | + private static extern IntPtr LockResource(IntPtr hResData); |
| 65 | + |
| 66 | + [DllImport("kernel32.dll", SetLastError = true)] |
| 67 | + private static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo); |
| 68 | + } |
| 69 | +} |
0 commit comments