Giorgi
05/10/2023, 4:26 PM#include <iostream>
#include <windows.h>
#include <Commdlg.h>
//
// Gobal Variables and declarations.
//
OPENFILENAME ofn ;
// a another memory buffer to contain the file name
char szFile[100] ;
int main() {
ZeroMemory( &ofn , sizeof( ofn));
ofn.lStructSize = sizeof ( ofn );
ofn.hwndOwner = NULL ;
ofn.lpstrFile = szFile ;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof( szFile );
ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex =1;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = 0 ;
ofn.lpstrInitialDir=NULL ;
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;
GetOpenFileName( &ofn );
// Now simpley display the file name
MessageBox ( NULL , ofn.lpstrFile , "File Name" , MB_OK);
return 0;
}
Currently, Im strugling with creating OPENFILENAME object. It is alias of tagOFNA, and it takes rawPtr: kotlinx.cinterop.NativePtr
in its constructor. How do I create NativePtr
?fun main(args: Array<String>) {
memScoped {
val result = alloc<tagOFNA>()
SecureZeroMemory?.invoke(result.ptr, sizeOf<tagOFNA>().toULong())
val szFile = ByteArray(100)
alloc<CHARVar>()
result.apply {
hwndOwner = null
lpstrFile = szFile
}
GetOpenFileNameA(result.ptr)
}
}
Now I need help with creating _char_ szFile[100] ;
and its pointer for ofn.lpstrFile
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\0*.*\0Text\0*.TXT\0".encodeToByteArray().pin()
result.apply {
hwndOwner = null
lpstrFile = pinnedSzFile.addressOf(0)
// nMaxFile = pinnedSzFile
// ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
lpstrFilter = filter.addressOf(0)
nFilterIndex = 1u;
lpstrFileTitle = null
nMaxFileTitle = 0u
lpstrInitialDir = null
Flags = (OFN_PATHMUSTEXIST or OFN_FILEMUSTEXIST).toUInt()
}
GetOpenFileNameA(result.ptr)
}
}
Need help with _val_ filter = "All\0*.*\0Text\0*.TXT\0".encodeToByteArray().pin()
it says illegal escape \0
import kotlinx.cinterop.*
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'