Windows.h revision 263508
1271294Sngie//===- Win32/Win32.h - Common Win32 Include File ----------------*- C++ -*-===//
2271294Sngie//
3271294Sngie//                     The LLVM Compiler Infrastructure
4271294Sngie//
5271294Sngie// This file is distributed under the University of Illinois Open Source
6271294Sngie// License. See LICENSE.TXT for details.
7271294Sngie//
8271294Sngie//===----------------------------------------------------------------------===//
9271294Sngie//
10271294Sngie// This file defines things specific to Win32 implementations.
11271294Sngie//
12271294Sngie//===----------------------------------------------------------------------===//
13271294Sngie
14271294Sngie//===----------------------------------------------------------------------===//
15271294Sngie//=== WARNING: Implementation here must contain only generic Win32 code that
16271294Sngie//===          is guaranteed to work on *all* Win32 variants.
17271294Sngie//===----------------------------------------------------------------------===//
18271294Sngie
19271294Sngie// mingw-w64 tends to define it as 0x0502 in its headers.
20271294Sngie#undef _WIN32_WINNT
21271294Sngie
22271294Sngie// Require at least Windows XP(5.1) API.
23#define _WIN32_WINNT 0x0501
24#define _WIN32_IE    0x0600 // MinGW at it again.
25#define WIN32_LEAN_AND_MEAN
26
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/Config/config.h" // Get build system configuration settings
30#include "llvm/Support/Compiler.h"
31#include "llvm/Support/system_error.h"
32#include <windows.h>
33#include <wincrypt.h>
34#include <cassert>
35#include <string>
36#include <vector>
37
38inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
39  if (!ErrMsg)
40    return true;
41  char *buffer = NULL;
42  DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
43                          FORMAT_MESSAGE_FROM_SYSTEM,
44                          NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
45  if (R)
46    *ErrMsg = prefix + buffer;
47  else
48    *ErrMsg = prefix + "Unknown error";
49
50  LocalFree(buffer);
51  return R != 0;
52}
53
54template <typename HandleTraits>
55class ScopedHandle {
56  typedef typename HandleTraits::handle_type handle_type;
57  handle_type Handle;
58
59  ScopedHandle(const ScopedHandle &other); // = delete;
60  void operator=(const ScopedHandle &other); // = delete;
61public:
62  ScopedHandle()
63    : Handle(HandleTraits::GetInvalid()) {}
64
65  explicit ScopedHandle(handle_type h)
66    : Handle(h) {}
67
68  ~ScopedHandle() {
69    if (HandleTraits::IsValid(Handle))
70      HandleTraits::Close(Handle);
71  }
72
73  handle_type take() {
74    handle_type t = Handle;
75    Handle = HandleTraits::GetInvalid();
76    return t;
77  }
78
79  ScopedHandle &operator=(handle_type h) {
80    if (HandleTraits::IsValid(Handle))
81      HandleTraits::Close(Handle);
82    Handle = h;
83    return *this;
84  }
85
86  // True if Handle is valid.
87  LLVM_EXPLICIT operator bool() const {
88    return HandleTraits::IsValid(Handle) ? true : false;
89  }
90
91  operator handle_type() const {
92    return Handle;
93  }
94};
95
96struct CommonHandleTraits {
97  typedef HANDLE handle_type;
98
99  static handle_type GetInvalid() {
100    return INVALID_HANDLE_VALUE;
101  }
102
103  static void Close(handle_type h) {
104    ::CloseHandle(h);
105  }
106
107  static bool IsValid(handle_type h) {
108    return h != GetInvalid();
109  }
110};
111
112struct JobHandleTraits : CommonHandleTraits {
113  static handle_type GetInvalid() {
114    return NULL;
115  }
116};
117
118struct CryptContextTraits : CommonHandleTraits {
119  typedef HCRYPTPROV handle_type;
120
121  static handle_type GetInvalid() {
122    return 0;
123  }
124
125  static void Close(handle_type h) {
126    ::CryptReleaseContext(h, 0);
127  }
128
129  static bool IsValid(handle_type h) {
130    return h != GetInvalid();
131  }
132};
133
134struct FindHandleTraits : CommonHandleTraits {
135  static void Close(handle_type h) {
136    ::FindClose(h);
137  }
138};
139
140struct FileHandleTraits : CommonHandleTraits {};
141
142typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
143typedef ScopedHandle<FileHandleTraits>   ScopedFileHandle;
144typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
145typedef ScopedHandle<FindHandleTraits>   ScopedFindHandle;
146typedef ScopedHandle<JobHandleTraits>    ScopedJobHandle;
147
148namespace llvm {
149template <class T>
150class SmallVectorImpl;
151
152template <class T>
153typename SmallVectorImpl<T>::const_pointer
154c_str(SmallVectorImpl<T> &str) {
155  str.push_back(0);
156  str.pop_back();
157  return str.data();
158}
159
160namespace sys {
161namespace windows {
162error_code UTF8ToUTF16(StringRef utf8,
163                       SmallVectorImpl<wchar_t> &utf16);
164error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
165                       SmallVectorImpl<char> &utf8);
166} // end namespace windows
167} // end namespace sys
168} // end namespace llvm.
169