Greetings!
I am trying to embed Julia code using C# (or C/C++) and a generated Julia System Image (DLL, on Windows). The system image contains a few (selfmade) functions i am trying to call from C#. These are decorated with Base.@ccallable and inspecting the DLL file with DLL Export Viewer, they look like they are correctly exported.
- The System Image is created like this:
PackageCompiler.create_sysimage(:MyModule;
sysimage_path = "path.../MyModule.dll",
precompile_execution_file = "path.../Precompile_calls.jl")
- The module looks like this:
module MyModule
using CSV
Base.@ccallable function some_main()::Cint
println("Julia Main called.")
try
real_main()
catch
Base.invokelatest(Base.display_error, Base.catch_stack())
return 1
end
return 0
end
function real_main()
println("Call function in same file.")
func1()
println("Call function in another file.")
kniffel(3,5)
return 0
end
Base.@ccallable function func1()::Cint
println("Function1 called.")
return 0
end
include("more.jl")
end # module
- Trying to import the DLL and calling the function(s) like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace CallSysimage
{
class Program
{
[DllImport(@"path.../MyModule.dll")]
public static extern int some_main();
[DllImport(@"path.../MyModule.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern int func1();
static void Main(string[] args)
{
try
{
some_main();
func1();
}
catch (Exception e)
{
Console.WriteLine("Error!");
Console.WriteLine("StackTrace:\n" + e.StackTrace);
Console.WriteLine("Message:\n" + e.Message);
}
}
}
}
results in this exception:
Exception: EXCEPTION_ACCESS_VIOLATION at 0x0 -- Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at CallSysimage.Program.some_main()
at CallSysimage.Program.some_main()
at CallSysimage.Program.Main(System.String[])
How do i call the function from the system image correctly? I am pretty sure that the DLL and the function is “read correctly” since it gets past “DLL not found” and “Entry point not found” errors.
I am using Julia 1.4.2 and VisualStudio 2019 on Windows 10. I tried something similar with C and got a “Segmentation fault”.
I hope someone can help me here! Thank you!
joschv