#include #include #include #include #include void explain (std::string what) { DWORD e = GetLastError (); std::cerr << what << ": "; DWORD r; std::shared_ptr buf; { LPSTR buf_; r = FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast (&buf_), 0, 0); buf.reset (buf_, LocalFree); } if (r) { char* end = buf.get () + std::strlen (buf.get ()); if (std::distance (buf.get (), end) >= 2 && *(end-2) == '\r' && *(end-1) == '\n') end -= 2; std::copy (buf.get (), end, std::ostream_iterator (std::cerr)); } else std::cerr << "?[" << GetLastError () << "]"; std::cerr << " (" << e << ")\n"; } int main () { using namespace boost::filesystem; path p = L"umbre☂☂a"; HANDLE fh = CreateFileW (p.c_str (), GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, 0); if (fh == INVALID_HANDLE_VALUE) explain ("CreateFileW"); else { if (!CloseHandle (fh)) { explain ("CloseHandle"); return 1; } } ofstream f (p, ofstream::out); if (!f) { explain ("ofstream"); return 1; } f << "blah blah\n"; f.close (); if (!f) { explain ("close"); return 1; } } // vim: ts=4 sw=4 noet