Ch11.14: Win32 File APIs

Overview

On Windows, user-mode applications talk to the kernel through kernel32.dll. The two entry points for opening files are:

fast_io hides this difference behind two types, mirroring the POSIX layer:

Choosing Between ANSI and Wide: win32_api_family

The Win32 layer exposes a traits type called ::fast_io::win32_api_family. It selects which variant of the API should be used on a given Windows target:

In practice you almost never need to touch win32_api_family directly: win32_file consults it automatically when you pass a filename.

Opening a File with win32_file


#include <fast_io.h>
#include <fast_io_device.h>

int main()
{
    using namespace ::fast_io::iomnp;

    // The constructor picks CreateFileW or CreateFileA through
    // ::fast_io::win32_api_family.
    ::fast_io::win32_file wf(u8"hello_win32.txt", ::fast_io::open_mode::out);

    println(wf, u8"Hello from win32_file!");

    ::std::size_t const n{42zu};
    println(wf, u8"The answer is: ", n);
    // CloseHandle is invoked automatically when `wf` goes out of scope.
}
      

Wrapping an Existing HANDLE

Just like posix_io_observer, you can build a non-owning view over a HANDLE you obtained elsewhere:


#include <fast_io.h>
#include <fast_io_device.h>

void write_via_handle(void* h)
{
    using namespace ::fast_io::iomnp;

    // Non-owning: does NOT call CloseHandle on destruction.
    ::fast_io::win32_io_observer observer{
        static_cast<::fast_io::win32_io_observer::native_handle_type>(h)};
    println(observer, u8"Written through a win32_io_observer\n");
}

int main()
{
    ::fast_io::win32_file wf(u8"legacy_handle.txt", ::fast_io::open_mode::out);
    write_via_handle(wf.native_handle());
}
      

Reading and Open-at

Reading uses scan. Opening relative to a directory handle uses NtCreateFile under the hood because the Win32 API itself has no *at equivalent:


#include <fast_io.h>
#include <fast_io_device.h>

int main()
{
    using namespace ::fast_io::iomnp;

    ::fast_io::win32_file inpf(u8"input.txt", ::fast_io::open_mode::in);
    ::std::size_t value{};
    ::fast_io::scan(inpf, value);

    ::fast_io::win32_file outpf(u8"output.txt", ::fast_io::open_mode::out);
    println(outpf, u8"Read the value: ", value);
}
      

Summary