Hi guys, I am trying to return to a C program a s...
# embedded-kotlin
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! Slack Conversation