https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Andrei Marshalov

06/14/2019, 11:07 AM
Hello, i have a question about using cinterop in kotlin native. I need to pass kotlin string to C function with signature like this:
Copy code
someFunctionFromC(data: kotlinx.cinterop.CValuesRef<libmylib.GByteVar /* = kotlinx.cinterop.UByteVarOf<libmylib.GByte /* = kotlin.UByte */> */>?)
So, i tried to use ‘cstr’ how it described here (https://kotlinlang.org/docs/tutorials/native/mapping-strings-from-c.html):
Copy code
val kotlinString = "qwerty"
someFunctionFromC(kotlinString.cstr)
but i get type mismatch:
Copy code
Type mismatch:
Required : CValuesRef<GByteVar /* = UByteVarOf<GByte /* = UByte */> */>?
Found : CValues<ByteVar /* = ByteVarOf<Byte  /*>
So i need to pass in C function not
Copy code
const char* cstring
, but
Copy code
const unsigned char* cstring
Solution that i found (https://github.com/JetBrains/kotlin-native/blob/master/INTEROP.md):
Copy code
kotlinString.toUtf8().toUByteArray().usePinned { kString ->
    someFunctionFromC(kString.adressOf(0))
}
It work, but doesn’t look well. Maybe someone can suggest a better way?
m

msink

06/14/2019, 11:13 AM
kotlinString.cstr.convert()
dosn't work too?
a

Andrei Marshalov

06/14/2019, 11:29 AM
There is not such function:```Unresolved reference: convert```
o

olonho

06/14/2019, 12:10 PM
practically
kotlinString.cstr as CValuesRef<GByteVar>
may work
a

Andrei Marshalov

06/14/2019, 12:35 PM
I just tried this, it compiles, but it breaks somewhere insight the C-library