Trying to send back a jstring from kotlin native j...
# kotlin-native
j
Trying to send back a jstring from kotlin native jni-bridge, is there a way to create/convert a kotlin string to a jstring? Example I have now:
Copy code
@CName("Java_com_example_project_createString")
fun createString(env: kotlinx.cinterop.CPointer<JNIEnvVar>, instance: jobject):jstring?{
    val jniNativeInterface: JNINativeInterface = env.pointed.pointed!!
    val fNewStringUTF = jniNativeInterface.NewStringUTF!!
    val result = fNewStringUTF(env, "A test")
    return result
}
Where the 'A test' part wont work, because of it not being a jstring
I suppose doing:
Copy code
var jstringConverted = memScoped {
        return@memScoped "my string".cstr.ptr
    }
and then doing:
Copy code
val result = fNewStringUTF8(env, jstringConverted)
Takes me some of the way
but that brings utf8 issues back to jvm
s
because of it not being a jstring
This is not related.
I suppose doing:
Your string gets deallocated before
fNewStringUTF8
here. Try
Copy code
val result = memScoped {
    fNewStringUTF8(env, "my string".cstr.ptr)
}
🎉 1
j
Thanks dude, spot on
m
Why convert to UTF8 and back, doesn't work just
Copy code
val result = memScoped {
    NewString(env, "my string".utf16.ptr)
}