Embedding Julia in Csharp or VB.net

Hello every one,

I try to embedding Julia in C# but when I call jl_init() after declaration of dllImport like, like that:
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{
class Program
{
[DllImport(“libjulia.dll”)]
static extern void jl_init();

    [DllImport("libjulia.dll")]
    static extern void jl_eval_string(string message);

    static void Main(string[] args)
    {
        jl_init();
        jl_eval_string("print(sqrt(2.0))");
    }
}

}

jl_init() crash with error System.stackoverflowexception

If I trying to bypass jl_init() and so execute directly jl_eval_string with or less pointer I have an other error message:
Unhandled Exception: System.AccessViolationException: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.

Thanks for your help !

1 Like

See Interfacing Julia & Red + error with julia 0.6.rc3 - #7 by ihnorton

This should be fixed in the next 0.6 point release:

https://github.com/JuliaLang/julia/pull/23255

1 Like

Thank you very much.
I will try

I found how to do it, so I shared an example:

static void Main(string[] args)
        {
            jl_is_initialized();
            int stackSize = 1024 * 1024 * 64;
            Thread th = new Thread(() =>
            {
                jl_init();
                IntPtr module;
                string p;
                p = @"print(sqrt(2.0))";
                module = jl_eval_string(p);
            },
                stackSize);

            th.Start();
            th.Join();

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

I found the solution here:

3 Likes

Thank you very much !