diff --git a/src/csharp/Julia.cs b/src/csharp/Julia.cs index f9a03aa..4e32589 100644 --- a/src/csharp/Julia.cs +++ b/src/csharp/Julia.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; +using System.IO; //Written by Johnathan Bizzano namespace JuliaInterface @@ -15,10 +16,11 @@ public class Julia public static void Init() { + var os = OperatingEnvironment.GetEnvironment(); var proc = new Process { StartInfo = new ProcessStartInfo { - FileName = "where.exe", - Arguments = "Julia", + FileName = os.GetWhereExe(), + Arguments = "julia", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true @@ -28,25 +30,26 @@ public static void Init() while (!proc.StandardOutput.EndOfStream) { string location = proc.StandardOutput.ReadLine(); - if (location.Contains("julia.exe")){ - Init(location.Substring(1, location.Length - 11)); + if (location.Contains("julia")){ + location = os.TrimJuliaPath(location); + Init(location); return; - } - + } } throw new Exception("Julia Path Not Found"); } - public static void Init(string dir){ - JuliaCalls.SetDllDirectory(dir); + var env = Environment.CurrentDirectory; + Environment.CurrentDirectory = dir; JuliaCalls.jl_init(); JuliaCalls.jl_eval_string(System.Text.Encoding.UTF8.GetString(Resource1.JuliaInterface)); JLModule.init_mods(); JLType.init_types(); JLFun.init_funs(); NativeSharp.init(); + Environment.CurrentDirectory = env; } public static void SetGlobal(JLModule m, JLSym sym, JLVal val) @@ -68,6 +71,7 @@ public static void CheckExceptions(){ } public static void Exit(int code) => JuliaCalls.jl_atexit_hook(code); + public static JLVal Eval(string str){ var val = JuliaCalls.jl_eval_string(str); CheckExceptions(); diff --git a/src/csharp/JuliaCalls.cs b/src/csharp/JuliaCalls.cs index 3ab9533..90624dd 100644 --- a/src/csharp/JuliaCalls.cs +++ b/src/csharp/JuliaCalls.cs @@ -8,10 +8,6 @@ namespace JuliaInterface public class JuliaCalls { - - [DllImport("kernel32.dll")] - public static extern bool SetDllDirectory(string path); - public enum JLIMAGESEARCH{ JL_IMAGE_CWD = 0, diff --git a/src/csharp/OperatingEnvironment.cs b/src/csharp/OperatingEnvironment.cs new file mode 100644 index 0000000..a4b1387 --- /dev/null +++ b/src/csharp/OperatingEnvironment.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace JuliaInterface +{ + public abstract class OperatingEnvironment + { + + public OperatingEnvironment() { } + + public abstract string GetWhereExe(); + public abstract string TrimJuliaPath(string s); + + public static OperatingEnvironment GetEnvironment(){ + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + return new WindowsEnvironment(); + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + return new LinuxEnvironment(); + else throw new Exception("Unsupported Operating System!"); + } + } + + public class WindowsEnvironment : OperatingEnvironment + { + + public override string GetWhereExe() => "where.exe"; + + public override string TrimJuliaPath(string s) => s.Substring(0, s.Length - 10); + } + + public class LinuxEnvironment : OperatingEnvironment + { + public override string GetWhereExe() => "which"; + + public override string TrimJuliaPath(string s) => s; + } + + +}