Call dll generate from kotlin native in kotlin native


first sorry if i this question in wrong place and i’m beginner in kotlin.

last night i try tutorial from and it work perfectly. but then i try to call *.dll file from kotlin native it self but always get error or wrong output. can anyone tell me how to call method or property in that *dll file or point me where to find the answer ?

thank you.

here is my project directory layout :


here is my code that call method or property in .dll file :
import kotlinx.cinterop.

import platform.posix.*
import libnative_symbols.*

these is command i call to compile source :
cinterop -def libnative_symbols.def -compilerOpts -I./ -o libnative_symbols
kotlinc-native ..\kotlin -l libnative_symbols -linker-options libnative.lib -o usedll
fun main (args : Array<String>) {
	val lib : CPointer<libnative_ExportedSymbols>? = libnative_symbols()
	// val ref = libnative_ExportedSymbols(lib.rawValue);

	var str : String? = "This is how to use dll from kotlin native"
	var resp : CPointer<ByteVar>? = lib!!.pointed.kotlin.root.example.get_globalString!!.reinterpret()

	var path : CPointer<ByteVar>? = getenv("PATH")
	var pathStr : String = path?.toKString() ?: ""

	println("input : " + str)
	println("output : " + resp!!.toKString())
	println("path : " + pathStr)

I am also looking for an answer to this


get_globalString is function pointer, it is represented as COpaquePointer

If I need to access using pinned and memcopy how do I do it for a return value

I found a solution, declare a CFunction as typealias and use it in the reinterpret and use invoke to call that C function pointer
Below is the code

typealias SomeFun = CFunction<() -> CPointer<ByteVar>?>
fun main (args : Array<String>) {
 val lib : CPointer<libnative_ExportedSymbols>? = libnative_symbols()

 val fun1 = lib!!.pointed.kotlin.root.example.get_globalString!!.reinterpret<SomeFun>()

 println("output : " + fun1.invoke()!!.toKString())
1 Like