# Interoperability with .NET

**URL:** <https://discourse.julialang.org/t/interoperability-with-net/19611>\
**Category:** General Usage\
**Tags:** dotnet, interoperability\
**Created:** [January 14, 2019, 11:09am UTC](https://discourse.julialang.org/t/interoperability-with-net/19611 "2019-01-14T11:09:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rudreshahk](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@rudreshahk](https://discourse.julialang.org/u/rudreshahk)\
**Post date:** [January 14, 2019, 11:09am UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/1 "2019-01-14T11:09:22Z")

</div>

We have some Dimension Selector built in .NET which i want to invoke from Julia. IS there any Julia to .NET interoperability tools?

I have used [R.NET](http://R.NET) for R to .NET connection and Pythonnet for Python to .NET connection. So looking to see if anything similar is available for Julia to .NET.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [January 14, 2019, 5:14pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/2 "2019-01-14T17:14:02Z")

</div>

Not that I’m aware of. I agree, it would be super cool if there was a package that provided interop with .Net, there is a lot of cool software out there on that platform!

And with .Net Core it should be quite simple to a) make that work on all platforms, not just Windows, and b) completely self-host .Net, i.e. have a private installation in the interop package, no dependency on anything systemwide etc.

PS: This probably doesn’t belong in this discourse section, it seems entirely unrelated to VS Code. Maybe one of the mods can move the thread?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 14, 2019, 6:36pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/3 "2019-01-14T18:36:54Z")

</div>

Moved to Usage.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 15, 2019, 9:42am UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/4 "2019-01-15T09:42:37Z")

</div>

Yes, such a package for .NET interop would be great.  
For a optimization project with some complex GUI I am currently working on a C# application calling julia like this:

```julia
    class Julia
    {
        [DllImport("kernel32.dll")]
        public static extern bool SetDllDirectory(string pathName);

        [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void jl_init(string julia_home_dir);

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

        //[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 void jl_atexit_hook(int a);

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

        public static void julia()
        {
            SetDllDirectory(@"c:\Program Files\Julia-0.6.2\bin\");

            jl_init(@"c:\Program Files\Julia-0.6.2\bin\");

            jl_eval_string("[\"2\" \"3\" \"4\"]");
            jl_eval_string("write(\"test.out\",\"helloxx2\");");
            jl_eval_string("println(\"test.out\"*\"hello2\");");

            jl_atexit_hook(0);
        }
    }

```

Still experimenting with it, with not so much success.

---

<div class="post-metadata">

**Author:** ![gabrielfreire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielfreire/32/8799_2.png) [@gabrielfreire](https://discourse.julialang.org/u/gabrielfreire)\
**Post date:** [June 23, 2019, 5:39pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/5 "2019-06-23T17:39:48Z")

</div>

Any updates here ? 😃

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 24, 2019, 1:23pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/6 "2019-06-24T13:23:57Z")

</div>

Currently I succeeded only with julia 0.6.2 with the following code in MS Visual Studio Community 2017 Version 15.9.12 and .NET Framework Version 4.7.03190:

```julia
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace julia
{
    class NativeMethods
    {
        
        [DllImport("kernel32.dll")]
        public static extern bool SetDllDirectory(string pathName);

        [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void jl_init(string julia_home_dir);

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

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

        public static void Julia( string juliaDir, string juliaScriptDir, string projectPath )
        {
            
            SetDllDirectory(juliaDir);

            jl_init(juliaDir);

            string script = (juliaScriptDir + "myJulia.jl").Replace(@"\",@"\\");
            jl_eval_string("include(\"" + script + "\");");

            jl_atexit_hook(0);

        }
    }
}

```

The parameters are

```julia
string juliaDir = "d:\\Temp\\julia-0.6.2\\bin\\"
string juliaScriptDir = "d:\\Temp\\julia-bin\\"

```

`string projectPath` is not used yet.

Calling

```julia
julia.NativeMethods.Julia(juliaDir, juliaScriptDir, projectPath);

```

