Hi,
thanks for all the help I got so far ... NIM is a treasure :)
Ok, now I am stuck at a CONVERSION from a WideCString string to a string in NIM. I know I have to use cast to link to the address of the CString, however ... I get errors ...
Here is my code with comments:
import winlean, nimpy
proc findFirstFile(a: string, b: var WIN32_FIND_DATA): Handle =
result = findFirstFileW(newWideCString(a), b)
proc findNextFileW*(hFindFile: Handle, lpFindFileData: var WIN32_FIND_DATA): int32
{.stdcall, dynlib: "kernel32", importc: "FindNextFileW", sideEffect.}
template getFilename(f: untyped): untyped =
$cast[WideCString](addr(f.cAlternateFileName[0]))
proc test*(file: string): WIN32_FIND_DATA {.exportpy.} =
var FileInfo: WIN32_FIND_DATA
var Handle = findFirstFile(file, FileInfo)
#[
tried THIS:
echo FileInfo.cAlternateFileName
==> ERROR MSG
Error: type mismatch: got <array[0..259, Utf16Char]>
but expected one of:
proc echo(x: varargs[typed, `$`])
first type mismatch at position: 1
required type for x: varargs[typed]
but expression 'FileInfo.cAlternateFileName' is of type: array[0..259, Utf16Char]
expression: echo FileInfo.cAlternateFileName
tried THIS:
echo getFilename(FileInfo)
==> ERROR MSG
Error: type mismatch: got <Utf16Char>
but expected one of:
proc unknownTypeCompileError()
first type mismatch at position: 1
extra argument given
expression: unknownTypeCompileError(v)
]#
result = FileInfo
You can use winim and winstr .
Example:
import winim/lean
var ffd: WIN32_FIND_DATA
var hFind = FindFirstFile("*.*", ffd)
if hFind != INVALID_HANDLE_VALUE:
defer:
FindClose(hFind)
while true:
block method1:
var buffer = T(MAX_PATH)
# buffer maybe wstring or mstring depends on -d:useWinAnsi
buffer << ffd.cFileName
# copy cFileName to buffer
buffer.nullTerminate
# set corrent length of buffer
echo buffer
block method2:
echo (%$ffd.cFileName).nullTerminated
# `$` for array cause ambiguous call since 0.2.0, use %$ instead
if FindNextFile(hFind, &ffd) == 0: break