Hi guys, I am trying to return to a C++ program a...
# kotlin-native
v
Hi guys, I am trying to return to a C++ program a struct pointer allocated in Kotlin Native. The struct:
Copy code
typedef struct {
    uint8_t a;
} testStructDto;
is translated in Kotlin as follows:
Copy code
@kotlinx.cinterop.internal.CStruct public final class testStructDto public constructor(rawPtr: kotlinx.cinterop.NativePtr /* = kotlin.native.internal.NativePtr */) : kotlinx.cinterop.CStructVar {
    public companion object : kotlinx.cinterop.CStructVar.Type {
    }

    public final var a: liboverlay.uint8_t /* = kotlin.UByte */ /* compiled code */
}
My Kotlin native function:
Copy code
@ExperimentalUnsignedTypes
	override fun init(): CPointer<testStructDto> {
		val dtoVar = nativeHeap.alloc<testStructDto>()
		dtoVar.a = 0xa5.toUByte()

		return dtoVar.ptr
	}
I can use it in Kotlin like this:
Copy code
@Test
	fun `initialize`() {
		val context = controller.init()

		assertEquals(0xa5.toUByte(), context.pointed.a)
	}
But when I try to use it in C like this:
Copy code
libtest_ExportedSymbols *libPtr = libtest_symbols()
libtest_kref_MyObjectFactory kFactory = libPtr->kotlin.root.MyObjectFactory._instance();
libtest_kref_MyObject kObject = libPtr->kotlin.MyObjectFactory.create(kFactory);
testStructDto *dto = (testStructDto *) libPtr->kotlin.root.MyObject.init(kObject);
I always get
Copy code
dto->a = 32
I get the value
32
even if I assign something else than
0xa5
. Does anybody know what I am doing wrong? Thanks in advance!
a
Hello, @Vlad Balan! I tried to reproduce this behavior, but was unable to do it. Can you expose a little bit more of your Kotlin function here? I keep getting 165 as output 😔
v
Hi @Artyom Degtyarev [JB]! In the description I wrote C program. I made a simple project and it works from C. What I am actually trying to is use it from C++. This does not work. I will send you a small code snippet: Kotlin-Native:
Copy code
package sample

import kotlinx.cinterop.*
import liboverlay.TestStruct

@ExperimentalUnsignedTypes
fun initStruct(): CPointer<TestStruct> {
    return nativeHeap.alloc<TestStruct>() {
        field1 = 0xa5.toUByte()
        field2 = 0xb6.toUShort()
    }.ptr
}

fun releaseStruct(struct: CPointer<TestStruct>) {
    nativeHeap.free(struct.rawValue)
}

fun main(args: Array<String>) {
    val struct = initStruct()

    println("size: ${cValue<TestStruct>().size}")
    println("struct.field1: ${struct.pointed.field1}")
    println("struct.field2: ${struct.pointed.field2}")

    releaseStruct(struct)
}
Calling it from C like this:
Copy code
#include <stdint.h>
#include <stdio.h>

#include "libtryout_api.h"

typedef struct {
    uint8_t field1;
    uint16_t field2;
} TestStruct;

int main(int argc, char **argv) {

    libtryout_ExportedSymbols *lib = libtryout_symbols();

    void *voidPtr = lib->kotlin.root.sample.initStruct();
    printf("voidPtr %p\n", voidPtr);

    TestStruct *testStructPtr = voidPtr;
    printf("testStructPtr %p\n", voidPtr);

    printf("testStructPtr->field1 %d\n", testStructPtr->field1);
    printf("testStructPtr->field2 %d\n", testStructPtr->field2);

    lib->kotlin.root.sample.releaseStruct(voidPtr);

    return 0;
}
works. But if I try to use the code from C++ it does not work.
It also works with this C++ code:
Copy code
#include<iostream>

using namespace std;

#include <stdint.h>
#include <stdio.h>

#include "libtryout_api.h"

typedef struct {
    uint8_t field1;
    uint16_t field2;
} TestStruct;

int main(int argc, char **argv) {

    libtryout_ExportedSymbols *lib = libtryout_symbols();

    void *voidPtr = lib->kotlin.root.sample.initStruct();
    printf("voidPtr %p\n", voidPtr);

    TestStruct *testStructPtr = (TestStruct *) voidPtr;
    printf("testStructPtr %p\n", voidPtr);

    printf("testStructPtr->field1 %d\n", testStructPtr->field1);
    printf("testStructPtr->field2 %d\n", testStructPtr->field2);

    lib->kotlin.root.sample.releaseStruct(voidPtr);

    return 0;
}
I am using it in a more complex environment and it seems that I have another problem. Thanks for the help. Sorry to bother you.
🙂 1