Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GrauBlitz opened this issue Feb 20, 2024 · 0 comments
Open

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

GrauBlitz opened this issue Feb 20, 2024 · 0 comments

Comments

@GrauBlitz
Copy link

GrauBlitz commented Feb 20, 2024

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,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant