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!
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 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 would work?
Have you considered or tried using pythondotnet via PyCall.jl?
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
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. Anything mentioned about how to do this from C is directly applicable for doing this in Julia.
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
There are a couple ways that I’ve sorted this out:
- The DotNET.jl package
- Use PowerShell. For example I can call my .Net Managed DLL using the following code:
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`)