Calling Win32 APIs from an empty project

The docs told you to include <Windows.h> and nothing else.

Also, get some RAII goodness:

#define WIN32_LEAN_AND_MEAN 1
#define NOMINMAX
#include <Windows.h>

struct Win32FileHandleDeleter
{
    class pointer
    {
    public:
        pointer(HANDLE handle = INVALID_HANDLE_VALUE) : mHandle(handle) {}
        pointer(std::nullptr_t) noexcept {}
        explicit operator HANDLE() const noexcept { return mHandle; }
        explicit operator bool() const noexcept { return *this != nullptr; }
        bool operator==(const pointer&) const noexcept = default;
        bool operator!=(const pointer&) const noexcept = default;
    private:
        HANDLE mHandle = INVALID_HANDLE_VALUE;
    };

    void operator()(pointer p) const noexcept
    {
        if (p)
            CloseHandle(HANDLE(p));
    }
};

using Win32FileHandle = std::unique_ptr<void, Win32FileHandleDeleter>;

Win32FileHandle file(CreateFileW(L"foo.txt", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr));
/r/cpp_questions Thread Parent