-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic libvips path ? #15
Comments
You could use an own module initializer in your program (to ensure that your own program is initialized first). For example, this program will P/Invoke using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using NetVips;
namespace ConsoleApp
{
/// <summary>
/// All code inside the <see cref="Initialize"/> method is ran as soon as the assembly is loaded.
/// </summary>
internal class Program
{
/// <summary>
/// Initializes the module.
/// </summary>
[ModuleInitializer]
public static void Initialize()
{
Console.WriteLine("Loading libvips " + (Environment.Is64BitOperatingSystem ? "x64" : "x86") + "-binary.");
SetDllDirectory(Path.GetFullPath(Environment.Is64BitOperatingSystem
? @"C:\vips-dev-8.12-w64\bin"
: @"C:\vips-dev-8.12-w32\bin"));
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetDllDirectory(string lpPathName);
static void Main(string[] args)
{
using var image = Image.Text("Hello <i>World!</i>", dpi: 300);
image.WriteToFile("hello-world.png");
}
}
} (See here for more information about If you could not use an own module initializer in your program, then you'll need to build NetVips with the module initializer disabled and call |
Thanks for your quick answer ! |
This is much easier now with the using System.Runtime.InteropServices;
using System.Runtime.Loader;
using NetVips;
AssemblyLoadContext.Default.ResolvingUnmanagedDll += (_, libName) =>
NativeLibrary.Load($"C:/vips-prefix/bin/{libName}");
// Try to init again in case the ModuleInitializer fails.
if (!ModuleInitializer.VipsInitialized)
{
ModuleInitializer.Initialize();
}
using var image = Image.Text("Hello <i>World!</i>", dpi: 300);
image.WriteToFile("hello-world.png"); |
Hello,
We have several types of C# processes that might use NetVips (and therefore, libvips). Some of them are 64bits and some of them are 32bits, some are web services, some are windows services.
To deal with that, I try to add the appropriate libvips path to the PATH environement variable at run time and at the process level (EnvironmentVariableTarget), depending of the current process architecure.
The problem is that ModuleInitializer's "Initialize" method is called way before any other code... So it's hard to patch the path variable without modifying NetVips code itself.
Do you a know a proper way to deal with this kind of dynamic libvips path ?
Thanks for your attention,
Guillaume
The text was updated successfully, but these errors were encountered: