Kotlin/native C library callback function definition and return value

I’ve got C library converted with cinterop to be used with kotlin/native. It’s a GUI libraru and based on concept of callbacks.

In C I set callback using special function SetCallback passing reference to my callback function using
following type construction: (Icallback)exit_cb, where exit_cb function was defuned as int exit_cb(void).

While working in kotlin/native I can’t find the way how I could use same concept:

Icallback defined as:

typealias Icallback = CPointer<CFunction<(CPointer< Ihandle >?) → Int >>

where

Ihandle is:

typealias Ihandle = Ihandle _

and for Ihandle_

import cnames.structs.Ihandle_

Now coming back to my callback function. In C it was simple function returing type int value and getting
no parameters. But how I could use similar concept of type conversion i.e. (Icallback)exit_cb in C ?
I trien to force setting return type of my exit_cb() function definition in kotlin to Icallback? - but then I stuck how to convert integer return value to Icallback

I am new to kotlin and would appreciate if you could guide me in right direction.

Hi, @agb1!
If I understood you correctly, you’re trying to use the IUP library from the Kotlin code, am I right? If so, I would recommend you to try casting functions like this:

typealias Icallback = CPointer<CFunction<(CPointer< Ihandle >?) -> Int >>
typealias Ihandle = Ihandle_
val myExit_cb: CPointer<CFunction<() -> Int>> = staticCFunction(fun ():Int {
    return 5 //I've seen in some samples that this function can be that simple.
})
val castedExit_cb = myExit_cb.reinterpret<CFunction<(kotlinx.cinterop.CPointer<Ihandle>?) -> Int >>() as Icallback // using as mainly for clarification purposes, it can be omitted.

After that, the castedExit_cb function should contain something analog to C (lcallback)exit_cb.

Hello @Artyom.Degtyarev ! Yes - indeed I am looking for a way to use that GUI library that I used in the past in my C project with kotlin/native. Hope that I would be able to solve issuesd and would be able to prepare even some documention on it’s usage (probably together with CD & IM libraries from Tecgraf).
I am trying as well to promote IUP usage in Russia by working on documentation of this library translation to Russian. As well participated in IUP localization project.

With respect of your suggested solution I would try it shortly and would let you know regading results.

Thank you again !

Hi @Artyom.Degtyarev ! I would like to confirm that your suggestion working fine for basic callback functions used in IUP. Could you please advice - in case if callback function require some parameters passed to it - i.e. in most cases it would be something like (in C): int save_check(Ihandle ih); or more advanced version of callback function with two (or more !) parameters: int dropfiles_cb(Ihandle ih, const char filename);* - would it work the same way (or better to say - should it be implemented the same way as one you suggested - or there might be other solution in that case ?

Also I’ve noticed that for some reason I can’t use val and const val objects from C library header definition converted by cinterop even if I use import iup.win64. at start of my kotlin source file (where iup.win64 is my IUP library converted by cinterop)… Or should I redifine them in my code ?

I suppose that for more complex functions you can follow the same steps, specifying CFunction and anonymous function input types like CFunction<(Ihandle) -> Int> and fun(ih:Ihandle):Int.
I’m missing the second question a bit. You cannot access some of the global variables and objects, but can see them in the klib? Maybe the problem is in their full name adding one more sub-package, like iup.win64.<something>.<theObject>.