runs `myJulia.jl` in `...\bin\x64\Debug\` which is the debug working folder of the Visual Studio project.

---

<div class="post-metadata">

**Author:** ![gabrielfreire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielfreire/32/8799_2.png) [@gabrielfreire](https://discourse.julialang.org/u/gabrielfreire)\
**Post date:** [June 24, 2019, 6:31pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/7 "2019-06-24T18:31:33Z")

</div>

Great, thank you. Do you have this project on Github ?

---

<div class="post-metadata">

**Author:** ![gabrielfreire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielfreire/32/8799_2.png) [@gabrielfreire](https://discourse.julialang.org/u/gabrielfreire)\
**Post date:** [June 24, 2019, 8:58pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/8 "2019-06-24T20:58:15Z")

</div>

I used the same code as you in .NET Core 3.0 and Julia 1.3-DEV and got this

```julia
System.EntryPointNotFoundException: 'Unable to find an entry point named 'jl_init' in DLL 'libjulia.dll'.'

```

Does anybody have an idea of what might be wrong ?

I searched the internet, found some stuff related to the creation of `libjulia.def` and `libjulia.lib` followed the tutorials and all but it still throws the same error.

I’ve also tried to change the signature of `jl_init` because i’ve noticed that there is only one `jl_init()` in this version which is

```nohighlight
JL_DLLEXPORT void jl_init(void);

```

but no success

---

<div class="post-metadata">

**Author:** ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)\
**Post date:** [June 24, 2019, 10:30pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/9 "2019-06-24T22:30:10Z")

</div>

You probably need to use the symbol `jl_init__threading`, which is the real function name behind a C preprocessor `jl_init` macro: [https://github.com/JuliaLang/julia/blob/cef655a68a4e824cbe7cca582256d61aa72eb725/src/julia.h#L1516-L1518](https://github.com/JuliaLang/julia/blob/cef655a68a4e824cbe7cca582256d61aa72eb725/src/julia.h#L1516-L1518)

---

<div class="post-metadata">

**Author:** ![gabrielfreire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielfreire/32/8799_2.png) [@gabrielfreire](https://discourse.julialang.org/u/gabrielfreire)\
**Post date:** [June 24, 2019, 10:32pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/10 "2019-06-24T22:32:31Z")

</div>

It worked! you’re a genious!! Thank you.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 25, 2019, 7:57am UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/11 "2019-06-25T07:57:24Z")

</div>

Thanks a lot. This was the missing link to use c# with julia 1.x.x

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 27, 2019, 7:29am UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/12 "2019-06-27T07:29:26Z")

</div>

I had a hard time now struggling with running a Julia script via `include("...")` from my C# GUI.  
There wasn’t a problem when using Julia 0.6 but with 1.1 it failed silently.

Finally I found a solution and during my way to it I was able to get some exception string from the Julia call.  
This is now my current code to call Julia 1.1 (or above) and run a Julia script from C#:

```julia
using System;

using System.Runtime.InteropServices;
using System.IO;

namespace julia
{
    class NativeMethods
    {
        
        [DllImport("kernel32.dll")]
        public static extern bool SetDllDirectory(string pathName);

