Calling Julia from .NET

2,149 views
Skip to first unread message

Guido De Bouver

unread,
Sep 26, 2014, 5:55:55 AM9/26/14
to julia...@googlegroups.com
dear Julia users,

I would need to call Julia from VB .NET. I have seen it is possible to do so from C#, but is it possible to call from VB .NET ? If so, any examples available ?
Thanks for this great product

Isaiah Norton

unread,
Sep 26, 2014, 9:05:24 AM9/26/14
to julia...@googlegroups.com

I am not aware of any VB examples, but if you have c# examples to start from then the same native invocation should be possible from VB.

Stefan Babinec

unread,
Sep 26, 2014, 10:33:53 AM9/26/14
to julia...@googlegroups.com
Hi Guido.

Can you please point me to C# example ? 

I would appreciate your help.

Thanks.

Guido De Bouver

unread,
Sep 29, 2014, 12:37:27 PM9/29/14
to julia...@googlegroups.com
I have not found the C# examples, but I have not looked for them. Sorry for that.

So, any help on this, how could we call Julia from .NET ???? 

Stefan Karpinski

unread,
Sep 29, 2014, 2:52:10 PM9/29/14
to julia...@googlegroups.com
I assume that you can call C libraries from .NET, right? The C library for Julia is libjulia – how to call it from C is explained in the embedding docs, calling it from .NET should be similar.

Tobias Knopp

unread,
Sep 29, 2014, 4:11:48 PM9/29/14
to julia...@googlegroups.com
yep, I have done this (mostly for fun) before and it works. One needs some experience with P/Invoke of course but this is no magic but similar to our ccall.

Cheers,

Tobi

Isaiah Norton

unread,
Sep 29, 2014, 11:58:22 PM9/29/14
to julia...@googlegroups.com
I tried this some time ago during 0.2, so to make sure it still works I made a minimal translation of the embedding example to C#:

```
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))");
        }
    }
}
```

I compiled this, copied the binary into `Julia-0.3.0\bin`, and it works:

```
C:\cmn\Julia-0.3.0>bin\ConsoleApplication2.exe
1.4142135623730951
```

Isaiah Norton

unread,
Sep 30, 2014, 8:03:42 AM9/30/14
to julia...@googlegroups.com

I should mention that it is necessary to change the project target CPU from the default Any to x64 or x86 to match the libjulia architecture.

Stefan Babinec

unread,
Sep 30, 2014, 9:08:11 AM9/30/14
to julia...@googlegroups.com
Hi Isaiah.

I tried and got this error:
"System image file " ?l\../lib/julia/sys.ji" not found"

System Image sys.ji looks to be on his place and I have no problem running Julia.

Thanks.

Isaiah Norton

unread,
Sep 30, 2014, 10:57:41 AM9/30/14
to julia...@googlegroups.com
Try running it from the Julia directory as `bin\CommandLine2.exe`. This is very much a minimal example; for general use, the bin directory should be passed as an argument to `jl_init`:


To go much further than this will really require to dig in to both jlapi.c and the general Julia source code. Be aware that dealing with type translation and garbage collection are both non-trivial. See also `examples/embedding.c` in the julia tree, and several previous discussion on the mailing list. 

Stefan Babinec

unread,
Sep 30, 2014, 11:24:46 AM9/30/14
to julia...@googlegroups.com
I've copied exe file directly to julia's bin directory Isaiah.

And I get the above mentioned error when I try to run it in the bin directory.

Isaiah Norton

unread,
Sep 30, 2014, 6:16:15 PM9/30/14
to julia...@googlegroups.com
The ">" indicates the cmd prompt working directory:

```
C:\cmn\Julia-0.3.0>bin\ConsoleApplication2.exe
1.4142135623730951
```
Otherwise, try passing the bin path as a char* to jl_init as suggested.

Stefan Babinec

unread,
Oct 2, 2014, 3:59:42 AM10/2/14
to julia...@googlegroups.com
Thanks Isaiah.

It works.

I also tried to pass Julia's system image to jl_init and with the little help 

of SetDllDirectory method it works properly when i try to run it in console. 

*******
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool SetDllDirectory(string lpPathName);

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

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

        static void Main(string[] args)
        {
            SetDllDirectory(@"c:\Users\SB\AppData\Local\Julia-0.3.0\bin\");

            jl_init(@"c:\Users\SB\AppData\Local\Julia-0.3.0\bin\"); 

            jl_eval_string("print(sqrt(2.0))");
        }
    }
}
*******

But when I try to run it directly in VS 2103 in (Debug/Release) I get 

following error from jl_init:

"System.AccesViolationException.
Attempted to read or write protected memory. This is often an indication that 

other memory is corrupt."

Small piece from :

"This issue shouldn't happen in managed code. The problem is typically caused by some component (typically unmanaged, but can also be a bad P/Invoke signature) that corrupts the program's memory."

I tried also to suppress this exception by disabling 
"Tools menu ->Options -> Debugging -> General -> Suppress JIT optimization on module load"
but with no success ?

Any ideas ?

Best regards.

Stefan.

Isaiah Norton

unread,
Oct 2, 2014, 8:53:50 AM10/2/14
to julia...@googlegroups.com

Hi Stefan, I'm not sure. I guess it might be failing the first time it tries to map an executable page. You might find some clues on the llvm mailing list. But the bigger issue is that the msvc debugger won't be very useful because msvc doesn't know anything about the jit'd object format (still ELF I think). If you want to use a debugger you should download the mingw64 tools in readme.windows and use gdb.

Stefan Babinec

unread,
Oct 3, 2014, 2:23:34 AM10/3/14
to julia...@googlegroups.com
Hi Isaiah.

