From d765df3c9cefaca58ae1e875c81cd599535a6847 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Thu, 7 Mar 2024 14:03:50 +0100
Subject: [PATCH 01/18] use high-level library loading
---
src/NodeApi/Runtime/NodejsPlatform.cs | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index 2c2e9d31..2b4d3fdc 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -2,6 +2,7 @@
// Licensed under the MIT License.
using System;
+using System.Reflection;
using System.Runtime.InteropServices;
namespace Microsoft.JavaScript.NodeApi.Runtime;
@@ -25,25 +26,29 @@ public sealed class NodejsPlatform : IDisposable
///
/// Initializes the Node.js platform.
///
- /// Path to the `libnode` shared library, including extension.
+ /// Name of the `libnode` shared library.
/// Optional application arguments.
/// Optional platform options.
/// A Node.js platform instance has already been
/// loaded in the current process.
public NodejsPlatform(
- string libnodePath,
+ string libnode,
string[]? args = null,
string[]? execArgs = null)
{
- if (string.IsNullOrEmpty(libnodePath)) throw new ArgumentNullException(nameof(libnodePath));
-
if (Current != null)
{
throw new InvalidOperationException(
"Only one Node.js platform instance per process is allowed.");
}
- nint libnodeHandle = NativeLibrary.Load(libnodePath);
+ var entryAssembly = Assembly.GetEntryAssembly();
+
+ nint libnodeHandle =
+ entryAssembly == null
+ ? NativeLibrary.Load(libnode, entryAssembly, null)
+ : NativeLibrary.Load(libnode);
+
Runtime = new NodejsRuntime(libnodeHandle);
Runtime.CreatePlatform(args, execArgs, (error) => Console.WriteLine(error), out _platform)
From d877e5445338a1095906e3707e7d010afe8b853d Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Thu, 7 Mar 2024 20:49:40 +0100
Subject: [PATCH 02/18] maybe fix build
---
src/NodeApi/Runtime/NodejsPlatform.cs | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index 2b4d3fdc..a7def557 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -2,9 +2,12 @@
// Licensed under the MIT License.
using System;
-using System.Reflection;
using System.Runtime.InteropServices;
+#if NETSTANDARD3_0_OR_GREATER
+using System.Reflection;
+#endif
+
namespace Microsoft.JavaScript.NodeApi.Runtime;
using static JSRuntime;
@@ -26,7 +29,10 @@ public sealed class NodejsPlatform : IDisposable
///
/// Initializes the Node.js platform.
///
- /// Name of the `libnode` shared library.
+ ///
+ /// Name of the `libnode` shared library.
+ /// Has to be a full file path when using .NET Framework.
+ ///
/// Optional application arguments.
/// Optional platform options.
/// A Node.js platform instance has already been
@@ -42,12 +48,19 @@ public NodejsPlatform(
"Only one Node.js platform instance per process is allowed.");
}
+#if NETSTANDARD3_0_OR_GREATER
var entryAssembly = Assembly.GetEntryAssembly();
nint libnodeHandle =
entryAssembly == null
? NativeLibrary.Load(libnode, entryAssembly, null)
: NativeLibrary.Load(libnode);
+#else
+ if (string.IsNullOrEmpty(libnode))
+ throw new ArgumentNullException(nameof(libnode));
+
+ nint libnodeHandle = NativeLibrary.Load(libnode);
+#endif
Runtime = new NodejsRuntime(libnodeHandle);
From 1dd3250dea6f962b32d918e5d926d692a7a4024d Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Thu, 7 Mar 2024 21:03:47 +0100
Subject: [PATCH 03/18] undo brainfart moment
---
src/NodeApi/Runtime/NodejsPlatform.cs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index a7def557..154f1be3 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -4,7 +4,7 @@
using System;
using System.Runtime.InteropServices;
-#if NETSTANDARD3_0_OR_GREATER
+#if NETCOREAPP3_0_OR_GREATER
using System.Reflection;
#endif
@@ -48,7 +48,7 @@ public NodejsPlatform(
"Only one Node.js platform instance per process is allowed.");
}
-#if NETSTANDARD3_0_OR_GREATER
+#if NETCOREAPP3_0_OR_GREATER
var entryAssembly = Assembly.GetEntryAssembly();
nint libnodeHandle =
From e6fa1c6f34aa0c47a72e2aa7f0c637d1d7a608f5 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Thu, 7 Mar 2024 21:08:14 +0100
Subject: [PATCH 04/18] this will fix it
---
src/NodeApi/Runtime/NodejsPlatform.cs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index 154f1be3..e32bbe33 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -4,7 +4,7 @@
using System;
using System.Runtime.InteropServices;
-#if NETCOREAPP3_0_OR_GREATER
+#if NET7_0_OR_GREATER
using System.Reflection;
#endif
@@ -48,7 +48,7 @@ public NodejsPlatform(
"Only one Node.js platform instance per process is allowed.");
}
-#if NETCOREAPP3_0_OR_GREATER
+#if NET7_0_OR_GREATER
var entryAssembly = Assembly.GetEntryAssembly();
nint libnodeHandle =
From 61c787a463769395ad98bf818e9e185b4dd4da2c Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Thu, 7 Mar 2024 21:12:33 +0100
Subject: [PATCH 05/18] i need more sleep
---
src/NodeApi/Runtime/NodejsPlatform.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index e32bbe33..25bb7ce2 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -52,7 +52,7 @@ public NodejsPlatform(
var entryAssembly = Assembly.GetEntryAssembly();
nint libnodeHandle =
- entryAssembly == null
+ entryAssembly != null
? NativeLibrary.Load(libnode, entryAssembly, null)
: NativeLibrary.Load(libnode);
#else
From ca3313c5b1d79043b67f3037284d8d8a646ba719 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Thu, 7 Mar 2024 21:18:39 +0100
Subject: [PATCH 06/18] remove using directive and reference Assembly by full
name
---
src/NodeApi/Runtime/NodejsPlatform.cs | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index 25bb7ce2..6ab7454b 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -4,10 +4,6 @@
using System;
using System.Runtime.InteropServices;
-#if NET7_0_OR_GREATER
-using System.Reflection;
-#endif
-
namespace Microsoft.JavaScript.NodeApi.Runtime;
using static JSRuntime;
@@ -49,7 +45,7 @@ public NodejsPlatform(
}
#if NET7_0_OR_GREATER
- var entryAssembly = Assembly.GetEntryAssembly();
+ var entryAssembly = System.Reflection.Assembly.GetEntryAssembly();
nint libnodeHandle =
entryAssembly != null
From f751f2b76d55793edc7a64f9c7a4351a9573d274 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Sat, 9 Mar 2024 14:30:57 +0100
Subject: [PATCH 07/18] try to add search
---
src/NodeApi/Runtime/NativeLibrary.cs | 81 ++++++++++++++++++++++++++++
1 file changed, 81 insertions(+)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index a7e59b4a..7d841298 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -4,6 +4,8 @@
#if !NET7_0_OR_GREATER
using System;
+using System.IO;
+using System.Reflection;
using System.Runtime.InteropServices;
#if !NETFRAMEWORK
using SysNativeLibrary = System.Runtime.InteropServices.NativeLibrary;
@@ -50,6 +52,24 @@ public static nint Load(string libraryName)
#endif
}
+ ///
+ /// Loads a native library using the high-level API.
+ ///
+ /// The name of the native library to be loaded.
+ /// The assembly loading the native library.
+ /// The search path.
+ /// The OS handle for the loaded native library.
+ public static nint Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
+ {
+#if NETFRAMEWORK
+ var libraryPath = FindLibrary(libraryName, assembly);
+
+ return LoadLibrary(libraryPath);
+#else
+ return SysNativeLibrary.Load(libraryName);
+#endif
+ }
+
///
/// Gets the address of an exported symbol.
///
@@ -75,6 +95,67 @@ public static bool TryGetExport(nint handle, string name, out nint procAddress)
#endif
}
+ ///
+ /// Searches various well-known paths for a library and returns the first result.
+ ///
+ /// Name of the library to search for.
+ /// Assembly to search relative from.
+ /// Library path if found, otherwise false.
+ private static string? FindLibrary(string libraryName, Assembly assembly)
+ {
+ if (File.Exists(libraryName))
+ {
+ return Path.GetFullPath(libraryName);
+ }
+
+ string[] tryLibraryNames;
+
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
+ tryLibraryNames =
+ [
+ libraryName,
+ $"{libraryName}.dll"
+ ];
+ }
+ else
+ {
+ string libraryExtension = RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
+ ? "dylib"
+ : "so";
+
+ tryLibraryNames =
+ [
+ libraryName,
+ $"lib{libraryName}",
+ $"{libraryName}.{libraryExtension}",
+ $"lib{libraryName}.{libraryExtension}"
+ ];
+ }
+
+ string[] tryDirectories =
+ [
+ Path.GetDirectoryName(assembly.Location),
+ Environment.CurrentDirectory,
+ Environment.SystemDirectory,
+ ];
+
+ foreach (string tryDirectory in tryDirectories)
+ {
+ foreach (string tryLibraryName in tryLibraryNames)
+ {
+ string tryLibraryPath = Path.Join(tryDirectory, tryLibraryName);
+
+ if (File.Exists(tryLibraryPath))
+ {
+ return tryLibraryPath;
+ }
+ }
+ }
+
+ return null;
+ }
+
#pragma warning disable CA2101 // Specify marshaling for P/Invoke string arguments
[DllImport("kernel32")]
From c9b67b630a7b831ee07db6621e519a295dea3572 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Sat, 9 Mar 2024 14:31:49 +0100
Subject: [PATCH 08/18] update platform file
---
src/NodeApi/Runtime/NodejsPlatform.cs | 10 ++--------
1 file changed, 2 insertions(+), 8 deletions(-)
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index 84fc0874..c1439df6 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -2,6 +2,7 @@
// Licensed under the MIT License.
using System;
+using System.Reflection;
using System.Runtime.InteropServices;
namespace Microsoft.JavaScript.NodeApi.Runtime;
@@ -42,19 +43,12 @@ public NodejsPlatform(
"Only one Node.js platform instance per process is allowed.");
}
-#if NET7_0_OR_GREATER
- var entryAssembly = System.Reflection.Assembly.GetEntryAssembly();
+ var entryAssembly = Assembly.GetAssembly(typeof(NodejsPlatform));
nint libnodeHandle =
entryAssembly != null
? NativeLibrary.Load(libnode, entryAssembly, null)
: NativeLibrary.Load(libnode);
-#else
- if (string.IsNullOrEmpty(libnode))
- throw new ArgumentNullException(nameof(libnode));
-
- nint libnodeHandle = NativeLibrary.Load(libnode);
-#endif
Runtime = new NodejsRuntime(libnodeHandle);
From 2497f24ae36ca810f97bf642184957ed3337506e Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Sat, 9 Mar 2024 14:49:05 +0100
Subject: [PATCH 09/18] use path combine
---
src/NodeApi/Runtime/NativeLibrary.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index 7d841298..944793ce 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -144,7 +144,7 @@ public static bool TryGetExport(nint handle, string name, out nint procAddress)
{
foreach (string tryLibraryName in tryLibraryNames)
{
- string tryLibraryPath = Path.Join(tryDirectory, tryLibraryName);
+ string tryLibraryPath = Path.Combine(tryDirectory, tryLibraryName);
if (File.Exists(tryLibraryPath))
{
From f24f6d2a5e416e4a2f35f214cefc2490ecae7859 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Sat, 9 Mar 2024 14:59:15 +0100
Subject: [PATCH 10/18] fix build
---
src/NodeApi/Runtime/NativeLibrary.cs | 2 +-
src/NodeApi/Runtime/NodejsPlatform.cs | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index 944793ce..a155bfbb 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -62,7 +62,7 @@ public static nint Load(string libraryName)
public static nint Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
#if NETFRAMEWORK
- var libraryPath = FindLibrary(libraryName, assembly);
+ string? libraryPath = FindLibrary(libraryName, assembly);
return LoadLibrary(libraryPath);
#else
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index c1439df6..0b016a7e 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -47,7 +47,7 @@ public NodejsPlatform(
nint libnodeHandle =
entryAssembly != null
- ? NativeLibrary.Load(libnode, entryAssembly, null)
+ ? NativeLibrary.Load(libnode, entryAssembly!, null)
: NativeLibrary.Load(libnode);
Runtime = new NodejsRuntime(libnodeHandle);
From 009a09f0990ae93f4dfce32843c9802e36096724 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Sat, 9 Mar 2024 15:04:48 +0100
Subject: [PATCH 11/18] fix nullability
---
src/NodeApi/Runtime/NativeLibrary.cs | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index a155bfbb..42ac7bdc 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -133,15 +133,20 @@ public static bool TryGetExport(nint handle, string name, out nint procAddress)
];
}
- string[] tryDirectories =
+ string?[] tryDirectories =
[
Path.GetDirectoryName(assembly.Location),
Environment.CurrentDirectory,
Environment.SystemDirectory,
];
- foreach (string tryDirectory in tryDirectories)
+ foreach (string? tryDirectory in tryDirectories)
{
+ if (tryDirectory == null)
+ {
+ continue;
+ }
+
foreach (string tryLibraryName in tryLibraryNames)
{
string tryLibraryPath = Path.Combine(tryDirectory, tryLibraryName);
From e6e1b386db82a0fcda82802b175fde789b9eff45 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Sat, 9 Mar 2024 15:06:48 +0100
Subject: [PATCH 12/18] add null check
---
src/NodeApi/Runtime/NativeLibrary.cs | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index 42ac7bdc..3f51ed51 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -64,6 +64,10 @@ public static nint Load(string libraryName, Assembly assembly, DllImportSearchPa
#if NETFRAMEWORK
string? libraryPath = FindLibrary(libraryName, assembly);
+ if (libraryPath == null) {
+ throw new ArgumentNullException(nameof(libraryName));
+ }
+
return LoadLibrary(libraryPath);
#else
return SysNativeLibrary.Load(libraryName);
From 16e53be20fcd2b73764f2aac0ba4540c3f069bcb Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Sat, 9 Mar 2024 15:07:50 +0100
Subject: [PATCH 13/18] fix loading call
---
src/NodeApi/Runtime/NativeLibrary.cs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index 3f51ed51..fb38c8da 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -70,7 +70,7 @@ public static nint Load(string libraryName, Assembly assembly, DllImportSearchPa
return LoadLibrary(libraryPath);
#else
- return SysNativeLibrary.Load(libraryName);
+ return SysNativeLibrary.Load(libraryName, assembly, searchPath);
#endif
}
From f3b8f1e8feac05f8ecf39f55d336260ae07fd876 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Sat, 9 Mar 2024 15:15:47 +0100
Subject: [PATCH 14/18] simplify null check
---
src/NodeApi/Runtime/NativeLibrary.cs | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index fb38c8da..84bf17c1 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -62,11 +62,8 @@ public static nint Load(string libraryName)
public static nint Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
#if NETFRAMEWORK
- string? libraryPath = FindLibrary(libraryName, assembly);
-
- if (libraryPath == null) {
- throw new ArgumentNullException(nameof(libraryName));
- }
+ string libraryPath = FindLibrary(libraryName, assembly)
+ ?? throw new ArgumentNullException(nameof(libraryName));
return LoadLibrary(libraryPath);
#else
From 11cfb01c0a64b2e6208e66cc598af1a53172b3dc Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Wed, 13 Mar 2024 14:58:04 +0100
Subject: [PATCH 15/18] fix safety concerns
---
src/NodeApi/Runtime/NativeLibrary.cs | 3 +--
src/NodeApi/Runtime/NodejsPlatform.cs | 2 +-
2 files changed, 2 insertions(+), 3 deletions(-)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index 84bf17c1..c0dd3157 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -104,7 +104,7 @@ public static bool TryGetExport(nint handle, string name, out nint procAddress)
/// Library path if found, otherwise false.
private static string? FindLibrary(string libraryName, Assembly assembly)
{
- if (File.Exists(libraryName))
+ if (Path.IsPathRooted(libraryName) && File.Exists(libraryName))
{
return Path.GetFullPath(libraryName);
}
@@ -137,7 +137,6 @@ public static bool TryGetExport(nint handle, string name, out nint procAddress)
string?[] tryDirectories =
[
Path.GetDirectoryName(assembly.Location),
- Environment.CurrentDirectory,
Environment.SystemDirectory,
];
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index 0b016a7e..fcb531bb 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -47,7 +47,7 @@ public NodejsPlatform(
nint libnodeHandle =
entryAssembly != null
- ? NativeLibrary.Load(libnode, entryAssembly!, null)
+ ? NativeLibrary.Load(libnode, entryAssembly!, DllImportSearchPath.SafeDirectories)
: NativeLibrary.Load(libnode);
Runtime = new NodejsRuntime(libnodeHandle);
From 678e3ba3e0f3424c3860680ff0fa76b5e3d3936c Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Wed, 13 Mar 2024 15:23:19 +0100
Subject: [PATCH 16/18] different search path
---
src/NodeApi/Runtime/NodejsPlatform.cs | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/NodeApi/Runtime/NodejsPlatform.cs b/src/NodeApi/Runtime/NodejsPlatform.cs
index fcb531bb..2bea1a26 100644
--- a/src/NodeApi/Runtime/NodejsPlatform.cs
+++ b/src/NodeApi/Runtime/NodejsPlatform.cs
@@ -47,7 +47,11 @@ public NodejsPlatform(
nint libnodeHandle =
entryAssembly != null
- ? NativeLibrary.Load(libnode, entryAssembly!, DllImportSearchPath.SafeDirectories)
+ ? NativeLibrary.Load(
+ libnode,
+ entryAssembly!,
+ DllImportSearchPath.AssemblyDirectory | DllImportSearchPath.SafeDirectories
+ )
: NativeLibrary.Load(libnode);
Runtime = new NodejsRuntime(libnodeHandle);
From 5b0e460aa2eaf06449901d31dc0e4426979724bb Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Wed, 13 Mar 2024 15:26:46 +0100
Subject: [PATCH 17/18] make search path more specific
---
src/NodeApi/Runtime/NativeLibrary.cs | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index c0dd3157..1efd35d1 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -62,7 +62,7 @@ public static nint Load(string libraryName)
public static nint Load(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
#if NETFRAMEWORK
- string libraryPath = FindLibrary(libraryName, assembly)
+ string libraryPath = FindLibrary(libraryName, assembly, searchPath)
?? throw new ArgumentNullException(nameof(libraryName));
return LoadLibrary(libraryPath);
@@ -101,8 +101,9 @@ public static bool TryGetExport(nint handle, string name, out nint procAddress)
///
/// Name of the library to search for.
/// Assembly to search relative from.
+ /// The search path.
/// Library path if found, otherwise false.
- private static string? FindLibrary(string libraryName, Assembly assembly)
+ private static string? FindLibrary(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (Path.IsPathRooted(libraryName) && File.Exists(libraryName))
{
@@ -136,8 +137,13 @@ public static bool TryGetExport(nint handle, string name, out nint procAddress)
string?[] tryDirectories =
[
- Path.GetDirectoryName(assembly.Location),
- Environment.SystemDirectory,
+ searchPath == null || (searchPath & DllImportSearchPath.AssemblyDirectory) > 0
+ ? Path.GetDirectoryName(assembly.Location)
+ : null,
+
+ searchPath == null || (searchPath & DllImportSearchPath.SafeDirectories) > 0
+ ? Environment.SystemDirectory
+ : null,
];
foreach (string? tryDirectory in tryDirectories)
From bc1091cae0196a5d2f05b73e694da16657af54e0 Mon Sep 17 00:00:00 2001
From: Neo Samardzic <110126965+samardzicneo@users.noreply.github.com>
Date: Wed, 27 Mar 2024 14:01:41 +0100
Subject: [PATCH 18/18] threads
---
src/NodeApi/Runtime/NativeLibrary.cs | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/src/NodeApi/Runtime/NativeLibrary.cs b/src/NodeApi/Runtime/NativeLibrary.cs
index 1efd35d1..8820a7a5 100644
--- a/src/NodeApi/Runtime/NativeLibrary.cs
+++ b/src/NodeApi/Runtime/NativeLibrary.cs
@@ -63,7 +63,7 @@ public static nint Load(string libraryName, Assembly assembly, DllImportSearchPa
{
#if NETFRAMEWORK
string libraryPath = FindLibrary(libraryName, assembly, searchPath)
- ?? throw new ArgumentNullException(nameof(libraryName));
+ ?? throw new DllNotFoundException($"Could not find library: {libraryName}");
return LoadLibrary(libraryPath);
#else
@@ -96,6 +96,7 @@ public static bool TryGetExport(nint handle, string name, out nint procAddress)
#endif
}
+#if NETFRAMEWORK
///
/// Searches various well-known paths for a library and returns the first result.
///
@@ -166,6 +167,7 @@ public static bool TryGetExport(nint handle, string name, out nint procAddress)
return null;
}
+#endif
#pragma warning disable CA2101 // Specify marshaling for P/Invoke string arguments