Created
April 18, 2022 20:38
-
-
Save KaceCottam/ef561f65ff884db41d00e614921fb9b5 to your computer and use it in GitHub Desktop.
dll import with c interop using c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__attribute__((dllexport)) void set1(int* a) { | |
*a=1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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