windows_types.h 11 KB

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