Skip to content

Instantly share code, notes, and snippets.

@shalva97
Created May 12, 2023 09:37
Show Gist options
  • Save shalva97/b435d027abbc1a815736f4ab0dec059e to your computer and use it in GitHub Desktop.
Save shalva97/b435d027abbc1a815736f4ab0dec059e to your computer and use it in GitHub Desktop.
Open file dialog on Windows with Kotlin Native
import kotlinx.cinterop.*
import platform.posix.IID
import platform.windows.*
import kotlin.native.concurrent.freeze
fun main(args: Array<String>) {
memScoped {
val result = alloc<tagOFNA>()
SecureZeroMemory?.invoke(result.ptr, sizeOf<tagOFNA>().toULong())
val szFile = ByteArray(100)
szFile[0] = 0
val pinnedSzFile = szFile.pin()
val filter = "All$NUL*.*${NUL}Text$NUL*.TXT$NUL".encodeToByteArray().pin()
result.apply {
lStructSize = sizeOf<tagOFNA>().toUInt()
hwndOwner = null
lpstrFile = pinnedSzFile.addressOf(0)
nMaxFile = szFile.size.toUInt()
lpstrFilter = filter.addressOf(0)
nFilterIndex = 1u;
lpstrFileTitle = null
nMaxFileTitle = 0u
lpstrInitialDir = null
Flags = (OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST).toUInt()
}
GetOpenFileNameA(result.ptr)
}
}
const val NUL = '\u0000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment