How to make bindings for a native library using ClangSharp?
For context, this is the library that I'm trying to make bindings for:
https://github.com/paullouisageneau/libjuice/tree/master
(and i'd ideally love to add this to nuget as a package or something)
GitHub
GitHub - paullouisageneau/libjuice: JUICE is a UDP Interactive Conn...
JUICE is a UDP Interactive Connectivity Establishment library - paullouisageneau/libjuice
32 Replies
@あんたのお尻が大きくて私が気絶したいです yeah i thought it was simpel too, but i kept running into this error
run cmd with admin rights?
this seemed to work fine for me
huh
alright lemme try
im on windows if that helps
course you dont need the last line on windows
(and that -wcc is kinda incorrect, it just makes the output cleaner)
like this?
needs to be in quotes in powershell
gotchu
wasnt it include/juice/juice.h
ah
okay cool, now what do i do?
you should just have output you can use in Generated
hm, how do i use it? do i just drop it into a sln or something?
into whatever project, yah
huh, i guess i could just link this as a folder
uh so how do i actually use this?
i can't just do
using Juice;
put the Generated folder in the project folder
huh
and then do i just drag the dll in there as well?
uhh there's a bit about packaging native dependencies on msdn https://learn.microsoft.com/en-us/nuget/create-packages/native-files-in-net-packages
Native files in .NET packages
How to pack native libraries in .NET packages
but otherwise you just tell it to copy to your output
oh man this is really annoying LOL
gonna do this later, but trying to convert the tests into c# is really werid haha
GitHub
libjuice/include/juice/juice.h at master · paullouisageneau/libjuice
JUICE is a UDP Interactive Connectivity Establishment library - paullouisageneau/libjuice
How would you rewrite this in c#?
I was running into really weird pointer conversion stuff and sbyte
And it doesn’t feel like I should have to do that
They give a good explanation as to why that is over here.
https://github.com/dotnet/ClangSharp/issues/428#issuecomment-1454811249
You can create a string with a
sbyte*
via new string(ptr)
This creates an ansi or utf8 string iircIs there a way to do it the other way around?
You want a sbyte* from a string?
Ye
I don't entirely know a way that's built in so i wrote 1 myself
https://github.com/JustArion/Managed_Corsair-SDK/blob/development/src/CorsairSDK/Device/Internal/UTF8StringHandle.cs
While you dont really need to hold on to the lifetime of the string via usings, it it heavily recommended to pass the entire class into your methods instead of just a sbyte*
eg.
You don't want your string to be disposed when its inside the C/C++ code
The technical explanation is that the handle won't be GCed if its still in the same stack frame that you're invoking the bindings (Example can be seen here)
C# strings are stored behind the scenes as UTF16 so we convert to UTF8
Huh interesting.
Going to come back to this later