qmlnet/src/net/Qml.Net/Internal/LinuxDllImportLibraryPathResolver.cs
2018-12-23 14:23:13 -05:00

53 lines
No EOL
1.5 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using NetNativeLibLoader.PathResolver;
namespace Qml.Net.Internal
{
public class LinuxDllImportLibraryPathResolver : IPathResolver
{
IPathResolver _original;
public LinuxDllImportLibraryPathResolver(IPathResolver original)
{
_original = original;
}
public ResolvePathResult Resolve(string library)
{
var result = _original.Resolve(library);
if(!result.IsSuccess && library == "QmlNet")
{
// Try to let .NET load the library.
try
{
qml_net_getVersion();
using (var currentProcess = Process.GetCurrentProcess())
{
foreach (ProcessModule module in currentProcess.Modules)
{
if (Path.GetFileNameWithoutExtension(module.FileName) == "libQmlNet")
{
return ResolvePathResult.FromSuccess(module.FileName);
}
}
}
}
// ReSharper disable EmptyGeneralCatchClause
catch (Exception)
// ReSharper restore EmptyGeneralCatchClause
{
}
}
return result;
}
[DllImport("libQmlNet.so")]
static extern long qml_net_getVersion();
}
}