marlersoft / zigwin32

Zig bindings for Win32 generated by https://github.com/marlersoft/zigwin32gen
MIT License
234 stars 30 forks source link

Can't compare handles and can't check for flags with bitwise AND #28

Open GrauBlitz opened 4 months ago

GrauBlitz commented 4 months ago

I am currently trying to transfer this MS WinAPI Example to Zig:

https://learn.microsoft.com/en-us/windows/win32/fileio/listing-the-files-in-a-directory

But I run into problems while comparing in my conditions because of some type mismatches.

Problem 1: Can't check for invalid file handle

const W = std.unicode.utf8ToUtf16LeStringLiteral;
const win = @import("win32/win32.zig");
const fs = win.storage.file_system;

pub const UNICODE: bool = true;

var ffd: fs.WIN32_FIND_DATAW = undefined;
// I also had to set the type manually to a better fitting "FindFileHandle" instead of the automatically recommended "isize"
const fileHandle: fs.FindFileHandle = fs.FindFirstFileW(W("C:/*"), &ffd);

if (fileHandle == win.foundation.INVALID_HANDLE_VALUE) {
    std.debug.print("Could not open directory", .{});
}

Error:

src\main.zig:19:20: error: incompatible types: 'isize' and '*anyopaque'
    if (fileHandle == win.foundation.INVALID_HANDLE_VALUE) {

Problem 2: Can't check for flags with bitwise AND &

var largeInt: win.foundation.LARGE_INTEGER = undefined;
while (true) {
//                u32        &    FILE_FLAGS_AND_ATTRIBUTES{ .FILE_ATTRIBUTE_DIRECTORY = 1 };
    if (ffd.dwFileAttributes & fs.FILE_ATTRIBUTE_DIRECTORY) {
        std.debug.print("[D] {s}", ffd.cFileName);
    } else {
        largeInt.u.LowPart = ffd.nFileSizeLow;
        largeInt.u.HighPart = ffd.nFileSizeHigh;
        std.debug.print("[F] {s} {i} bytes", ffd.cFileName, largeInt.QuadPart);
    }
    // Also: Why is there no do while loop in Zig?!
    fs.FindNextFileW(fileHandle, &ffd);
}

Error:

src\main.zig:24:38: error: incompatible types: 'u32' and 'win32.win32.storage.file_system.FILE_FLAGS_AND_ATTRIBUTES'
            if (ffd.dwFileAttributes & fs.FILE_ATTRIBUTE_DIRECTORY) {

Problem 3: FindNextFileW does use another handle type than FindFirstFileW returns

FindFirstFileW returns a FindFileHandle but FindNextFileW needs a generic HANDLE?

Error:

src\main.zig:31:30: error: expected type '?*anyopaque', found 'isize'
            fs.FindNextFileW(fileHandle, &ffd);
                             ^~~~~~~~~~
src\win32\win32\storage\file_system.zig:5504:16: note: parameter type declared here
    hFindFile: ?HANDLE,