Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions Src/RE2.Managed/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ static NativeMethods()
string platform =
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "win" : "linux";

if (platform == "linux")
{
return;
}
bool isWindows = platform == "win";

// Strictly speaking we don't need any platform-specific code here. I leave
// it just in case we find out that we *do* need to perform the Linux
Expand All @@ -34,26 +31,26 @@ static NativeMethods()

if (File.Exists(filePath))
{
LoadLibrary(filePath);
if (isWindows) { LoadLibrary(filePath); } else { LinuxMethods.dlopen(filePath, LinuxMethods.RTLD_NOW); }
}
}
else if (platform == "win")
else
{
string driverDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

// Load if next to this binary
string dllAdjacent = Path.Combine(driverDirectory, dllName);
if (File.Exists(dllAdjacent))
{
LoadLibrary(dllAdjacent);
if (isWindows) { LoadLibrary(dllAdjacent); } else { LinuxMethods.dlopen(dllAdjacent, LinuxMethods.RTLD_NOW); }
}

// Load if in runtimes subdirectory
string runtimeFolder = Environment.Is64BitProcess ? @$"runtimes\{platform}-x64\native" : @$"runtimes\{platform}-x86\native";
string dllInRuntime = Path.Combine(driverDirectory, runtimeFolder, dllName);
if (File.Exists(dllInRuntime))
{
LoadLibrary(dllInRuntime);
if (isWindows) { LoadLibrary(dllInRuntime); } else { LinuxMethods.dlopen(dllInRuntime, LinuxMethods.RTLD_NOW); }
}
}
}
Expand Down Expand Up @@ -148,6 +145,12 @@ public static unsafe void MatchesCaptureGroupsDispose(MatchesCaptureGroupsOutput

private static class LinuxMethods
{
public const int RTLD_NOW = 0x002;

[SuppressUnmanagedCodeSecurity]
[DllImport("libdl", ExactSpelling = true)]
public static unsafe extern IntPtr dlopen(string fileName, int flags);

[SuppressUnmanagedCodeSecurity]
[DllImport("libre2.so", PreserveSig = true, CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern int Test();
Expand Down