1------------------------------------------------------------------------------
2--                                                                          --
3--                        GNAT RUN-TIME COMPONENTS                          --
4--                                                                          --
5--                         S Y S T E M . W I N 3 2                          --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2008-2020, Free Software Foundation, Inc.         --
10--                                                                          --
11-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12-- terms of the  GNU General Public License as published  by the Free Soft- --
13-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17--                                                                          --
18-- As a special exception under Section 7 of GPL version 3, you are granted --
19-- additional permissions described in the GCC Runtime Library Exception,   --
20-- version 3.1, as published by the Free Software Foundation.               --
21--                                                                          --
22-- You should have received a copy of the GNU General Public License and    --
23-- a copy of the GCC Runtime Library Exception along with this program;     --
24-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25-- <http://www.gnu.org/licenses/>.                                          --
26--                                                                          --
27-- GNAT was originally developed  by the GNAT team at  New York University. --
28-- Extensive contributions were provided by Ada Core Technologies Inc.      --
29--                                                                          --
30------------------------------------------------------------------------------
31
32--  This package plus its child provide the low level interface to the Win32
33--  API. The core part of the Win32 API (common to RTX and Win32) is in this
34--  package, and an additional part of the Win32 API which is not supported by
35--  RTX is in package System.Win32.Ext.
36
37with Interfaces.C;
38
39package System.Win32 is
40   pragma Pure;
41
42   -------------------
43   -- General Types --
44   -------------------
45
46   --  The LARGE_INTEGER type is actually a fixed point type
47   --  that only can represent integers. The reason for this is
48   --  easier conversion to Duration or other fixed point types.
49   --  (See System.OS_Primitives.Clock, mingw and rtx versions.)
50
51   type LARGE_INTEGER is delta 1.0 range -2.0**63 .. 2.0**63 - 1.0;
52
53   subtype PVOID is Address;
54
55   type HANDLE is new Interfaces.C.ptrdiff_t;
56
57   INVALID_HANDLE_VALUE : constant HANDLE := -1;
58   INVALID_FILE_SIZE    : constant := 16#FFFFFFFF#;
59
60   type ULONG     is new Interfaces.C.unsigned_long;
61   type DWORD     is new Interfaces.C.unsigned_long;
62   type WORD      is new Interfaces.C.unsigned_short;
63   type BYTE      is new Interfaces.C.unsigned_char;
64   type LONG      is new Interfaces.C.long;
65   type CHAR      is new Interfaces.C.char;
66   type SIZE_T    is new Interfaces.C.size_t;
67   type DWORD_PTR is mod 2 ** Standard'Address_Size;
68
69   type BOOL      is new Interfaces.C.int;
70   for BOOL'Size use Interfaces.C.int'Size;
71
72   type Bits1  is range 0 .. 2 ** 1 - 1;
73   type Bits2  is range 0 .. 2 ** 2 - 1;
74   type Bits17 is range 0 .. 2 ** 17 - 1;
75   for Bits1'Size  use 1;
76   for Bits2'Size  use 2;
77   for Bits17'Size use 17;
78
79   --  Note that the following clashes with standard names are to stay
80   --  compatible with the historical choice of following the C names.
81
82   pragma Warnings (Off);
83   FALSE : constant := 0;
84   TRUE  : constant := 1;
85   pragma Warnings (On);
86
87   function GetLastError return DWORD;
88   pragma Import (Stdcall, GetLastError, "GetLastError");
89
90   -----------
91   -- Files --
92   -----------
93
94   CP_UTF8                            : constant := 65001;
95   CP_ACP                             : constant := 0;
96
97   GENERIC_READ                       : constant := 16#80000000#;
98   GENERIC_WRITE                      : constant := 16#40000000#;
99
100   CREATE_NEW                         : constant := 1;
101   CREATE_ALWAYS                      : constant := 2;
102   OPEN_EXISTING                      : constant := 3;
103   OPEN_ALWAYS                        : constant := 4;
104   TRUNCATE_EXISTING                  : constant := 5;
105
106   FILE_SHARE_DELETE                  : constant := 16#00000004#;
107   FILE_SHARE_READ                    : constant := 16#00000001#;
108   FILE_SHARE_WRITE                   : constant := 16#00000002#;
109
110   FILE_BEGIN                         : constant := 0;
111   FILE_CURRENT                       : constant := 1;
112   FILE_END                           : constant := 2;
113
114   PAGE_NOACCESS                      : constant := 16#0001#;
115   PAGE_READONLY                      : constant := 16#0002#;
116   PAGE_READWRITE                     : constant := 16#0004#;
117   PAGE_WRITECOPY                     : constant := 16#0008#;
118   PAGE_EXECUTE                       : constant := 16#0010#;
119
120   FILE_MAP_ALL_ACCESS                : constant := 16#F001f#;
121   FILE_MAP_READ                      : constant := 4;
122   FILE_MAP_WRITE                     : constant := 2;
123   FILE_MAP_COPY                      : constant := 1;
124
125   FILE_ADD_FILE                      : constant := 16#0002#;
126   FILE_ADD_SUBDIRECTORY              : constant := 16#0004#;
127   FILE_APPEND_DATA                   : constant := 16#0004#;
128   FILE_CREATE_PIPE_INSTANCE          : constant := 16#0004#;
129   FILE_DELETE_CHILD                  : constant := 16#0040#;
130   FILE_EXECUTE                       : constant := 16#0020#;
131   FILE_LIST_DIRECTORY                : constant := 16#0001#;
132   FILE_READ_ATTRIBUTES               : constant := 16#0080#;
133   FILE_READ_DATA                     : constant := 16#0001#;
134   FILE_READ_EA                       : constant := 16#0008#;
135   FILE_TRAVERSE                      : constant := 16#0020#;
136   FILE_WRITE_ATTRIBUTES              : constant := 16#0100#;
137   FILE_WRITE_DATA                    : constant := 16#0002#;
138   FILE_WRITE_EA                      : constant := 16#0010#;
139   STANDARD_RIGHTS_READ               : constant := 16#20000#;
140   STANDARD_RIGHTS_WRITE              : constant := 16#20000#;
141   SYNCHRONIZE                        : constant := 16#100000#;
142
143   FILE_ATTRIBUTE_READONLY            : constant := 16#00000001#;
144   FILE_ATTRIBUTE_HIDDEN              : constant := 16#00000002#;
145   FILE_ATTRIBUTE_SYSTEM              : constant := 16#00000004#;
146   FILE_ATTRIBUTE_DIRECTORY           : constant := 16#00000010#;
147   FILE_ATTRIBUTE_ARCHIVE             : constant := 16#00000020#;
148   FILE_ATTRIBUTE_DEVICE              : constant := 16#00000040#;
149   FILE_ATTRIBUTE_NORMAL              : constant := 16#00000080#;
150   FILE_ATTRIBUTE_TEMPORARY           : constant := 16#00000100#;
151   FILE_ATTRIBUTE_SPARSE_FILE         : constant := 16#00000200#;
152   FILE_ATTRIBUTE_REPARSE_POINT       : constant := 16#00000400#;
153   FILE_ATTRIBUTE_COMPRESSED          : constant := 16#00000800#;
154   FILE_ATTRIBUTE_OFFLINE             : constant := 16#00001000#;
155   FILE_ATTRIBUTE_NOT_CONTENT_INDEXED : constant := 16#00002000#;
156   FILE_ATTRIBUTE_ENCRYPTED           : constant := 16#00004000#;
157   FILE_ATTRIBUTE_VALID_FLAGS         : constant := 16#00007fb7#;
158   FILE_ATTRIBUTE_VALID_SET_FLAGS     : constant := 16#000031a7#;
159
160   GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS : constant := 16#00000004#;
161
162   type OVERLAPPED is record
163      Internal     : access ULONG;
164      InternalHigh : access ULONG;
165      Offset       : DWORD;
166      OffsetHigh   : DWORD;
167      hEvent       : HANDLE;
168   end record;
169   pragma Convention (C_Pass_By_Copy, OVERLAPPED);
170
171   type SECURITY_ATTRIBUTES is record
172      nLength             : DWORD;
173      pSecurityDescriptor : PVOID;
174      bInheritHandle      : BOOL;
175   end record;
176   pragma Convention (C_Pass_By_Copy, SECURITY_ATTRIBUTES);
177
178   function CreateFileA
179     (lpFileName            : Address;
180      dwDesiredAccess       : DWORD;
181      dwShareMode           : DWORD;
182      lpSecurityAttributes  : access SECURITY_ATTRIBUTES;
183      dwCreationDisposition : DWORD;
184      dwFlagsAndAttributes  : DWORD;
185      hTemplateFile         : HANDLE) return HANDLE;
186   pragma Import (Stdcall, CreateFileA, "CreateFileA");
187
188   function CreateFile
189     (lpFileName            : Address;
190      dwDesiredAccess       : DWORD;
191      dwShareMode           : DWORD;
192      lpSecurityAttributes  : access SECURITY_ATTRIBUTES;
193      dwCreationDisposition : DWORD;
194      dwFlagsAndAttributes  : DWORD;
195      hTemplateFile         : HANDLE) return HANDLE;
196   pragma Import (Stdcall, CreateFile, "CreateFileW");
197
198   function GetFileSize
199     (hFile          : HANDLE;
200      lpFileSizeHigh : access DWORD) return BOOL;
201   pragma Import (Stdcall, GetFileSize, "GetFileSize");
202
203   function SetFilePointer
204     (hFile                : HANDLE;
205      lDistanceToMove      : LONG;
206      lpDistanceToMoveHigh : access LONG;
207      dwMoveMethod         : DWORD) return DWORD;
208   pragma Import (Stdcall, SetFilePointer, "SetFilePointer");
209
210   function WriteFile
211     (hFile                  : HANDLE;
212      lpBuffer               : Address;
213      nNumberOfBytesToWrite  : DWORD;
214      lpNumberOfBytesWritten : access DWORD;
215      lpOverlapped           : access OVERLAPPED) return BOOL;
216   pragma Import (Stdcall, WriteFile, "WriteFile");
217
218   function ReadFile
219     (hFile                : HANDLE;
220      lpBuffer             : Address;
221      nNumberOfBytesToRead : DWORD;
222      lpNumberOfBytesRead  : access DWORD;
223      lpOverlapped         : access OVERLAPPED) return BOOL;
224   pragma Import (Stdcall, ReadFile, "ReadFile");
225
226   function CloseHandle (hObject : HANDLE) return BOOL;
227   pragma Import (Stdcall, CloseHandle, "CloseHandle");
228
229   function CreateFileMapping
230     (hFile                : HANDLE;
231      lpSecurityAttributes : access SECURITY_ATTRIBUTES;
232      flProtect            : DWORD;
233      dwMaximumSizeHigh    : DWORD;
234      dwMaximumSizeLow     : DWORD;
235      lpName               : Address) return HANDLE;
236   pragma Import (Stdcall, CreateFileMapping, "CreateFileMappingA");
237
238   function MapViewOfFile
239     (hFileMappingObject   : HANDLE;
240      dwDesiredAccess      : DWORD;
241      dwFileOffsetHigh     : DWORD;
242      dwFileOffsetLow      : DWORD;
243      dwNumberOfBytesToMap : SIZE_T) return System.Address;
244   pragma Import (Stdcall, MapViewOfFile, "MapViewOfFile");
245
246   function UnmapViewOfFile (lpBaseAddress : System.Address) return BOOL;
247   pragma Import (Stdcall, UnmapViewOfFile, "UnmapViewOfFile");
248
249   function MultiByteToWideChar
250     (CodePage       : WORD;
251      dwFlags        : DWORD;
252      lpMultiByteStr : System.Address;
253      cchMultiByte   : WORD;
254      lpWideCharStr  : System.Address;
255      cchWideChar    : WORD) return WORD;
256   pragma Import (Stdcall, MultiByteToWideChar, "MultiByteToWideChar");
257
258   ------------------------
259   -- System Information --
260   ------------------------
261
262   subtype ProcessorId is DWORD;
263
264   type SYSTEM_INFO is record
265      dwOemId                     : DWORD;
266      dwPageSize                  : DWORD;
267      lpMinimumApplicationAddress : PVOID;
268      lpMaximumApplicationAddress : PVOID;
269      dwActiveProcessorMask       : DWORD_PTR;
270      dwNumberOfProcessors        : DWORD;
271      dwProcessorType             : DWORD;
272      dwAllocationGranularity     : DWORD;
273      dwReserved                  : DWORD;
274   end record;
275   pragma Convention (C_Pass_By_Copy, SYSTEM_INFO);
276
277   procedure GetSystemInfo (SI : access SYSTEM_INFO);
278   pragma Import (Stdcall, GetSystemInfo, "GetSystemInfo");
279
280   ---------------------
281   -- Time Management --
282   ---------------------
283
284   type SYSTEMTIME is record
285      wYear         : WORD;
286      wMonth        : WORD;
287      wDayOfWeek    : WORD;
288      wDay          : WORD;
289      wHour         : WORD;
290      wMinute       : WORD;
291      wSecond       : WORD;
292      wMilliseconds : WORD;
293   end record;
294   pragma Convention (C_Pass_By_Copy, SYSTEMTIME);
295
296   procedure GetSystemTime (pSystemTime : access SYSTEMTIME);
297   pragma Import (Stdcall, GetSystemTime, "GetSystemTime");
298
299   procedure GetSystemTimeAsFileTime (lpFileTime : access Long_Long_Integer);
300   pragma Import (Stdcall, GetSystemTimeAsFileTime, "GetSystemTimeAsFileTime");
301
302   function FileTimeToSystemTime
303     (lpFileTime   : access Long_Long_Integer;
304      lpSystemTime : access SYSTEMTIME) return BOOL;
305   pragma Import (Stdcall, FileTimeToSystemTime, "FileTimeToSystemTime");
306
307   function SystemTimeToFileTime
308     (lpSystemTime : access SYSTEMTIME;
309      lpFileTime   : access Long_Long_Integer) return BOOL;
310   pragma Import (Stdcall, SystemTimeToFileTime, "SystemTimeToFileTime");
311
312   function FileTimeToLocalFileTime
313     (lpFileTime      : access Long_Long_Integer;
314      lpLocalFileTime : access Long_Long_Integer) return BOOL;
315   pragma Import (Stdcall, FileTimeToLocalFileTime, "FileTimeToLocalFileTime");
316
317   function LocalFileTimeToFileTime
318     (lpFileTime      : access Long_Long_Integer;
319      lpLocalFileTime : access Long_Long_Integer) return BOOL;
320   pragma Import (Stdcall, LocalFileTimeToFileTime, "LocalFileTimeToFileTime");
321
322   procedure Sleep (dwMilliseconds : DWORD);
323   pragma Import (Stdcall, Sleep, External_Name => "Sleep");
324
325   function QueryPerformanceCounter
326     (lpPerformanceCount : access LARGE_INTEGER) return BOOL;
327   pragma Import
328     (Stdcall, QueryPerformanceCounter, "QueryPerformanceCounter");
329
330   ------------
331   -- Module --
332   ------------
333
334   function GetModuleHandleEx
335     (dwFlags      : DWORD;
336      lpModuleName : Address;
337      phModule     : access HANDLE) return BOOL;
338   pragma Import (Stdcall, GetModuleHandleEx, "GetModuleHandleExA");
339
340   function GetModuleFileName
341     (hModule    : HANDLE;
342      lpFilename : Address;
343      nSize      : DWORD) return DWORD;
344   pragma Import (Stdcall, GetModuleFileName, "GetModuleFileNameA");
345
346   function FreeLibrary (hModule : HANDLE) return BOOL;
347   pragma Import (Stdcall, FreeLibrary, "FreeLibrary");
348
349end System.Win32;
350