windowskotlinwinapinative

Initializing WinAPI struct COORD (Windows OS) with Kotlin/Native application?


I need to place the cursor at the beginning of the console window. This requires initialization of the platform.windows.COORD structure. How do I do this?

import kotlinx.cinterop.*
import platform.windows.*
import platform.posix.*

@kotlinx.cinterop.ExperimentalForeignApi
fun main()
{
    platform.windows.Sleep(333u);  // OK

    val h : HANDLE? = GetStdHandle(STD_OUTPUT_HANDLE); // OK

    val aaa = memScoped {

        val z: SHORT = 0

        var coord : kotlinx.cinterop.CValue<platform.windows.COORD> // ??? ERROR: Variable 'coord' must be initialized.

        platform.windows.SetConsoleCursorPosition(h, coord)
    }
}

Solution

  • You can create a CValue<T> using the cValue global function. e.g.

    var coord = cValue<COORD> {
        X = 10
        Y = 10
    }
    

    For more info, see this guide.