# Importing a system image dll with c#/c++/c

**URL:** <https://discourse.julialang.org/t/importing-a-system-image-dll-with-c-c-c/43836>\
**Category:** General Usage\
**Tags:** question, embedding\
**Created:** [July 28, 2020, 8:02pm UTC](https://discourse.julialang.org/t/importing-a-system-image-dll-with-c-c-c/43836 "2020-07-28T20:02:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joschv](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@joschv](https://discourse.julialang.org/u/joschv)\
**Post date:** [July 28, 2020, 8:02pm UTC](https://discourse.julialang.org/t/importing-a-system-image-dll-with-c-c-c/43836/1 "2020-07-28T20:02:56Z")

</div>

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.

1. The System Image is created like this:

```julia
PackageCompiler.create_sysimage(:MyModule;
        sysimage_path = "path.../MyModule.dll",
        precompile_execution_file = "path.../Precompile_calls.jl")

```

1. The module looks like this:

```julia
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

```

1. Trying to import the DLL and calling the function(s) like this:

```julia
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:

```julia
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

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 28, 2020, 8:16pm UTC](https://discourse.julialang.org/t/importing-a-system-image-dll-with-c-c-c/43836/2 "2020-07-28T20:16:58Z")

</div>

[https://docs.julialang.org/en/v1/manual/embedding/#High-Level-Embedding-1](https://docs.julialang.org/en/v1/manual/embedding/#High-Level-Embedding-1)

and

[https://github.com/JuliaLang/PackageCompiler.jl/blob/master/src/embedding\_wrapper.c](https://github.com/JuliaLang/PackageCompiler.jl/blob/master/src/embedding_wrapper.c)

might be useful. Note e.g. that you need to initialize the Julia runtime.

---

<div class="post-metadata">

**Author:** ![joschv](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@joschv](https://discourse.julialang.org/u/joschv)\
**Post date:** [July 28, 2020, 9:19pm UTC](https://discourse.julialang.org/t/importing-a-system-image-dll-with-c-c-c/43836/3 "2020-07-28T21:19:05Z")

</div>

Thank you! I already made contact with these sources but was and am struggling and confirmation that i’m missing a lot (e.g Julia runtime initialization) helps.  
“Update:” I am trying to follow [Embedding Julia · The Julia Language](https://docs.julialang.org/en/v1/manual/embedding/#High-Level-Embedding-on-Windows-with-Visual-Studio-1) and currently stuck with a [#error: “No Target Architecture”](https://docs.microsoft.com/en-gb/cpp/error-messages/compiler-errors-1/fatal-error-c1189?view=vs-2019) when trying to build and run.

---

<div class="post-metadata">

**Author:** ![joschv](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@joschv](https://discourse.julialang.org/u/joschv)\
**Post date:** [July 30, 2020, 12:02pm UTC](https://discourse.julialang.org/t/importing-a-system-image-dll-with-c-c-c/43836/4 "2020-07-30T12:02:36Z")

</div>

So just in case someone stumbles upon the same problems as me.  
When following that example i got several errors like “PCONTEXT is undefined” “PLIST\_HEADER is undefined”. I found a solve or workaround for the errors [here](https://sourceforge.net/p/mingw-w64/mailman/message/19399380/).  
Also i added #include \<julia.h\> in the main file since there was no “stdafx.h” created as far as i know. The file that actually compiles for me:

```julia
#define _AMD64_
#include <iostream>
#include <julia.h>

int main(int argc, char* argv[])
{
    /* required: setup the Julia context */
    jl_init();

    /* run Julia commands */
    jl_eval_string("print(sqrt(2.0))");

    /* strongly recommended: notify Julia that the
         program is about to terminate. this allows
         Julia time to cleanup pending write requests
         and run all finalizers
    */
    jl_atexit_hook(0);
    return 0;
}

```
