Skip to content

Instantly share code, notes, and snippets.

@KaceCottam
Created April 18, 2022 20:38
Show Gist options
  • Save KaceCottam/ef561f65ff884db41d00e614921fb9b5 to your computer and use it in GitHub Desktop.
Save KaceCottam/ef561f65ff884db41d00e614921fb9b5 to your computer and use it in GitHub Desktop.
dll import with c interop using c#
__attribute__((dllexport)) void set1(int* a) {
*a=1;
}
a.so: a.c
gcc -fPIC -shared -o a.so a.c
.PHONY: run
run: a.so Program.cs
LD_LIBRARY_PATH="./;$$LD_LIBRARY_PATH" dotnet run
// See https://aka.ms/new-console-template for more information
using System.Runtime.InteropServices;
Console.WriteLine("Hello, World!");
[DllImport("a.so")]
static extern unsafe void set1(int* ptr);
int i = 0;
Console.WriteLine(i);
unsafe
{
set1(&i);
}
Console.WriteLine(i);
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment