From d43d23aa0a2a212a35dec748103188c7a00a8408 Mon Sep 17 00:00:00 2001 From: Haves Irfan <20160532+ha-ves@users.noreply.github.com> Date: Thu, 1 Aug 2024 00:39:49 +0900 Subject: [PATCH 1/2] rcl init overload --- rcldotnet/RCLdotnet.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/rcldotnet/RCLdotnet.cs b/rcldotnet/RCLdotnet.cs index eeefea8a..f9b37571 100644 --- a/rcldotnet/RCLdotnet.cs +++ b/rcldotnet/RCLdotnet.cs @@ -1470,14 +1470,29 @@ public static void SpinOnce(Node node, long timeout) } } + /// + /// Initialize ROS2 with app's launch arguments. + /// public static void Init() + { + string[] args = System.Environment.GetCommandLineArgs(); + Init(args); + } + + /// + /// Initialize ROS2 with supplied arguments. + /// + /// + /// This is overloaded so that it can support arguments set up during run-time. + /// + /// ROS2 arguments to pass along. + public static void Init(string[] args) { lock (syncLock) { if (!initialized) { - string[] args = System.Environment.GetCommandLineArgs(); - RCLRet ret = RCLdotnetDelegates.native_rcl_init(args.Length, args); + RCLRet ret = RCLdotnetDelegates.native_rcl_init(args.Length, args.Length <= 0 ? null : args); RCLExceptionHelper.CheckReturnValue(ret, $"{nameof(RCLdotnetDelegates.native_rcl_init)}() failed."); initialized = true; } From fa354bd95dd0e7fb14c07f451597b8031e7de4e4 Mon Sep 17 00:00:00 2001 From: Haves Irfan <20160532+ha-ves@users.noreply.github.com> Date: Fri, 2 Aug 2024 04:29:11 +0900 Subject: [PATCH 2/2] loadlib custom path under executable directory --- rcldotnet_common/DllLoadUtils.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/rcldotnet_common/DllLoadUtils.cs b/rcldotnet_common/DllLoadUtils.cs index 43f0ecef..4c2d000f 100644 --- a/rcldotnet_common/DllLoadUtils.cs +++ b/rcldotnet_common/DllLoadUtils.cs @@ -16,6 +16,7 @@ // Based on http://dimitry-i.blogspot.com.es/2013/01/mononet-how-to-dynamically-load-native.html using System; +using System.IO; using System.Runtime.InteropServices; namespace ROS2 @@ -195,6 +196,8 @@ public void RegisterNativeFunction(IntPtr nativeLibrary, string na IntPtr nativeFunctionPointer = GetProcAddress(nativeLibrary, nativeFunctionName); functionDelegate = (FunctionType)Marshal.GetDelegateForFunctionPointer(nativeFunctionPointer, typeof(FunctionType)); } + + public const string AssemblyDirectory = "ros2"; } public class DllLoadUtilsUWP : DllLoadUtilsAbs @@ -215,7 +218,7 @@ public class DllLoadUtilsUWP : DllLoadUtilsAbs public override IntPtr LoadLibrary(string fileName) { - string libraryName = fileName + "_native.dll"; + string libraryName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AssemblyDirectory, fileName + "_native.dll"); IntPtr ptr = LoadPackagedLibraryUWP(libraryName); if (ptr == IntPtr.Zero) { @@ -228,6 +231,9 @@ public override IntPtr LoadLibrary(string fileName) public class DllLoadUtilsWindowsDesktop : DllLoadUtilsAbs { + [DllImport("kernel32.dll", EntryPoint = "GetLastError", SetLastError = true, ExactSpelling = true)] + private static extern int GetLastError(); + [DllImport("kernel32.dll", EntryPoint = "LoadLibraryA", SetLastError = true, ExactSpelling = true)] private static extern IntPtr LoadLibraryA(string fileName, int reserved = 0); @@ -243,7 +249,7 @@ public class DllLoadUtilsWindowsDesktop : DllLoadUtilsAbs public override IntPtr LoadLibrary(string fileName) { - string libraryName = fileName + "_native.dll"; + string libraryName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AssemblyDirectory, fileName + "_native.dll"); IntPtr ptr = LoadLibraryA(libraryName); if (ptr == IntPtr.Zero) { @@ -287,7 +293,7 @@ public override IntPtr GetProcAddress(IntPtr dllHandle, string name) public override IntPtr LoadLibrary(string fileName) { - string libraryName = "lib" + fileName + "_native.so"; + string libraryName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AssemblyDirectory, "lib" + fileName + "_native.so"); IntPtr ptr = dlopen(libraryName, RTLD_NOW); if (ptr == IntPtr.Zero) { @@ -334,7 +340,7 @@ public override IntPtr GetProcAddress(IntPtr dllHandle, string name) public override IntPtr LoadLibrary(string fileName) { - string libraryName = "lib" + fileName + "_native.dylib"; + string libraryName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AssemblyDirectory, "lib" + fileName + "_native.dylib"); IntPtr ptr = dlopen(libraryName, RTLD_NOW); if (ptr == IntPtr.Zero) {