Yes, you are probably right. Maybe staticaly compiled Julia would help in this case of debugging under msvc.

Another question.

How am I able to call from .NET my own methods written in Julia  ?
How am I able to let know libjulia.dll that such methods exist ?

Thanks.

Stefan.

Isaiah Norton

unread,
Oct 3, 2014, 9:13:53 AM10/3/14
to julia...@googlegroups.com

You can call your methods the same way you would call them from C, using ccall and .NET interop to expose managed functions with a C ABI.

(To be honest, if I was stuck with .NET and wanted a nicer language I would probably just use F#)

Greg Trzeciak

unread,
Oct 4, 2014, 11:31:01 AM10/4/14
to julia...@googlegroups.com
If performance is not an issue you could use WebSockets and eval(). 

It's my first day here and using Julia so don't take it as recommendation - more like an option to explore.


On Friday, September 26, 2014 11:55:55 AM UTC+2, Guido De Bouver wrote:

Tony Kelman

unread,
Oct 7, 2014, 2:34:22 AM10/7/14
to julia...@googlegroups.com
It should probably be noted in this thread that Julia master can now be compiled using Visual Studio (2013, only really tested with 64 bit so far since I still need to build new LLVM binaries for 32 bit), see https://github.com/JuliaLang/julia/pull/7761 for instructions and more information. It can start up a basic REPL, but many operations don't work - there are issues with 128-bit integer intrinsics in LLVM, various headers that likely aren't preprocessed correctly yet, etc. And the "build system" is a hacky mess of a shell script, plus minor tweaks to the existing makefiles, that still has to run in msys. A much better long-term solution would be to start porting as much of Julia's build system to cmake as possible, downloading mingw binaries for any dependencies that can't be easily built using MSVC (openblas, arpack, gmp). This may eventually enable better debugging of Julia on Windows using native Visual-Studio-format debuggers rather than MinGW gdb.

Stefan Babinec

unread,
Oct 29, 2014, 6:01:22 AM10/29/14
to julia...@googlegroups.com
It's quite interesting that my sample above works perfectly under Win 7 but when I tried to repeat it under Win 8.1 I got following error (running from console):

**************************
Exception: EXCEPTION_ACCESS_VIOLATION at 0x8f6d37d2e4 -- unknown function (ip: 1832375012)
unknown function (ip: 1832375012)
anonymous at inference.jl:410
unknown function (ip: 1824097072)
builtin_tfunction at inference.jl:541
unknown function (ip: 1824073740)
abstract_call at inference.jl:860
unknown function (ip: 1824073740)
abstract_eval_call at inference.jl:900
abstract_eval at inference.jl:935
unknown function (ip: 1824073740)
abstract_eval_arg at inference.jl:866
unknown function (ip: 1824073740)
abstract_eval_call at inference.jl:878
abstract_eval at inference.jl:935
unknown function (ip: 1824073740)
abstract_interpret at inference.jl:1090
unknown function (ip: 1824073740)
typeinf at inference.jl:1409  at  (unknown line)
unknown function (ip: 1824073740)
abstract_call_gf at inference.jl:726
unknown function (ip: 1824073740)
abstract_call at inference.jl:819
unknown function (ip: 1824073740)
abstract_eval_call at inference.jl:900
abstract_eval at inference.jl:935
unknown function (ip: 1824073740)
abstract_interpret at inference.jl:1098
unknown function (ip: 1824073740)
typeinf at inference.jl:1409  at  (unknown line)
unknown function (ip: 1824073740)
typeinf_ext at inference.jl:1216
unknown function (ip: 1824073740)
unknown function (ip: 1824068669)
unknown function (ip: 1824071645)
unknown function (ip: 1824073841)
unknown function (ip: 1824123220)
unknown function (ip: 1824358486)
unknown function (ip: 1824333072)
unknown function (ip: 1824390705)
unknown function (ip: 287507815)
unknown function (ip: 1432107587)
unknown function (ip: -2)
unknown function (ip: -1540888000)
unknown function (ip: -1540887784)
unknown function (ip: 1432107587)
unknown function (ip: 1547850588)
unknown function (ip: 1549890657)
unknown function (ip: 1967807596)
unknown function (ip: 775106096)
unknown function (ip: 23662)
unknown function (ip: -1540888544)
unknown function (ip: 1)
unknown function (ip: -1540888560)
unknown function (ip: 44)
unknown function (ip: -1540888560)
unknown function (ip: 286343240)
unknown function (ip: 286343240)
unknown function (ip: 0)
unknown function (ip: -1540888560)
unknown function (ip: 1914573205)
unknown function (ip: 1894973440)
unknown function (ip: -1540887488)
unknown function (ip: 286343240)
unknown function (ip: 286343240)
unknown function (ip: -1540888544)
unknown function (ip: 287507815)
unknown function (ip: -1540888512)
unknown function (ip: -1539105552)
unknown function (ip: 1914573205)
unknown function (ip: -1540888272)
unknown function (ip: -1540887488)
unknown function (ip: -1540888000)
unknown function (ip: -1540887784)
unknown function (ip: -1540887440)
unknown function (ip: -1540887624)
unknown function (ip: -1540887488)
unknown function (ip: -1540887896)
unknown function (ip: -1540887784)
unknown function (ip: -1540888176)
unknown function (ip: 287506648)
unknown function (ip: -1504039680)
unknown function (ip: -1539105552)

Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.
   at ConsoleApplication1.Program.jl_init(String message)
   at ConsoleApplication1.Program.Main(String[] args) in c:\_Julia Test\Julia_In
terop\Program.cs:line 28

**************************

Same version of VS 2013 for recompiling, same version of .NET, same sample. 

Any ideas ?

Thanks.




Reply all
Reply to author
Forward
0 new messages