https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
n

Nathan Bedell

11/14/2023, 7:22 PM
Hey all. I am just starting out with Kotlin native, and my goal is to (eventually) be able to re-use some Kotlin code I have in a .net application, building my Kotlin library as a native dll, and calling that from my C# application. So far I have been able to call exposed Kotlin functions with primitive arguments (with
@CName
) pretty easily, but I'm having trouble running callbacks from C# for some reason. The exception I get (which I found by debugging seems to happen when my Kotlin function tries to call it's callback argument) is: "Access violation reading location 0xFFFFFFFFFFFFFFFF", which I think is a null-pointer, and yet what I pass to the function from C# definitely isn't null, and isn't getting GC'd, so I'm not sure what's going on. Does anyone here have any insights?
Actually I seem to be having problems calling my Kotlin dll from C alone (when trying to do a callback) as well.
Is the
cinterop
library currently a requirement in order to pass in a callback into a Kotlin native function or something?
I am also not sure how to access the classes like
CPointer
and
CFunction
from my multiplatform project. The documentation points me towards a command line tool to generate C bindings for use in Kotlin, but I just want to allow C libraries to be able to pass callbacks into my Kotlin functions.
Nevermind, I figured it out. I didn't have access to those classes because my code was in commonMain. Seems to be working well now.
👌 1
e

edrd

11/16/2023, 10:16 PM
@Nathan Bedell just curious: you’re being able to call K/N from C#, but not the opposite, right? (sorry if it’s a dumb question, I know almost nothing of C#)
n

Nathan Bedell

11/16/2023, 10:21 PM
Right, the problem I was having was being able to call a C# callback inside of a K/N function, the other way around worked. But now I am able to do it either way (with a callback from the C# side). I just had to use CPointer<CFunction<...>> for my callback.
e

edrd

11/16/2023, 10:26 PM
Interesting. Didn’t think it was possible to call C# code, since it runs on a VM
n

Nathan Bedell

11/16/2023, 11:49 PM
I don't understand all the details, but essentially C# has notions of "managed" (i.e. managed by the GC) and "unmanaged" code, so it's possible to go a bit lower level than Java for instance. But basically to "call" C# from K/N I'm basically relying on some compiler directives to handle the marshalling to the K/N C API (basically I say -- use the C calling convention to export this C# type). So the calling C# code is a bit indirect, via FFI.
e

edrd

11/20/2023, 3:17 PM
Got it. I’m thinking of writing some game mods using K/N + C# and that helps me a lot. Thanks!
n

Nathan Bedell

11/21/2023, 1:30 PM
That's really cool! What sort of games are you thinking of modding? I'd definitely love to stay posted on your progress if you've got a GitHub.
e

edrd

11/21/2023, 3:02 PM
Cool! I want to start modding Valheim. Will message you once I get a repo for it 🙂 (it may take a long time, though)
2 Views