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; } 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) {