# Call dll generate from kotlin native in kotlin native

**URL:** <https://discuss.kotlinlang.org/t/call-dll-generate-from-kotlin-native-in-kotlin-native/12703>\
**Category:** Native\
**Created:** [May 12, 2019, 10:18am UTC](https://discuss.kotlinlang.org/t/call-dll-generate-from-kotlin-native-in-kotlin-native/12703 "2019-05-12T10:18:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryudith](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ryudith/32/9355_2.png) [@ryudith](https://discuss.kotlinlang.org/u/ryudith)\
**Post date:** [May 12, 2019, 10:18am UTC](https://discuss.kotlinlang.org/t/call-dll-generate-from-kotlin-native-in-kotlin-native/12703/1 "2019-05-12T10:18:57Z")

</div>

hi,

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

last night i try tutorial from [https://kotlinlang.org/docs/tutorials/native/dynamic-libraries.html](https://kotlinlang.org/docs/tutorials/native/dynamic-libraries.html) 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 :

> /src  
> /src/main  
> /src/main/kotlin  
> /src/main/kotlin/HelloDll.kt  
> /src/main/resource/  
> /src/main/resource/libnative\_symbols-build/  
> /src/main/resource/libnative.dll  
> /src/main/resource/libnative.lib  
> /src/main/resource/libnative\_api.h  
> /src/main/resource/libnative\_symbols.def  
> /src/main/resource/libnative\_symbols.dll  
> /src/main/resource/libnative\_symbols.klib  
> /src/main/resource/usedll.exe

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)
}

```

---

<div class="post-metadata">

**Author:** ![dickensas](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dickensas](https://discuss.kotlinlang.org/u/dickensas)\
**Post date:** [April 20, 2020, 5:25am UTC](https://discuss.kotlinlang.org/t/call-dll-generate-from-kotlin-native-in-kotlin-native/12703/2 "2020-04-20T05:25:14Z")

</div>

I am also looking for an answer to this

lib!!.pointed.kotlin.root.example.get\_globalString

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

---

<div class="post-metadata">

**Author:** ![dickensas](https://avatars.discourse-cdn.com/v4/letter/d/90ced4/32.png) [@dickensas](https://discuss.kotlinlang.org/u/dickensas)\
**Post date:** [April 20, 2020, 3:15pm UTC](https://discuss.kotlinlang.org/t/call-dll-generate-from-kotlin-native-in-kotlin-native/12703/3 "2020-04-20T15:15:42Z")

</div>

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())
}

```
