aboutsummaryrefslogtreecommitdiff
path: root/libbutl/win32-utility.cxx
blob: c69842b71db6bfe7d4cd5895f0b46f0525836a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// file      : libbutl/win32-utility.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbutl/win32-utility.hxx>

// Note that while we exclude it from the buildfile-based builds, we still
// need these guards for build2 bootstrap.
//
#ifdef _WIN32

#include <memory> // unique_ptr

#include <libbutl/utility.hxx> // throw_system_error()

using namespace std;

namespace butl
{
  namespace win32
  {
    const nullhandle_t nullhandle (INVALID_HANDLE_VALUE);

    void auto_handle::
    close ()
    {
      if (handle_ != INVALID_HANDLE_VALUE)
      {
        bool r (CloseHandle (handle_));

        // If CloseHandle() failed then no reason to expect it to succeed the
        // next time.
        //
        handle_ = INVALID_HANDLE_VALUE;

        if (!r)
          throw_system_error (GetLastError ());
      }
    }

    struct msg_deleter
    {
      void operator() (char* p) const {LocalFree (p);}
    };

    string
    error_msg (DWORD code)
    {
      char* msg;
      if (!FormatMessageA (
            FORMAT_MESSAGE_ALLOCATE_BUFFER |
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_IGNORE_INSERTS |
            FORMAT_MESSAGE_MAX_WIDTH_MASK,
            0,
            code,
            MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
            (char*)&msg,
            0,
            0))
        return "unknown error code " + std::to_string (code);

      unique_ptr<char, msg_deleter> m (msg);
      return msg;
    }

    string
    last_error_msg ()
    {
      return error_msg (GetLastError ());
    }
  }
}

#endif // _WIN32