Embedding Julia Script into C#: jl_get_function doesn't exist?

Viewed 699

Currently I am working on a project where I have to get some Julia scripts written by other people to be called from C# within Unity. I've been trying to do some basic examples just to see what works and what doesn't. On the Julia documentation, it says to use the function: jl_get_function to grab a pointer to a function withing a julia module. However, I get an EntryPointNotFound in the libjulia.dll, and when I open up the dll on my computer with DependencyWalker I can't find a function called that. Am I crazy or did I install something oddly? other things like jl_eval_string and jl_unbox_float64 work fine.

Also, I'm not entirely sure how to get a pointer to the module for jl_get_function. I've thought of grabbing a pointer from a Memory Mapped file object, or from grabbing the IntPtr from jl_eval_string(include([module name in directory]));, but I'm not sure.

Here's my code in Julia for this test.

module TestModule

export calculate

function calculate(a::Float64,b::Float64)::Float64
  return 3a+b^2
end
function calcMore(a,b)
  return ones(a,b)::Array{Float64,2};
end
function moreTest(a::Float64, b::Float64)
  return (a+b)::Float64;
end
end

and here's my code in C#, that's been snipped a bit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace TestCInCSharp
{
class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool SetDllDirectory(string lpPathName);

    [DllImport("libjulia.dll", SetLastError = true)]
    public static extern void jl_init(string path);

    [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr jl_eval_string(string input);

    [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr jl_box_float64(float value);

    [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern double jl_unbox_float64(IntPtr value);

    [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr jl_get_function(IntPtr func, string name);

    [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr jl_call2(IntPtr func, IntPtr v1, IntPtr v2);

    [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern void jl_atexit_hook(int a);

    static void Main(string[] args)
    {
        string p = @"C:\Users\schulk4\Documents\Programming\TestJuliaSim\Assets\test_julia.jl";
        string julia_path = @"C:\Users\schulk4\AppData\Local\Julia-0.5.2\bin";
        IntPtr module, module2;
        IntPtr a, b, c;

        SetDllDirectory(julia_path);
        jl_init(julia_path);
        p = @"C:\\Users\\schulk4\\Documents\\Programming\\TestJuliaSim\\Assets\\test_julia.jl";
        p = "include(\"" + p + "\")";
        module = jl_eval_string(p); //holds module pointer?
        a = jl_eval_string("TestModule.calculate(3.0,4.0)");
        double d = jl_unbox_float64(a);
        Console.WriteLine(d);
        a = jl_eval_string("TestModule.calculate");
        b = jl_box_float64(3.0f);
        c = jl_box_float64(4.0f);
        module2 = jl_call2(a, b, c);
        d = jl_unbox_float64(module2);
        Console.WriteLine(d);

        a = jl_eval_string("TestModule.moreTest");
        b = jl_box_float64(12.0f);
        c = jl_box_float64(13.0f);
        module2 = jl_call2(a, b, c);
        d = jl_unbox_float64(module2);
        Console.WriteLine(d);

        IntPtr f = jl_get_function(module, "calculate"); //EntryPointNotFoundException

        jl_atexit_hook(0);
        Console.ReadLine();
    }
}

}

You can see my attempts at getting a pointer to a function with jl_eval_string in the code. This is an example run before the exception:

25
1.5977136277678E-314
1.08223857600744E-314

I've been running into all sorts of problems, I was just wondering if anybody would be able to help me. I am not very familiar with this topic, I learned about P/Invoke about a week ago.

1 Answers
Related