Skip to content

Commit dcd0d22

Browse files
committed
Add way to attempt a load from Path of the native library
1 parent 50f4631 commit dcd0d22

File tree

6 files changed

+114
-2
lines changed

6 files changed

+114
-2
lines changed

src/FRC-Utilities/NativeLibraryUtilities/EmbeddedLibraryLoader.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ void ILibraryLoader.LoadLibrary(string filename)
2828
}
2929
}
3030

31+
/// <summary>
32+
/// Try to load a native library from a path
33+
/// </summary>
34+
/// <param name="filename"></param>
35+
/// <returns></returns>
36+
public bool TryLoadLibrary(string filename)
37+
{
38+
IntPtr dl = dlopen(filename, 2);
39+
if (dl != IntPtr.Zero)
40+
{
41+
NativeLibraryHandle = dl;
42+
return true;
43+
};
44+
return false;
45+
}
46+
3147
/// <inheritdoc/>
3248
IntPtr IFunctionPointerLoader.GetProcAddress(string name)
3349
{

src/FRC-Utilities/NativeLibraryUtilities/ILibraryLoader.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public interface ILibraryLoader : IFunctionPointerLoader
1717
/// </summary>
1818
/// <param name="filename"></param>
1919
void LoadLibrary(string filename);
20+
21+
/// <summary>
22+
/// Tires to load library from specified file name
23+
/// </summary>
24+
/// <param name="filename"></param>
25+
/// <returns></returns>
26+
bool TryLoadLibrary(string filename);
27+
2028
/// <summary>
2129
/// Unloads the native library
2230
/// </summary>

src/FRC-Utilities/NativeLibraryUtilities/LinuxLibraryLoader.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ void ILibraryLoader.LoadLibrary(string filename)
3131
}
3232
}
3333

34+
/// <summary>
35+
/// Try to load a native library from a path
36+
/// </summary>
37+
/// <param name="filename"></param>
38+
/// <returns></returns>
39+
public bool TryLoadLibrary(string filename)
40+
{
41+
IntPtr dl = dlopen(filename, 2);
42+
if (dl != IntPtr.Zero)
43+
{
44+
NativeLibraryHandle = dl;
45+
return true;
46+
};
47+
return false;
48+
}
49+
3450
/// <inheritdoc/>
3551
IntPtr IFunctionPointerLoader.GetProcAddress(string name)
3652
{

src/FRC-Utilities/NativeLibraryUtilities/MacOsLibraryLoader.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ void ILibraryLoader.LoadLibrary(string filename)
3030
}
3131
}
3232

33+
/// <summary>
34+
/// Try to load a native library from a path
35+
/// </summary>
36+
/// <param name="filename"></param>
37+
/// <returns></returns>
38+
bool ILibraryLoader.TryLoadLibrary(string filename)
39+
{
40+
IntPtr dl = dlopen(filename, 2);
41+
if (dl != IntPtr.Zero)
42+
{
43+
NativeLibraryHandle = dl;
44+
return true;
45+
};
46+
return false;
47+
}
48+
3349
IntPtr IFunctionPointerLoader.GetProcAddress(string name)
3450
{
3551
dlerror();

src/FRC-Utilities/NativeLibraryUtilities/NativeLibraryLoader.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,39 @@ public void LoadNativeLibrary<T>(bool directLoad = false, string? extractLocatio
168168
LoadNativeLibrary<T>(LibraryLoader, m_nativeLibraryName[osType], directLoad, extractLocation);
169169
}
170170

171+
/// <summary>
172+
/// Try to load a native library directly from a path
173+
/// </summary>
174+
/// <param name="libraryName"></param>
175+
/// <returns></returns>
176+
public bool TryLoadNativeLibraryPath(string libraryName)
177+
{
178+
OsType osType = OsType;
179+
180+
if (osType == OsType.None)
181+
throw new InvalidOperationException(
182+
"OS type is unknown. Must use the overload to manually load the file");
183+
184+
ILibraryLoader loader = osType switch
185+
{
186+
OsType.Windows32 => new WindowsLibraryLoader(),
187+
OsType.Windows64 => new WindowsLibraryLoader(),
188+
OsType.Linux32 => new LinuxLibraryLoader(),
189+
OsType.Linux64 => new LinuxLibraryLoader(),
190+
OsType.MacOs32 => new MacOsLibraryLoader(),
191+
OsType.MacOs64 => new MacOsLibraryLoader(),
192+
_ => new EmbeddedLibraryLoader()
193+
};
194+
195+
bool wasLoaded = loader.TryLoadLibrary(libraryName);
196+
if (wasLoaded)
197+
{
198+
LibraryLoader = loader;
199+
LibraryLocation = libraryName;
200+
}
201+
return wasLoaded;
202+
}
203+
171204
/// <summary>
172205
/// Loads a native library with a reflected assembly holding the native libraries
173206
/// </summary>

src/FRC-Utilities/NativeLibraryUtilities/WindowsLibraryLoader.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ComponentModel;
23
using System.IO;
34
using System.Runtime.InteropServices;
45
using FRC.ILGeneration;
@@ -17,7 +18,29 @@ void ILibraryLoader.LoadLibrary(string filename)
1718
{
1819
if (!File.Exists(filename))
1920
throw new FileNotFoundException("The file requested to be loaded could not be found");
20-
NativeLibraryHandle = LoadLibrary(filename);
21+
IntPtr dl = LoadLibrary(filename);
22+
if (dl != IntPtr.Zero)
23+
{
24+
NativeLibraryHandle = dl;
25+
return;
26+
}
27+
throw new Win32Exception(Marshal.GetLastWin32Error());
28+
}
29+
30+
/// <summary>
31+
/// Try to load a native library from a path
32+
/// </summary>
33+
/// <param name="filename"></param>
34+
/// <returns></returns>
35+
public bool TryLoadLibrary(string filename)
36+
{
37+
IntPtr dl = LoadLibrary(filename);
38+
if (dl != IntPtr.Zero)
39+
{
40+
NativeLibraryHandle = dl;
41+
return true;
42+
};
43+
return false;
2144
}
2245

2346
IntPtr IFunctionPointerLoader.GetProcAddress(string name)
@@ -36,7 +59,7 @@ void ILibraryLoader.UnloadLibrary()
3659
FreeLibrary(NativeLibraryHandle);
3760
}
3861

39-
[DllImport("kernel32")]
62+
[DllImport("kernel32", SetLastError = true)]
4063
private static extern IntPtr LoadLibrary(string fileName);
4164

4265
[DllImport("kernel32.dll")]

0 commit comments

Comments
 (0)