How to call native (C or assembly) code from a .NET (C#) program (under Linux)?

  • Thread starter Thread starter WisdomButcher
  • Start date Start date
W

WisdomButcher

Guest
The error of dotnet run defined here. I'm trying to run assembly code via C library DllImport in C# with .Net core x64 Linux. How to do this correctly?

C#:

using System.Runtime.InteropServices;
class Program{
[DllImport("lib.so")] public static extern int foo ();
static void Main(string[] args)
{
int code = foo();
System.Console.WriteLine(code);
}
}

C my.c:

#define EXPORT __attribute__((visibility("default")))
EXPORT int foo(void);
int foo(void)
{
extern int _start();
return _start();
}

Assembly asm.s:

.text
.globl _start

_start:
movq $231, %rax
movq $1, %rdi
syscall

The program goal is to return zero result code from assembly via C library. The following error message is printed; no exception is raised:

Hosting components are already initialized.
Re-initialization to execute an app is not allowed.

I have compiled C and Assembly sources using GCC:

gcc -shared -fpic -o lib.so my.c asm.s

Continue reading...
 
Back
Top