1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file contains defines and typedefs that allow popular Windows types to
6 // be used without the overhead of including windows.h.
7
8 #ifndef BASE_WIN_WINDOWS_TYPES_H_
9 #define BASE_WIN_WINDOWS_TYPES_H_
10
11 // Needed for function prototypes.
12 #include <concurrencysal.h>
13 #include <sal.h>
14 #include <specstrings.h>
15
16 #include "base/win/win_handle_types.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 // typedef and define the most commonly used Windows integer types.
23
24 typedef unsigned long DWORD; // NOLINT(runtime/int)
25 typedef long LONG; // NOLINT(runtime/int)
26 typedef __int64 LONGLONG;
27 typedef unsigned __int64 ULONGLONG;
28
29 #define VOID void
30 typedef char CHAR;
31 typedef short SHORT; // NOLINT(runtime/int)
32 typedef long LONG; // NOLINT(runtime/int)
33 typedef int INT;
34 typedef unsigned int UINT;
35 typedef unsigned int* PUINT;
36 typedef unsigned __int64 UINT64;
37 typedef void* LPVOID;
38 typedef void* PVOID;
39 typedef void* HANDLE;
40 typedef int BOOL;
41 typedef unsigned char BYTE;
42 typedef BYTE BOOLEAN;
43 typedef DWORD ULONG;
44 typedef unsigned short WORD; // NOLINT(runtime/int)
45 typedef WORD UWORD;
46 typedef WORD ATOM;
47
48 #if defined(_WIN64)
49 typedef __int64 INT_PTR, *PINT_PTR;
50 typedef unsigned __int64 UINT_PTR, *PUINT_PTR;
51
52 typedef __int64 LONG_PTR, *PLONG_PTR;
53 typedef unsigned __int64 ULONG_PTR, *PULONG_PTR;
54 #else
55 typedef __w64 int INT_PTR, *PINT_PTR;
56 typedef __w64 unsigned int UINT_PTR, *PUINT_PTR;
57
58 typedef __w64 long LONG_PTR, *PLONG_PTR; // NOLINT(runtime/int)
59 typedef __w64 unsigned long ULONG_PTR, *PULONG_PTR; // NOLINT(runtime/int)
60 #endif
61
62 typedef UINT_PTR WPARAM;
63 typedef LONG_PTR LPARAM;
64 typedef LONG_PTR LRESULT;
65 #define LRESULT LONG_PTR
66 typedef _Return_type_success_(return >= 0) long HRESULT; // NOLINT(runtime/int)
67
68 typedef ULONG_PTR SIZE_T, *PSIZE_T;
69 typedef LONG_PTR SSIZE_T, *PSSIZE_T;
70
71 typedef DWORD ACCESS_MASK;
72 typedef ACCESS_MASK REGSAM;
73
74 typedef LONG NTSTATUS;
75
76 // As defined in guiddef.h.
77 #ifndef _REFGUID_DEFINED
78 #define _REFGUID_DEFINED
79 #define REFGUID const GUID&
80 #endif
81
82 typedef LPVOID HINTERNET;
83 typedef HICON HCURSOR;
84 typedef HINSTANCE HMODULE;
85 typedef PVOID LSA_HANDLE;
86 typedef PVOID HDEVINFO;
87
88 // Forward declare some Windows struct/typedef sets.
89
90 typedef struct _OVERLAPPED OVERLAPPED;
91 typedef struct tagMSG MSG, *PMSG, *NPMSG, *LPMSG;
92 typedef struct tagTOUCHINPUT TOUCHINPUT;
93 typedef struct tagPOINTER_INFO POINTER_INFO;
94
95 typedef struct _RTL_SRWLOCK RTL_SRWLOCK;
96 typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
97
98 typedef struct _GUID GUID;
99 typedef GUID CLSID;
100 typedef GUID IID;
101
102 typedef struct tagLOGFONTW LOGFONTW, *PLOGFONTW, *NPLOGFONTW, *LPLOGFONTW;
103 typedef LOGFONTW LOGFONT;
104
105 typedef struct _FILETIME FILETIME;
106
107 typedef struct tagMENUITEMINFOW MENUITEMINFOW, MENUITEMINFO;
108
109 typedef struct tagNMHDR NMHDR;
110
111 typedef struct _SP_DEVINFO_DATA SP_DEVINFO_DATA;
112
113 typedef PVOID PSID;
114 typedef PVOID PSECURITY_DESCRIPTOR;
115 typedef DWORD SECURITY_INFORMATION;
116
117 typedef HANDLE HLOCAL;
118
119 typedef /* [wire_marshal] */ WORD CLIPFORMAT;
120 typedef struct tagDVTARGETDEVICE DVTARGETDEVICE;
121
122 typedef struct tagFORMATETC FORMATETC;
123
124 // Use WIN32_FIND_DATAW when you just need a forward declaration. Use
125 // CHROME_WIN32_FIND_DATA when you need a concrete declaration to reserve
126 // space.
127 typedef struct _WIN32_FIND_DATAW WIN32_FIND_DATAW;
128 typedef WIN32_FIND_DATAW WIN32_FIND_DATA;
129
130 typedef UINT_PTR SOCKET;
131 typedef struct _PROCESS_INFORMATION PROCESS_INFORMATION;
132 typedef struct _SECURITY_CAPABILITIES SECURITY_CAPABILITIES;
133 typedef struct _ACL ACL;
134 typedef struct _SECURITY_DESCRIPTOR SECURITY_DESCRIPTOR;
135 typedef struct _GENERIC_MAPPING GENERIC_MAPPING;
136
137 // Declare Chrome versions of some Windows structures. These are needed for
138 // when we need a concrete type but don't want to pull in Windows.h. We can't
139 // declare the Windows types so we declare our types and cast to the Windows
140 // types in a few places. The sizes must match the Windows types so we verify
141 // that with static asserts in win_includes_unittest.cc.
142 // ChromeToWindowsType functions are provided for pointer conversions.
143
144 struct CHROME_SRWLOCK {
145 PVOID Ptr;
146 };
147
148 struct CHROME_CONDITION_VARIABLE {
149 PVOID Ptr;
150 };
151
152 struct CHROME_LUID {
153 DWORD LowPart;
154 LONG HighPart;
155
156 bool operator==(CHROME_LUID const& that) const {
157 return this->LowPart == that.LowPart && this->HighPart == that.HighPart;
158 }
159
160 bool operator!=(CHROME_LUID const& that) const { return !(*this == that); }
161 };
162
163 // _WIN32_FIND_DATAW is 592 bytes and the largest built-in type in it is a
164 // DWORD. The buffer declaration guarantees the correct size and alignment.
165 struct CHROME_WIN32_FIND_DATA {
166 DWORD buffer[592 / sizeof(DWORD)];
167 };
168
169 struct CHROME_FORMATETC {
170 CLIPFORMAT cfFormat;
171 /* [unique] */ DVTARGETDEVICE* ptd;
172 DWORD dwAspect;
173 LONG lindex;
174 DWORD tymed;
175 };
176
177 struct CHROME_POINT {
178 LONG x;
179 LONG y;
180 };
181
182 struct CHROME_MSG {
183 HWND hwnd;
184 UINT message;
185 WPARAM wParam;
186 LPARAM lParam;
187 DWORD time;
188 CHROME_POINT pt;
189 };
190
191 // Define some commonly used Windows constants. Note that the layout of these
192 // macros - including internal spacing - must be 100% consistent with windows.h.
193
194 // clang-format off
195
196 #ifndef INVALID_HANDLE_VALUE
197 // Work around there being two slightly different definitions in the SDK.
198 #define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)
199 #endif
200 #define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
201 #define HTNOWHERE 0
202 #define MAX_PATH 260
203 #define CS_GLOBALCLASS 0x4000
204
205 #define ERROR_SUCCESS 0L
206 #define ERROR_FILE_NOT_FOUND 2L
207 #define ERROR_ACCESS_DENIED 5L
208 #define ERROR_INVALID_HANDLE 6L
209 #define ERROR_SHARING_VIOLATION 32L
210 #define ERROR_LOCK_VIOLATION 33L
211 #define ERROR_MORE_DATA 234L
212 #define REG_BINARY ( 3ul )
213 #define REG_NONE ( 0ul )
214
215 #ifndef STATUS_PENDING
216 // Allow people to include ntstatus.h
217 #define STATUS_PENDING ((DWORD )0x00000103L)
218 #endif // STATUS_PENDING
219 #define STILL_ACTIVE STATUS_PENDING
220 #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
221 #define FAILED(hr) (((HRESULT)(hr)) < 0)
222
223 #define HKEY_CLASSES_ROOT (( HKEY ) (ULONG_PTR)((LONG)0x80000000) )
224 #define HKEY_LOCAL_MACHINE (( HKEY ) (ULONG_PTR)((LONG)0x80000002) )
225 #define HKEY_CURRENT_USER (( HKEY ) (ULONG_PTR)((LONG)0x80000001) )
226 #define KEY_QUERY_VALUE (0x0001)
227 #define KEY_SET_VALUE (0x0002)
228 #define KEY_CREATE_SUB_KEY (0x0004)
229 #define KEY_ENUMERATE_SUB_KEYS (0x0008)
230 #define KEY_NOTIFY (0x0010)
231 #define KEY_CREATE_LINK (0x0020)
232 #define KEY_WOW64_32KEY (0x0200)
233 #define KEY_WOW64_64KEY (0x0100)
234 #define KEY_WOW64_RES (0x0300)
235
236 #define PROCESS_QUERY_INFORMATION (0x0400)
237 #define READ_CONTROL (0x00020000L)
238 #define SYNCHRONIZE (0x00100000L)
239
240 #define STANDARD_RIGHTS_READ (READ_CONTROL)
241 #define STANDARD_RIGHTS_WRITE (READ_CONTROL)
242 #define STANDARD_RIGHTS_ALL (0x001F0000L)
243
244 #define KEY_READ ((STANDARD_RIGHTS_READ |\
245 KEY_QUERY_VALUE |\
246 KEY_ENUMERATE_SUB_KEYS |\
247 KEY_NOTIFY) \
248 & \
249 (~SYNCHRONIZE))
250
251
252 #define KEY_WRITE ((STANDARD_RIGHTS_WRITE |\
253 KEY_SET_VALUE |\
254 KEY_CREATE_SUB_KEY) \
255 & \
256 (~SYNCHRONIZE))
257
258 #define KEY_ALL_ACCESS ((STANDARD_RIGHTS_ALL |\
259 KEY_QUERY_VALUE |\
260 KEY_SET_VALUE |\
261 KEY_CREATE_SUB_KEY |\
262 KEY_ENUMERATE_SUB_KEYS |\
263 KEY_NOTIFY |\
264 KEY_CREATE_LINK) \
265 & \
266 (~SYNCHRONIZE))
267
268 // The trailing white-spaces after this macro are required, for compatibility
269 // with the definition in winnt.h.
270 #define RTL_SRWLOCK_INIT {0} // NOLINT
271 #define SRWLOCK_INIT RTL_SRWLOCK_INIT
272
273 // clang-format on
274
275 // Define some macros needed when prototyping Windows functions.
276
277 #define DECLSPEC_IMPORT __declspec(dllimport)
278 #define WINBASEAPI DECLSPEC_IMPORT
279 #define WINUSERAPI DECLSPEC_IMPORT
280 #define WINAPI __stdcall
281 #define APIENTRY WINAPI
282 #define CALLBACK __stdcall
283
284 // Needed for LockImpl.
285 WINBASEAPI _Releases_exclusive_lock_(*SRWLock) VOID WINAPI
286 ReleaseSRWLockExclusive(_Inout_ PSRWLOCK SRWLock);
287 WINBASEAPI BOOLEAN WINAPI TryAcquireSRWLockExclusive(_Inout_ PSRWLOCK SRWLock);
288
289 // Needed to support protobuf's GetMessage macro magic.
290 WINUSERAPI BOOL WINAPI GetMessageW(_Out_ LPMSG lpMsg,
291 _In_opt_ HWND hWnd,
292 _In_ UINT wMsgFilterMin,
293 _In_ UINT wMsgFilterMax);
294
295 // Needed for thread_local_storage.h
296 WINBASEAPI LPVOID WINAPI TlsGetValue(_In_ DWORD dwTlsIndex);
297
298 WINBASEAPI BOOL WINAPI TlsSetValue(_In_ DWORD dwTlsIndex,
299 _In_opt_ LPVOID lpTlsValue);
300
301 // Needed for scoped_handle.h
302 WINBASEAPI _Check_return_ _Post_equals_last_error_ DWORD WINAPI
303 GetLastError(VOID);
304
305 WINBASEAPI VOID WINAPI SetLastError(_In_ DWORD dwErrCode);
306
307 WINBASEAPI BOOL WINAPI TerminateProcess(_In_ HANDLE hProcess,
308 _In_ UINT uExitCode);
309
310 // Support for a deleter for LocalAlloc memory.
311 WINBASEAPI HLOCAL WINAPI LocalFree(_In_ HLOCAL hMem);
312
313 #ifdef __cplusplus
314 }
315
316 // Helper functions for converting between Chrome and Windows native versions of
317 // type pointers.
318 // Overloaded functions must be declared outside of the extern "C" block.
319
ChromeToWindowsType(CHROME_WIN32_FIND_DATA * p)320 inline WIN32_FIND_DATA* ChromeToWindowsType(CHROME_WIN32_FIND_DATA* p) {
321 return reinterpret_cast<WIN32_FIND_DATA*>(p);
322 }
323
ChromeToWindowsType(const CHROME_WIN32_FIND_DATA * p)324 inline const WIN32_FIND_DATA* ChromeToWindowsType(
325 const CHROME_WIN32_FIND_DATA* p) {
326 return reinterpret_cast<const WIN32_FIND_DATA*>(p);
327 }
328
ChromeToWindowsType(CHROME_FORMATETC * p)329 inline FORMATETC* ChromeToWindowsType(CHROME_FORMATETC* p) {
330 return reinterpret_cast<FORMATETC*>(p);
331 }
332
ChromeToWindowsType(const CHROME_FORMATETC * p)333 inline const FORMATETC* ChromeToWindowsType(const CHROME_FORMATETC* p) {
334 return reinterpret_cast<const FORMATETC*>(p);
335 }
336
ChromeToWindowsType(CHROME_MSG * p)337 inline MSG* ChromeToWindowsType(CHROME_MSG* p) {
338 return reinterpret_cast<MSG*>(p);
339 }
340
341 #endif
342
343 // These macros are all defined by windows.h and are also used as the names of
344 // functions in the Chromium code base. Having these macros consistently defined
345 // or undefined can be critical to avoid mismatches between the functions
346 // defined and functions called. Macros need to be added to this list in those
347 // cases where it is easier to have the macro defined everywhere rather than
348 // undefined everywhere. As windows.h is removed from more source files we may
349 // be able to shorten this list.
350
351 #define CopyFile CopyFileW
352 #define CreateDirectory CreateDirectoryW
353 #define CreateFile CreateFileW
354 #define CreateService CreateServiceW
355 #define DeleteFile DeleteFileW
356 #define DispatchMessage DispatchMessageW
357 #define DrawText DrawTextW
358 #define FindFirstFile FindFirstFileW
359 #define FindNextFile FindNextFileW
360 #define GetClassName GetClassNameW
361 #define GetCurrentDirectory GetCurrentDirectoryW
362 #define GetCurrentTime() GetTickCount()
363 #define GetFileAttributes GetFileAttributesW
364 #define GetMessage GetMessageW
365 #define LoadIcon LoadIconW
366 #define PostMessage PostMessageW
367 #define ReplaceFile ReplaceFileW
368 #define SendMessage SendMessageW
369 #define SendMessageCallback SendMessageCallbackW
370 #define SetCurrentDirectory SetCurrentDirectoryW
371
372 #endif // BASE_WIN_WINDOWS_TYPES_H_
373