Files
Robovoice/Robovoice.Tts.LibPiper/NativeDependencyLoader.cs
T

29 lines
898 B
C#
Raw Normal View History

2026-08-10 11:27:28 +00:00
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);
}