        [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void jl_init__threading();

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

        [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr jl_exception_occurred();

        [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern IntPtr jl_typeof_str(IntPtr value);

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

        public static void Julia(string juliaDir, string juliaScriptDir, string projectPath)
        {
            SetDllDirectory(juliaDir);

            jl_init__threading();

            string script = @"Base.include(Base,raw""d:\Temp\test.jl"";)";
            //string script = @"include(raw""d:\Temp\test.jl"";)";
            jl_eval_string(script);
            IntPtr exception = jl_exception_occurred();
            if (exception != (IntPtr)0x0)
            {
                string exceptionString = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(jl_typeof_str(jl_exception_occurred()));
                Console.WriteLine(exceptionString);
            }

            jl_atexit_hook(0);
        }
    }
}

```

If you use the out-commented statement

```julia
string script = @"include(raw""d:\Temp\test.jl"";)";

```

the `exceptionString` will be `"UndefVarError"` and the script will not run.

The call is

```julia
julia.NativeMethods.Julia(juliaDir, juliaScriptDir, projectPath);

```

Parameter `juliaDir` must be the folder where libjulia.dll resides, e.g. `c:\\Programs\\Julia-1.1.0\\bin\\`.

The other two paramers (`juliaScriptDir` and `projectPath`) are not used in this example.

---

<div class="post-metadata">

**Author:** ![gabrielfreire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielfreire/32/8799_2.png) [@gabrielfreire](https://discourse.julialang.org/u/gabrielfreire)\
**Post date:** [July 9, 2019, 6:45pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/13 "2019-07-09T18:45:05Z")

</div>

Hi Guys, have any of you ever tried/managed to build/publish a self-container C# application with Julia embedded ? If yes, could you give me a direction ?

Cheers

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 10, 2019, 8:22pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/14 "2019-07-10T20:22:06Z")

</div>

I am currently on this, I guess, but I am not sure what you mean with “a self-container C# application with Julia embedded”. Can you elaborate?

---

<div class="post-metadata">

**Author:** ![gabrielfreire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielfreire/32/8799_2.png) [@gabrielfreire](https://discourse.julialang.org/u/gabrielfreire)\
**Post date:** [July 10, 2019, 8:27pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/15 "2019-07-10T20:27:53Z")

</div>

Hi @oheil, sure, by self-contained c# application i mean the usual .NET Core self-contained publish step that ships the .NET core runtime, i was wondering if we could also ship the Julia runtime along and have both languages packaged into this self-contained published `.exe`.

Example, when we publish any .NET core application it packages a bunch of `System.XXX.dll` like `System.Collections.dll`, i was wondering if we could also package the `libjulia.dll`s and it’s dependencies in order to have everything in one package, i think this would allow for very powerful applications.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [July 10, 2019, 8:40pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/16 "2019-07-10T20:40:05Z")

</div>

Ah, ok. No, I am not this far with my application yet. When it comes to publishing everything using a installer my thoughts were on NSIS just because I used it before.  
I will check this out when times come but I expect some month to come .

---

<div class="post-metadata">

**Author:** ![gabrielfreire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabrielfreire/32/8799_2.png) [@gabrielfreire](https://discourse.julialang.org/u/gabrielfreire)\
**Post date:** [July 10, 2019, 8:58pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/17 "2019-07-10T20:58:58Z")

</div>

Ah ok, i was just wondering the applicability of this approach since `PackageCompiler.jl` is not very nice yet. I think my problem is the fact that distributing a Julia application is so hard right now, maybe merging it with C# could mitigate this.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 29, 2020, 11:55am UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/18 "2020-11-29T11:55:09Z")

</div>

I recently got some likes on this thread, so I opened it again to see what I did here.  
The project I worked on is publicly available: [https://github.com/oheil/doTimeTable/](https://github.com/oheil/doTimeTable/)  
and shows how Julia can be embedded in a complete C# application installer. Which means, installing the application is all the user needs to do (no preinstalling of Julia or something like that).  
The link from C# to Julia is mainly in GUI/doTimeTable/Julia.cs. Julia is called in a separate thread.

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [November 29, 2020, 1:38pm UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/19 "2020-11-29T13:38:10Z")

</div>

Nice work oheil. I had to figure this out at a previous job - it was not fun.

---

<div class="post-metadata">

**Author:** ![HyperSphereStudio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hyperspherestudio/32/32322_2.png) [@HyperSphereStudio](https://discourse.julialang.org/u/HyperSphereStudio)\
**Post date:** [January 11, 2022, 5:43am UTC](https://discourse.julialang.org/t/interoperability-with-net/19611/21 "2022-01-11T05:43:50Z")

</div>

Note: Repost to give greater detail

Here is my library on Julia & .NET Interoperability: [GitHub - HyperSphereStudio/JULIAdotNET](https://github.com/HyperSphereStudio/JULIA.Net)

It uses the C-Interface of both languages to translate between the languages very quickly compared to normal server/client based systems.

```julia
 Julia.Init();
  JLFun fun = Julia.Eval("t(x) = sqrt(x)");
  double result = (double) fun.Invoke(2);
  object dotNetObject = fun.Invoke(3).Value;
  Julia.Exit(0);  

```

It is very new but it has support for various built in Julia features such as a JLArray to allow manipulating jl\_arrays neatly.

You can also work with .NET from Julia (Experimental at the moment). It utilizes .NET reflection

In C#:

```nohighlight
public class TestClass{
    public long g;
    public TestClass(long g) => this.g = g;
}

```

Working from Julia:

```julia
sharpType = SharpType("TestClass") #Retrieve Type
sharpCon = SharpConstructor(sharpType, 0) #Get Constructor at Index 0
o = sharpCon(6) #Create Instance
sharpField = SharpField(sharpType, "g") #Get Field
println(sharpField(o)) #Get Field Value

```

[Next page](https://discourse.julialang.org/t/interoperability-with-net/19611.md?page=2)
