Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

How to access a method - Kotlin Shared DLL using CPointer #4119

Closed
dickensas opened this issue Apr 19, 2020 · 3 comments
Closed

How to access a method - Kotlin Shared DLL using CPointer #4119

dickensas opened this issue Apr 19, 2020 · 3 comments

Comments

@dickensas
Copy link

dickensas commented Apr 19, 2020

Following this Documentation

https://kotlinlang.org/docs/tutorials/native/dynamic-libraries.html

This is a C code in that documentation

 libnative_ExportedSymbols* lib = libnative_symbols();
 lib->kotlin.root.example.forIntegers()

How to do the same in Kotlin

memScoped {
    val lib = alloc<libnative_ExportedSymbols>().apply {
         libnative_symbols()
    }
    println(lib.kotlin.root.example.forIntegers())
}

The lib.kotlin.root.example is perfect but after that how to proceed

I am getting this error

 Type mismatch: inferred type is COpaquePointer? /* = CPointer<out CPointed>? */ but CPointer<CFunction<() -> String>> was expected
@artdfel
Copy link
Contributor

artdfel commented Apr 20, 2020

Hello, @dickensas! First of all, I got to remind you about Kotlin/Native issue tracker migration(see #4079). This particular problem seems to be more likely a question rather than a bug, so maybe it could be asked on the #kotlin-native slack channel.
About the error itself. The void * construction from C is being mapped to the special COpaquePointer type(see details here). As the documentation says, such pointer should be explicitly cast to necessary type using .reinterpret() method. In this case, it should look somehow like this:

memScoped {
    val lib = alloc<libnative_ExportedSymbols>().apply {
         libnative_symbols()
    }
    println(lib.kotlin.root.example.forIntegers.reinterpret<<CFunction<() -> String>>().invoke())
}

Also, I just want to ask about reasons of choosing such uneasy approach. Using a .klib might be more relevant here, or I'm missing something?

@dickensas
Copy link
Author

I did definitely referred the code for libcurl and curl and understood in the past

The reason I am going for direct DLL is that I am trying create .pyd for python without a C stub

Documentation Reference - .dll -> .pyd

Regarding your code, I could not compile it in kotlin 1.3.72

Therefore I went for this code, Above main method I declared typealias

  typealias SomeFun = CFunction<() -> CPointer<ByteVar>?>
  ....
  ....
  val s = lib.kotlin.root.example.someLibraryMethod?.reinterpret<SomeFun>()    
  println(s?.invoke())

But I am getting null

This is the GitHub URL Kotlin DLL - to - Kotlin

@dickensas
Copy link
Author

This technique is working, therefore I close this issue

memScoped {
     val lib:CPointer<libmyext_ExportedSymbols>? = libmyext_symbols()
     println(lib!!.pointed.kotlin.root.example.getHello!!.reinterpret<SomeFun>()!!()!!.toKString())
}

Output

> Task :runDebugExecutableLibpymyext
That is from Kolin DLL

BUILD SUCCESSFUL in 9s

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants