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
JoakimForslund
08/26/2019, 9:11 AM
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
JoakimForslund
08/26/2019, 9:11 AM
but that brings utf8 issues back to jvm
s
svyatoslav.scherbina
08/26/2019, 9:36 AM
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
JoakimForslund
08/26/2019, 9:45 AM
Thanks dude, spot on
m
msink
08/26/2019, 10:15 AM
Why convert to UTF8 and back, doesn't work just
Copy code
val result = memScoped {
NewString(env, "my string".utf16.ptr)
}