# Is it possible to call C# functions with Julia?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-call-c-functions-with-julia/18979>\
**Category:** New to Julia\
**Created:** [December 25, 2018, 10:12am UTC](https://discourse.julialang.org/t/is-it-possible-to-call-c-functions-with-julia/18979 "2018-12-25T10:12:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cloudostrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cloudostrich/32/35682_2.png) [@cloudostrich](https://discourse.julialang.org/u/cloudostrich)\
**Post date:** [December 25, 2018, 10:12am UTC](https://discourse.julialang.org/t/is-it-possible-to-call-c-functions-with-julia/18979/1 "2018-12-25T10:12:43Z")

</div>

Hi all,  
I am new to Julia, coming from python, and very excited to learn this wonderful language. Like to ask is there a library for Julia to call C# functions (in .Net framework) from Julia, like how Cxx.jl calls C++ from Julia. In python I used to use pythondotnet, which worked flawlessly. Searching through forums and google, what i understand is that julia’s FFI applies only to c and fortran. I have seen a few posts online about calling julia from within C#. Is there a way to go the other way? Calling C# from Julia?  
Thank you all in advance,  
and Merry Christmas and a Happy New Year!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [December 25, 2018, 2:16pm UTC](https://discourse.julialang.org/t/is-it-possible-to-call-c-functions-with-julia/18979/2 "2018-12-25T14:16:33Z")

</div>

As a general rule, anything you can easily call from C will be callable from Julia. For example, there was some early discussion on calling Rust from Julia via `ccall` over in [Interfacing rust? - #5 by Rory-Finnegan](https://discourse.julialang.org/t/interfacing-rust/6389/5) which was apparently quite successful.

I haven’t used C#, but as long as it’s possible to generate a library that you can call from C, you should be able to use that same library from Julia with `ccall`. Perhaps something like [Calling C# from C - Stack Overflow](https://stackoverflow.com/a/6795403) would work?

---

<div class="post-metadata">

**Author:** ![garrison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrison/32/209519_2.png) [@garrison](https://discourse.julialang.org/u/garrison)\
**Post date:** [December 26, 2018, 1:42am UTC](https://discourse.julialang.org/t/is-it-possible-to-call-c-functions-with-julia/18979/3 "2018-12-26T01:42:54Z")

</div>

Have you considered or tried using pythondotnet via PyCall.jl?

---

<div class="post-metadata">

**Author:** ![cloudostrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cloudostrich/32/35682_2.png) [@cloudostrich](https://discourse.julialang.org/u/cloudostrich)\
**Post date:** [December 27, 2018, 2:33am UTC](https://discourse.julialang.org/t/is-it-possible-to-call-c-functions-with-julia/18979/4 "2018-12-27T02:33:19Z")

</div>

Thank you for your prompt replies. Much appreciated. Sorry for my late reply…

@ rdeits : I have looked at ccall, but as I am not fluent in C or C#, am not sure exactly how to go about this. Is there any online resource that might help to understand how to do this?

@garrison : Yes, I have. However, it seems like an inefficient way, calling python to call C#. I was hoping to see if I can find a way to call C# directly, make my code base all Julia. Having said that, this is my fallback plan in the event I cannot find a way to call C# directly from Julia

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [December 28, 2018, 4:17pm UTC](https://discourse.julialang.org/t/is-it-possible-to-call-c-functions-with-julia/18979/5 "2018-12-28T16:17:11Z")

</div>

As rdeits mentioned, there’s a couple solutions, depending on where you want to put the conversion codes. Earlier in the SO thread, someone mentions there’s 4 common patterns: [Calling C# from C - Stack Overflow](https://stackoverflow.com/a/4428336/1712368). Anything mentioned about how to do this from C is directly applicable for doing this in Julia.

---

<div class="post-metadata">

**Author:** ![cloudostrich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cloudostrich/32/35682_2.png) [@cloudostrich](https://discourse.julialang.org/u/cloudostrich)\
**Post date:** [December 30, 2018, 12:27am UTC](https://discourse.julialang.org/t/is-it-possible-to-call-c-functions-with-julia/18979/6 "2018-12-30T00:27:11Z")

</div>

Hi Jameson,  
Thank you for the link. I will look into the 4 patterns and post my solutions once I figure out a workable solution. Thanks again, you guys are so helpful

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [November 4, 2020, 6:27pm UTC](https://discourse.julialang.org/t/is-it-possible-to-call-c-functions-with-julia/18979/7 "2020-11-04T18:27:16Z")

</div>

There are a couple ways that I’ve sorted this out:

1. The DotNET.jl package
2. Use PowerShell. For example I can call my .Net Managed DLL using the following code:

```julia
dllfile = raw"c:\Support.dll" #managed .net dll

cmd = """&{
               Add-Type -Path "$dllfile";

               \$is_inverted=\$false;
               \$sled=[Support.SledType]::Frontal;

               \$target=[Support.SystemData.Result]::TargetResult(\$is_inverted, \$sled);

               \$target_sample_rate=\$target.dT;

         }"""

run(`powershell $cmd`)

```
