Created
May 12, 2023 09:37
-
-
Save shalva97/b435d027abbc1a815736f4ab0dec059e to your computer and use it in GitHub Desktop.
Open file dialog on Windows with Kotlin Native
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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