using System.Runtime.InteropServices; namespace Robovoice.Tts.LibPiper; internal static class NativeDependencyLoader { private static int _loaded; public static void EnsureLoaded() { if (Interlocked.CompareExchange(ref _loaded, 1, 0) != 0) return; string baseDir = AppContext.BaseDirectory; string nativeDir = Path.Combine(baseDir, "runtimes", "win-x64", "native"); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Directory.Exists(nativeDir)) { if (!SetDllDirectory(nativeDir)) throw new InvalidOperationException( $"SetDllDirectory failed for: {nativeDir}"); } } [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetDllDirectory(string lpPathName); }