1 //! C definitions used by libnative that don't belong in liblibc
3 #![allow(nonstandard_style)]
4 #![cfg_attr(test, allow(dead_code))]
5 #![unstable(issue = "none", feature = "windows_c")]
8 use crate::os::raw::{c_char, c_int, c_long, c_longlong, c_uint, c_ulong, c_ushort};
9 use crate::os::windows::io::{BorrowedHandle, HandleOrInvalid, HandleOrNull};
11 use core::ffi::NonZero_c_ulong;
13 use libc::{c_void, size_t, wchar_t};
15 #[path = "c/errors.rs"] // c.rs is included from two places so we need to specify this
19 pub use self::EXCEPTION_DISPOSITION::*;
20 pub use self::FILE_INFO_BY_HANDLE_CLASS::*;
22 pub type DWORD_PTR = ULONG_PTR;
23 pub type DWORD = c_ulong;
24 pub type NonZeroDWORD = NonZero_c_ulong;
25 pub type HANDLE = LPVOID;
26 pub type HINSTANCE = HANDLE;
27 pub type HMODULE = HINSTANCE;
28 pub type HRESULT = LONG;
29 pub type BOOL = c_int;
31 pub type BOOLEAN = BYTE;
32 pub type GROUP = c_uint;
33 pub type LARGE_INTEGER = c_longlong;
34 pub type LONG = c_long;
35 pub type UINT = c_uint;
37 pub type USHORT = c_ushort;
38 pub type SIZE_T = usize;
40 pub type CHAR = c_char;
41 pub type CCHAR = c_char;
42 pub type ULONG_PTR = usize;
43 pub type ULONG = c_ulong;
44 pub type NTSTATUS = LONG;
45 pub type ACCESS_MASK = DWORD;
47 pub type LPBOOL = *mut BOOL;
48 pub type LPBYTE = *mut BYTE;
49 pub type LPCSTR = *const CHAR;
50 pub type LPCWSTR = *const WCHAR;
51 pub type LPDWORD = *mut DWORD;
52 pub type LPHANDLE = *mut HANDLE;
53 pub type LPOVERLAPPED = *mut OVERLAPPED;
54 pub type LPPROCESS_INFORMATION = *mut PROCESS_INFORMATION;
55 pub type LPSECURITY_ATTRIBUTES = *mut SECURITY_ATTRIBUTES;
56 pub type LPSTARTUPINFO = *mut STARTUPINFO;
57 pub type LPVOID = *mut c_void;
58 pub type LPWCH = *mut WCHAR;
59 pub type LPWIN32_FIND_DATAW = *mut WIN32_FIND_DATAW;
60 pub type LPWSADATA = *mut WSADATA;
61 pub type LPWSAPROTOCOL_INFO = *mut WSAPROTOCOL_INFO;
62 pub type LPWSTR = *mut WCHAR;
63 pub type LPFILETIME = *mut FILETIME;
64 pub type LPSYSTEM_INFO = *mut SYSTEM_INFO;
65 pub type LPWSABUF = *mut WSABUF;
66 pub type LPWSAOVERLAPPED = *mut c_void;
67 pub type LPWSAOVERLAPPED_COMPLETION_ROUTINE = *mut c_void;
69 pub type PCONDITION_VARIABLE = *mut CONDITION_VARIABLE;
70 pub type PLARGE_INTEGER = *mut c_longlong;
71 pub type PSRWLOCK = *mut SRWLOCK;
73 pub type SOCKET = crate::os::windows::raw::SOCKET;
74 pub type socklen_t = c_int;
75 pub type ADDRESS_FAMILY = USHORT;
77 pub const TRUE: BOOL = 1;
78 pub const FALSE: BOOL = 0;
80 pub const CSTR_LESS_THAN: c_int = 1;
81 pub const CSTR_EQUAL: c_int = 2;
82 pub const CSTR_GREATER_THAN: c_int = 3;
84 pub const FILE_ATTRIBUTE_READONLY: DWORD = 0x1;
85 pub const FILE_ATTRIBUTE_DIRECTORY: DWORD = 0x10;
86 pub const FILE_ATTRIBUTE_REPARSE_POINT: DWORD = 0x400;
87 pub const INVALID_FILE_ATTRIBUTES: DWORD = DWORD::MAX;
89 pub const FILE_SHARE_DELETE: DWORD = 0x4;
90 pub const FILE_SHARE_READ: DWORD = 0x1;
91 pub const FILE_SHARE_WRITE: DWORD = 0x2;
93 pub const FILE_OPEN: ULONG = 0x00000001;
94 pub const FILE_OPEN_REPARSE_POINT: ULONG = 0x200000;
95 pub const OBJ_DONT_REPARSE: ULONG = 0x1000;
97 pub const CREATE_ALWAYS: DWORD = 2;
98 pub const CREATE_NEW: DWORD = 1;
99 pub const OPEN_ALWAYS: DWORD = 4;
100 pub const OPEN_EXISTING: DWORD = 3;
101 pub const TRUNCATE_EXISTING: DWORD = 5;
103 pub const FILE_LIST_DIRECTORY: DWORD = 0x1;
104 pub const FILE_WRITE_DATA: DWORD = 0x00000002;
105 pub const FILE_APPEND_DATA: DWORD = 0x00000004;
106 pub const FILE_WRITE_EA: DWORD = 0x00000010;
107 pub const FILE_WRITE_ATTRIBUTES: DWORD = 0x00000100;
108 pub const DELETE: DWORD = 0x10000;
109 pub const READ_CONTROL: DWORD = 0x00020000;
110 pub const SYNCHRONIZE: DWORD = 0x00100000;
111 pub const GENERIC_READ: DWORD = 0x80000000;
112 pub const GENERIC_WRITE: DWORD = 0x40000000;
113 pub const STANDARD_RIGHTS_WRITE: DWORD = READ_CONTROL;
114 pub const FILE_GENERIC_WRITE: DWORD = STANDARD_RIGHTS_WRITE
116 | FILE_WRITE_ATTRIBUTES
121 pub const FILE_FLAG_OPEN_REPARSE_POINT: DWORD = 0x00200000;
122 pub const FILE_FLAG_BACKUP_SEMANTICS: DWORD = 0x02000000;
123 pub const SECURITY_SQOS_PRESENT: DWORD = 0x00100000;
125 pub const FIONBIO: c_ulong = 0x8004667e;
129 pub struct WIN32_FIND_DATAW {
130 pub dwFileAttributes: DWORD,
131 pub ftCreationTime: FILETIME,
132 pub ftLastAccessTime: FILETIME,
133 pub ftLastWriteTime: FILETIME,
134 pub nFileSizeHigh: DWORD,
135 pub nFileSizeLow: DWORD,
136 pub dwReserved0: DWORD,
137 pub dwReserved1: DWORD,
138 pub cFileName: [wchar_t; 260], // #define MAX_PATH 260
139 pub cAlternateFileName: [wchar_t; 14],
141 impl Clone for WIN32_FIND_DATAW {
142 fn clone(&self) -> Self {
147 pub const WSA_FLAG_OVERLAPPED: DWORD = 0x01;
148 pub const WSA_FLAG_NO_HANDLE_INHERIT: DWORD = 0x80;
150 pub const WSADESCRIPTION_LEN: usize = 256;
151 pub const WSASYS_STATUS_LEN: usize = 128;
152 pub const WSAPROTOCOL_LEN: DWORD = 255;
153 pub const INVALID_SOCKET: SOCKET = !0;
155 pub const MAX_PROTOCOL_CHAIN: DWORD = 7;
157 pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: usize = 16 * 1024;
158 pub const FSCTL_GET_REPARSE_POINT: DWORD = 0x900a8;
159 pub const IO_REPARSE_TAG_SYMLINK: DWORD = 0xa000000c;
160 pub const IO_REPARSE_TAG_MOUNT_POINT: DWORD = 0xa0000003;
161 pub const SYMLINK_FLAG_RELATIVE: DWORD = 0x00000001;
162 pub const FSCTL_SET_REPARSE_POINT: DWORD = 0x900a4;
164 pub const SYMBOLIC_LINK_FLAG_DIRECTORY: DWORD = 0x1;
165 pub const SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE: DWORD = 0x2;
167 // Note that these are not actually HANDLEs, just values to pass to GetStdHandle
168 pub const STD_INPUT_HANDLE: DWORD = -10i32 as DWORD;
169 pub const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
170 pub const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
172 pub const PROGRESS_CONTINUE: DWORD = 0;
174 pub const E_NOTIMPL: HRESULT = 0x80004001u32 as HRESULT;
176 pub const INVALID_HANDLE_VALUE: HANDLE = ptr::invalid_mut(!0);
178 pub const FACILITY_NT_BIT: DWORD = 0x1000_0000;
180 pub const FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
181 pub const FORMAT_MESSAGE_FROM_HMODULE: DWORD = 0x00000800;
182 pub const FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
184 pub const TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
186 pub const DLL_THREAD_DETACH: DWORD = 3;
187 pub const DLL_PROCESS_DETACH: DWORD = 0;
189 pub const INFINITE: DWORD = !0;
191 pub const DUPLICATE_SAME_ACCESS: DWORD = 0x00000002;
193 pub const CONDITION_VARIABLE_INIT: CONDITION_VARIABLE = CONDITION_VARIABLE { ptr: ptr::null_mut() };
194 pub const SRWLOCK_INIT: SRWLOCK = SRWLOCK { ptr: ptr::null_mut() };
196 pub const DETACHED_PROCESS: DWORD = 0x00000008;
197 pub const CREATE_NEW_PROCESS_GROUP: DWORD = 0x00000200;
198 pub const CREATE_UNICODE_ENVIRONMENT: DWORD = 0x00000400;
199 pub const STARTF_USESTDHANDLES: DWORD = 0x00000100;
201 pub const AF_INET: c_int = 2;
202 pub const AF_INET6: c_int = 23;
203 pub const SD_BOTH: c_int = 2;
204 pub const SD_RECEIVE: c_int = 0;
205 pub const SD_SEND: c_int = 1;
206 pub const SOCK_DGRAM: c_int = 2;
207 pub const SOCK_STREAM: c_int = 1;
208 pub const SOCKET_ERROR: c_int = -1;
209 pub const SOL_SOCKET: c_int = 0xffff;
210 pub const SO_LINGER: c_int = 0x0080;
211 pub const SO_RCVTIMEO: c_int = 0x1006;
212 pub const SO_SNDTIMEO: c_int = 0x1005;
213 pub const IPPROTO_IP: c_int = 0;
214 pub const IPPROTO_TCP: c_int = 6;
215 pub const IPPROTO_IPV6: c_int = 41;
216 pub const TCP_NODELAY: c_int = 0x0001;
217 pub const IP_TTL: c_int = 4;
218 pub const IPV6_V6ONLY: c_int = 27;
219 pub const SO_ERROR: c_int = 0x1007;
220 pub const SO_BROADCAST: c_int = 0x0020;
221 pub const IP_MULTICAST_LOOP: c_int = 11;
222 pub const IPV6_MULTICAST_LOOP: c_int = 11;
223 pub const IP_MULTICAST_TTL: c_int = 10;
224 pub const IP_ADD_MEMBERSHIP: c_int = 12;
225 pub const IP_DROP_MEMBERSHIP: c_int = 13;
226 pub const IPV6_ADD_MEMBERSHIP: c_int = 12;
227 pub const IPV6_DROP_MEMBERSHIP: c_int = 13;
228 pub const MSG_PEEK: c_int = 0x2;
231 #[derive(Copy, Clone)]
233 pub l_onoff: c_ushort,
234 pub l_linger: c_ushort,
239 pub imr_multiaddr: in_addr,
240 pub imr_interface: in_addr,
244 pub struct ipv6_mreq {
245 pub ipv6mr_multiaddr: in6_addr,
246 pub ipv6mr_interface: c_uint,
249 pub const VOLUME_NAME_DOS: DWORD = 0x0;
250 pub const MOVEFILE_REPLACE_EXISTING: DWORD = 1;
252 pub const FILE_BEGIN: DWORD = 0;
253 pub const FILE_CURRENT: DWORD = 1;
254 pub const FILE_END: DWORD = 2;
256 pub const WAIT_OBJECT_0: DWORD = 0x00000000;
257 pub const WAIT_TIMEOUT: DWORD = 258;
258 pub const WAIT_FAILED: DWORD = 0xFFFFFFFF;
260 pub const PIPE_ACCESS_INBOUND: DWORD = 0x00000001;
261 pub const PIPE_ACCESS_OUTBOUND: DWORD = 0x00000002;
262 pub const FILE_FLAG_FIRST_PIPE_INSTANCE: DWORD = 0x00080000;
263 pub const FILE_FLAG_OVERLAPPED: DWORD = 0x40000000;
264 pub const PIPE_WAIT: DWORD = 0x00000000;
265 pub const PIPE_TYPE_BYTE: DWORD = 0x00000000;
266 pub const PIPE_REJECT_REMOTE_CLIENTS: DWORD = 0x00000008;
267 pub const PIPE_READMODE_BYTE: DWORD = 0x00000000;
269 pub const FD_SETSIZE: usize = 64;
271 pub const STACK_SIZE_PARAM_IS_A_RESERVATION: DWORD = 0x00010000;
273 pub const STATUS_SUCCESS: NTSTATUS = 0x00000000;
274 pub const STATUS_DELETE_PENDING: NTSTATUS = 0xc0000056_u32 as _;
275 pub const STATUS_INVALID_PARAMETER: NTSTATUS = 0xc000000d_u32 as _;
277 pub const STATUS_PENDING: NTSTATUS = 0x103 as _;
278 pub const STATUS_END_OF_FILE: NTSTATUS = 0xC0000011_u32 as _;
280 // Equivalent to the `NT_SUCCESS` C preprocessor macro.
281 // See: https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/using-ntstatus-values
282 pub fn nt_success(status: NTSTATUS) -> bool {
286 pub const BCRYPT_USE_SYSTEM_PREFERRED_RNG: DWORD = 0x00000002;
289 pub struct UNICODE_STRING {
291 pub MaximumLength: u16,
292 pub Buffer: *mut u16,
294 impl UNICODE_STRING {
295 pub fn from_ref(slice: &[u16]) -> Self {
296 let len = slice.len() * mem::size_of::<u16>();
297 Self { Length: len as _, MaximumLength: len as _, Buffer: slice.as_ptr() as _ }
301 pub struct OBJECT_ATTRIBUTES {
303 pub RootDirectory: HANDLE,
304 pub ObjectName: *const UNICODE_STRING,
305 pub Attributes: ULONG,
306 pub SecurityDescriptor: *mut c_void,
307 pub SecurityQualityOfService: *mut c_void,
309 impl Default for OBJECT_ATTRIBUTES {
310 fn default() -> Self {
312 Length: mem::size_of::<Self>() as _,
313 RootDirectory: ptr::null_mut(),
314 ObjectName: ptr::null_mut(),
316 SecurityDescriptor: ptr::null_mut(),
317 SecurityQualityOfService: ptr::null_mut(),
322 union IO_STATUS_BLOCK_union {
324 Pointer: *mut c_void,
326 impl Default for IO_STATUS_BLOCK_union {
327 fn default() -> Self {
328 Self { Pointer: ptr::null_mut() }
333 pub struct IO_STATUS_BLOCK {
334 u: IO_STATUS_BLOCK_union,
335 pub Information: usize,
338 pub type LPOVERLAPPED_COMPLETION_ROUTINE = unsafe extern "system" fn(
340 dwNumberOfBytesTransfered: DWORD,
341 lpOverlapped: *mut OVERLAPPED,
344 type IO_APC_ROUTINE = unsafe extern "system" fn(
345 ApcContext: *mut c_void,
346 IoStatusBlock: *mut IO_STATUS_BLOCK,
351 #[cfg(not(target_pointer_width = "64"))]
354 pub wHighVersion: WORD,
355 pub szDescription: [u8; WSADESCRIPTION_LEN + 1],
356 pub szSystemStatus: [u8; WSASYS_STATUS_LEN + 1],
357 pub iMaxSockets: u16,
359 pub lpVendorInfo: *mut u8,
362 #[cfg(target_pointer_width = "64")]
365 pub wHighVersion: WORD,
366 pub iMaxSockets: u16,
368 pub lpVendorInfo: *mut u8,
369 pub szDescription: [u8; WSADESCRIPTION_LEN + 1],
370 pub szSystemStatus: [u8; WSASYS_STATUS_LEN + 1],
373 #[derive(Copy, Clone)]
381 pub struct WSAPROTOCOL_INFO {
382 pub dwServiceFlags1: DWORD,
383 pub dwServiceFlags2: DWORD,
384 pub dwServiceFlags3: DWORD,
385 pub dwServiceFlags4: DWORD,
386 pub dwProviderFlags: DWORD,
387 pub ProviderId: GUID,
388 pub dwCatalogEntryId: DWORD,
389 pub ProtocolChain: WSAPROTOCOLCHAIN,
391 pub iAddressFamily: c_int,
392 pub iMaxSockAddr: c_int,
393 pub iMinSockAddr: c_int,
394 pub iSocketType: c_int,
395 pub iProtocol: c_int,
396 pub iProtocolMaxOffset: c_int,
397 pub iNetworkByteOrder: c_int,
398 pub iSecurityScheme: c_int,
399 pub dwMessageSize: DWORD,
400 pub dwProviderReserved: DWORD,
401 pub szProtocol: [u16; (WSAPROTOCOL_LEN as usize) + 1],
405 #[derive(Copy, Clone)]
406 pub struct WIN32_FILE_ATTRIBUTE_DATA {
407 pub dwFileAttributes: DWORD,
408 pub ftCreationTime: FILETIME,
409 pub ftLastAccessTime: FILETIME,
410 pub ftLastWriteTime: FILETIME,
411 pub nFileSizeHigh: DWORD,
412 pub nFileSizeLow: DWORD,
416 #[allow(dead_code)] // we only use some variants
417 pub enum FILE_INFO_BY_HANDLE_CLASS {
419 FileStandardInfo = 1,
422 FileDispositionInfo = 4,
423 FileAllocationInfo = 5,
424 FileEndOfFileInfo = 6,
426 FileCompressionInfo = 8,
427 FileAttributeTagInfo = 9,
428 FileIdBothDirectoryInfo = 10, // 0xA
429 FileIdBothDirectoryRestartInfo = 11, // 0xB
430 FileIoPriorityHintInfo = 12, // 0xC
431 FileRemoteProtocolInfo = 13, // 0xD
432 FileFullDirectoryInfo = 14, // 0xE
433 FileFullDirectoryRestartInfo = 15, // 0xF
434 FileStorageInfo = 16, // 0x10
435 FileAlignmentInfo = 17, // 0x11
436 FileIdInfo = 18, // 0x12
437 FileIdExtdDirectoryInfo = 19, // 0x13
438 FileIdExtdDirectoryRestartInfo = 20, // 0x14
439 FileDispositionInfoEx = 21, // 0x15, Windows 10 version 1607
440 MaximumFileInfoByHandlesClass,
444 pub struct FILE_DISPOSITION_INFO {
445 pub DeleteFile: BOOLEAN,
448 pub const FILE_DISPOSITION_DELETE: DWORD = 0x1;
449 pub const FILE_DISPOSITION_POSIX_SEMANTICS: DWORD = 0x2;
450 pub const FILE_DISPOSITION_IGNORE_READONLY_ATTRIBUTE: DWORD = 0x10;
453 pub struct FILE_DISPOSITION_INFO_EX {
459 pub struct FILE_ID_BOTH_DIR_INFO {
460 pub NextEntryOffset: DWORD,
461 pub FileIndex: DWORD,
462 pub CreationTime: LARGE_INTEGER,
463 pub LastAccessTime: LARGE_INTEGER,
464 pub LastWriteTime: LARGE_INTEGER,
465 pub ChangeTime: LARGE_INTEGER,
466 pub EndOfFile: LARGE_INTEGER,
467 pub AllocationSize: LARGE_INTEGER,
468 pub FileAttributes: DWORD,
469 pub FileNameLength: DWORD,
471 pub ShortNameLength: CCHAR,
472 pub ShortName: [WCHAR; 12],
473 pub FileId: LARGE_INTEGER,
474 pub FileName: [WCHAR; 1],
477 pub struct FILE_BASIC_INFO {
478 pub CreationTime: LARGE_INTEGER,
479 pub LastAccessTime: LARGE_INTEGER,
480 pub LastWriteTime: LARGE_INTEGER,
481 pub ChangeTime: LARGE_INTEGER,
482 pub FileAttributes: DWORD,
486 pub struct FILE_END_OF_FILE_INFO {
487 pub EndOfFile: LARGE_INTEGER,
491 pub struct REPARSE_DATA_BUFFER {
492 pub ReparseTag: c_uint,
493 pub ReparseDataLength: c_ushort,
494 pub Reserved: c_ushort,
499 pub struct SYMBOLIC_LINK_REPARSE_BUFFER {
500 pub SubstituteNameOffset: c_ushort,
501 pub SubstituteNameLength: c_ushort,
502 pub PrintNameOffset: c_ushort,
503 pub PrintNameLength: c_ushort,
505 pub PathBuffer: WCHAR,
509 pub struct MOUNT_POINT_REPARSE_BUFFER {
510 pub SubstituteNameOffset: c_ushort,
511 pub SubstituteNameLength: c_ushort,
512 pub PrintNameOffset: c_ushort,
513 pub PrintNameLength: c_ushort,
514 pub PathBuffer: WCHAR,
517 pub type LPPROGRESS_ROUTINE = crate::option::Option<
518 unsafe extern "system" fn(
519 TotalFileSize: LARGE_INTEGER,
520 TotalBytesTransferred: LARGE_INTEGER,
521 StreamSize: LARGE_INTEGER,
522 StreamBytesTransferred: LARGE_INTEGER,
523 dwStreamNumber: DWORD,
524 dwCallbackReason: DWORD,
526 hDestinationFile: HANDLE,
532 pub struct CONDITION_VARIABLE {
541 pub struct REPARSE_MOUNTPOINT_DATA_BUFFER {
542 pub ReparseTag: DWORD,
543 pub ReparseDataLength: DWORD,
545 pub ReparseTargetLength: WORD,
546 pub ReparseTargetMaximumLength: WORD,
548 pub ReparseTarget: WCHAR,
556 pub Data4: [BYTE; 8],
560 pub struct WSAPROTOCOLCHAIN {
562 pub ChainEntries: [DWORD; MAX_PROTOCOL_CHAIN as usize],
566 pub struct SECURITY_ATTRIBUTES {
568 pub lpSecurityDescriptor: LPVOID,
569 pub bInheritHandle: BOOL,
573 pub struct PROCESS_INFORMATION {
574 pub hProcess: HANDLE,
576 pub dwProcessId: DWORD,
577 pub dwThreadId: DWORD,
581 pub struct STARTUPINFO {
583 pub lpReserved: LPWSTR,
584 pub lpDesktop: LPWSTR,
590 pub dwXCountChars: DWORD,
591 pub dwYCountCharts: DWORD,
592 pub dwFillAttribute: DWORD,
594 pub wShowWindow: WORD,
595 pub cbReserved2: WORD,
596 pub lpReserved2: LPBYTE,
597 pub hStdInput: HANDLE,
598 pub hStdOutput: HANDLE,
599 pub hStdError: HANDLE,
603 pub struct SOCKADDR {
604 pub sa_family: ADDRESS_FAMILY,
605 pub sa_data: [CHAR; 14],
609 #[derive(Copy, Clone)]
610 pub struct FILETIME {
611 pub dwLowDateTime: DWORD,
612 pub dwHighDateTime: DWORD,
616 pub struct SYSTEM_INFO {
617 pub wProcessorArchitecture: WORD,
619 pub dwPageSize: DWORD,
620 pub lpMinimumApplicationAddress: LPVOID,
621 pub lpMaximumApplicationAddress: LPVOID,
622 pub dwActiveProcessorMask: DWORD_PTR,
623 pub dwNumberOfProcessors: DWORD,
624 pub dwProcessorType: DWORD,
625 pub dwAllocationGranularity: DWORD,
626 pub wProcessorLevel: WORD,
627 pub wProcessorRevision: WORD,
631 pub struct OVERLAPPED {
632 pub Internal: *mut c_ulong,
633 pub InternalHigh: *mut c_ulong,
635 pub OffsetHigh: DWORD,
640 #[allow(dead_code)] // we only use some variants
641 pub enum ADDRESS_MODE {
649 pub struct SOCKADDR_STORAGE_LH {
650 pub ss_family: ADDRESS_FAMILY,
651 pub __ss_pad1: [CHAR; 6],
653 pub __ss_pad2: [CHAR; 112],
657 pub struct ADDRINFOA {
659 pub ai_family: c_int,
660 pub ai_socktype: c_int,
661 pub ai_protocol: c_int,
662 pub ai_addrlen: size_t,
663 pub ai_canonname: *mut c_char,
664 pub ai_addr: *mut SOCKADDR,
665 pub ai_next: *mut ADDRINFOA,
669 #[derive(Copy, Clone)]
670 pub struct sockaddr_in {
671 pub sin_family: ADDRESS_FAMILY,
672 pub sin_port: USHORT,
673 pub sin_addr: in_addr,
674 pub sin_zero: [CHAR; 8],
678 #[derive(Copy, Clone)]
679 pub struct sockaddr_in6 {
680 pub sin6_family: ADDRESS_FAMILY,
681 pub sin6_port: USHORT,
682 pub sin6_flowinfo: c_ulong,
683 pub sin6_addr: in6_addr,
684 pub sin6_scope_id: c_ulong,
688 #[derive(Copy, Clone)]
694 #[derive(Copy, Clone)]
695 pub struct in6_addr {
696 pub s6_addr: [u8; 16],
700 #[derive(Copy, Clone)]
701 #[allow(dead_code)] // we only use some variants
702 pub enum EXCEPTION_DISPOSITION {
703 ExceptionContinueExecution,
704 ExceptionContinueSearch,
705 ExceptionNestedException,
706 ExceptionCollidedUnwind,
712 pub fd_count: c_uint,
713 pub fd_array: [SOCKET; FD_SETSIZE],
716 impl Clone for fd_set {
717 fn clone(&self) -> fd_set {
723 #[derive(Copy, Clone)]
729 // Desktop specific functions & types
731 if #[cfg(not(target_vendor = "uwp"))] {
732 pub const EXCEPTION_CONTINUE_SEARCH: LONG = 0;
733 pub const EXCEPTION_STACK_OVERFLOW: DWORD = 0xc00000fd;
734 pub const EXCEPTION_MAXIMUM_PARAMETERS: usize = 15;
737 pub struct EXCEPTION_RECORD {
738 pub ExceptionCode: DWORD,
739 pub ExceptionFlags: DWORD,
740 pub ExceptionRecord: *mut EXCEPTION_RECORD,
741 pub ExceptionAddress: LPVOID,
742 pub NumberParameters: DWORD,
743 pub ExceptionInformation: [LPVOID; EXCEPTION_MAXIMUM_PARAMETERS],
749 pub struct EXCEPTION_POINTERS {
750 pub ExceptionRecord: *mut EXCEPTION_RECORD,
751 pub ContextRecord: *mut CONTEXT,
754 pub type PVECTORED_EXCEPTION_HANDLER =
755 extern "system" fn(ExceptionInfo: *mut EXCEPTION_POINTERS) -> LONG;
758 #[derive(Copy, Clone)]
759 pub struct CONSOLE_READCONSOLE_CONTROL {
761 pub nInitialChars: ULONG,
762 pub dwCtrlWakeupMask: ULONG,
763 pub dwControlKeyState: ULONG,
766 pub type PCONSOLE_READCONSOLE_CONTROL = *mut CONSOLE_READCONSOLE_CONTROL;
769 pub struct BY_HANDLE_FILE_INFORMATION {
770 pub dwFileAttributes: DWORD,
771 pub ftCreationTime: FILETIME,
772 pub ftLastAccessTime: FILETIME,
773 pub ftLastWriteTime: FILETIME,
774 pub dwVolumeSerialNumber: DWORD,
775 pub nFileSizeHigh: DWORD,
776 pub nFileSizeLow: DWORD,
777 pub nNumberOfLinks: DWORD,
778 pub nFileIndexHigh: DWORD,
779 pub nFileIndexLow: DWORD,
782 pub type LPBY_HANDLE_FILE_INFORMATION = *mut BY_HANDLE_FILE_INFORMATION;
783 pub type LPCVOID = *const c_void;
785 pub const HANDLE_FLAG_INHERIT: DWORD = 0x00000001;
787 pub const TOKEN_READ: DWORD = 0x20008;
789 #[link(name = "advapi32")]
791 // Allowed but unused by UWP
792 pub fn OpenProcessToken(
793 ProcessHandle: HANDLE,
794 DesiredAccess: DWORD,
795 TokenHandle: *mut HANDLE,
799 #[link(name = "userenv")]
801 // Allowed but unused by UWP
802 pub fn GetUserProfileDirectoryW(
804 lpProfileDir: LPWSTR,
805 lpcchSize: *mut DWORD,
809 #[link(name = "kernel32")]
811 // Functions forbidden when targeting UWP
813 hConsoleInput: HANDLE,
815 nNumberOfCharsToRead: DWORD,
816 lpNumberOfCharsRead: LPDWORD,
817 pInputControl: PCONSOLE_READCONSOLE_CONTROL,
820 pub fn WriteConsoleW(
821 hConsoleOutput: HANDLE,
823 nNumberOfCharsToWrite: DWORD,
824 lpNumberOfCharsWritten: LPDWORD,
828 pub fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL;
829 // Allowed but unused by UWP
830 pub fn GetFileInformationByHandle(
832 lpFileInformation: LPBY_HANDLE_FILE_INFORMATION,
834 pub fn SetHandleInformation(hObject: HANDLE, dwMask: DWORD, dwFlags: DWORD) -> BOOL;
835 pub fn AddVectoredExceptionHandler(
837 VectoredHandler: PVECTORED_EXCEPTION_HANDLER,
839 pub fn CreateHardLinkW(
840 lpSymlinkFileName: LPCWSTR,
841 lpTargetFileName: LPCWSTR,
842 lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
844 pub fn SetThreadStackGuarantee(_size: *mut c_ulong) -> BOOL;
845 pub fn GetWindowsDirectoryW(lpBuffer: LPWSTR, uSize: UINT) -> UINT;
850 // UWP specific functions & types
852 if #[cfg(target_vendor = "uwp")] {
854 pub struct FILE_STANDARD_INFO {
855 pub AllocationSize: LARGE_INTEGER,
856 pub EndOfFile: LARGE_INTEGER,
857 pub NumberOfLinks: DWORD,
858 pub DeletePending: BOOLEAN,
859 pub Directory: BOOLEAN,
864 // Shared between Desktop & UWP
866 #[link(name = "kernel32")]
868 pub fn GetCurrentProcessId() -> DWORD;
870 pub fn GetSystemDirectoryW(lpBuffer: LPWSTR, uSize: UINT) -> UINT;
871 pub fn RemoveDirectoryW(lpPathName: LPCWSTR) -> BOOL;
872 pub fn SetFileAttributesW(lpFileName: LPCWSTR, dwFileAttributes: DWORD) -> BOOL;
873 pub fn SetLastError(dwErrCode: DWORD);
874 pub fn GetCommandLineW() -> LPWSTR;
875 pub fn GetTempPathW(nBufferLength: DWORD, lpBuffer: LPCWSTR) -> DWORD;
876 pub fn GetCurrentProcess() -> HANDLE;
877 pub fn GetCurrentThread() -> HANDLE;
878 pub fn GetStdHandle(which: DWORD) -> HANDLE;
879 pub fn ExitProcess(uExitCode: c_uint) -> !;
880 pub fn DeviceIoControl(
882 dwIoControlCode: DWORD,
884 nInBufferSize: DWORD,
886 nOutBufferSize: DWORD,
887 lpBytesReturned: LPDWORD,
888 lpOverlapped: LPOVERLAPPED,
891 lpThreadAttributes: LPSECURITY_ATTRIBUTES,
893 lpStartAddress: extern "system" fn(*mut c_void) -> DWORD,
895 dwCreationFlags: DWORD,
898 pub fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) -> DWORD;
899 pub fn SwitchToThread() -> BOOL;
900 pub fn Sleep(dwMilliseconds: DWORD);
901 pub fn SleepEx(dwMilliseconds: DWORD, bAlertable: BOOL) -> DWORD;
902 pub fn GetProcessId(handle: HANDLE) -> DWORD;
904 lpExistingFileName: LPCWSTR,
905 lpNewFileName: LPCWSTR,
906 lpProgressRoutine: LPPROGRESS_ROUTINE,
911 pub fn FormatMessageW(
920 pub fn TlsAlloc() -> DWORD;
921 pub fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
922 pub fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
923 pub fn GetLastError() -> DWORD;
924 pub fn QueryPerformanceFrequency(lpFrequency: *mut LARGE_INTEGER) -> BOOL;
925 pub fn QueryPerformanceCounter(lpPerformanceCount: *mut LARGE_INTEGER) -> BOOL;
926 pub fn GetExitCodeProcess(hProcess: HANDLE, lpExitCode: LPDWORD) -> BOOL;
927 pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) -> BOOL;
928 pub fn CreateProcessW(
929 lpApplicationName: LPCWSTR,
930 lpCommandLine: LPWSTR,
931 lpProcessAttributes: LPSECURITY_ATTRIBUTES,
932 lpThreadAttributes: LPSECURITY_ATTRIBUTES,
933 bInheritHandles: BOOL,
934 dwCreationFlags: DWORD,
935 lpEnvironment: LPVOID,
936 lpCurrentDirectory: LPCWSTR,
937 lpStartupInfo: LPSTARTUPINFO,
938 lpProcessInformation: LPPROCESS_INFORMATION,
940 pub fn GetEnvironmentVariableW(n: LPCWSTR, v: LPWSTR, nsize: DWORD) -> DWORD;
941 pub fn SetEnvironmentVariableW(n: LPCWSTR, v: LPCWSTR) -> BOOL;
942 pub fn GetEnvironmentStringsW() -> LPWCH;
943 pub fn FreeEnvironmentStringsW(env_ptr: LPWCH) -> BOOL;
944 pub fn GetModuleFileNameW(hModule: HMODULE, lpFilename: LPWSTR, nSize: DWORD) -> DWORD;
945 pub fn CreateDirectoryW(
947 lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
949 pub fn DeleteFileW(lpPathName: LPCWSTR) -> BOOL;
950 pub fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: LPWSTR) -> DWORD;
951 pub fn SetCurrentDirectoryW(lpPathName: LPCWSTR) -> BOOL;
952 pub fn DuplicateHandle(
953 hSourceProcessHandle: HANDLE,
954 hSourceHandle: HANDLE,
955 hTargetProcessHandle: HANDLE,
956 lpTargetHandle: LPHANDLE,
957 dwDesiredAccess: DWORD,
958 bInheritHandle: BOOL,
962 hFile: BorrowedHandle<'_>,
964 nNumberOfBytesToRead: DWORD,
965 lpNumberOfBytesRead: LPDWORD,
966 lpOverlapped: LPOVERLAPPED,
969 hFile: BorrowedHandle<'_>,
971 nNumberOfBytesToRead: DWORD,
972 lpOverlapped: LPOVERLAPPED,
973 lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE,
976 hFile: BorrowedHandle<'_>,
978 nNumberOfBytesToWrite: DWORD,
979 lpOverlapped: LPOVERLAPPED,
980 lpCompletionRoutine: LPOVERLAPPED_COMPLETION_ROUTINE,
982 pub fn CloseHandle(hObject: HANDLE) -> BOOL;
983 pub fn MoveFileExW(lpExistingFileName: LPCWSTR, lpNewFileName: LPCWSTR, dwFlags: DWORD)
985 pub fn SetFilePointerEx(
987 liDistanceToMove: LARGE_INTEGER,
988 lpNewFilePointer: PLARGE_INTEGER,
991 pub fn FlushFileBuffers(hFile: HANDLE) -> BOOL;
994 dwDesiredAccess: DWORD,
996 lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
997 dwCreationDisposition: DWORD,
998 dwFlagsAndAttributes: DWORD,
999 hTemplateFile: HANDLE,
1000 ) -> HandleOrInvalid;
1002 pub fn FindFirstFileW(fileName: LPCWSTR, findFileData: LPWIN32_FIND_DATAW) -> HANDLE;
1003 pub fn FindNextFileW(findFile: HANDLE, findFileData: LPWIN32_FIND_DATAW) -> BOOL;
1004 pub fn FindClose(findFile: HANDLE) -> BOOL;
1006 pub fn GetProcAddress(handle: HMODULE, name: LPCSTR) -> *mut c_void;
1007 pub fn GetModuleHandleA(lpModuleName: LPCSTR) -> HMODULE;
1008 pub fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE;
1010 pub fn GetSystemTimeAsFileTime(lpSystemTimeAsFileTime: LPFILETIME);
1011 pub fn GetSystemInfo(lpSystemInfo: LPSYSTEM_INFO);
1013 pub fn CreateEventW(
1014 lpEventAttributes: LPSECURITY_ATTRIBUTES,
1016 bInitialState: BOOL,
1019 pub fn WaitForMultipleObjects(
1021 lpHandles: *const HANDLE,
1023 dwMilliseconds: DWORD,
1026 hReadPipe: *mut HANDLE,
1027 hWritePipe: *mut HANDLE,
1028 lpPipeAttributes: *const SECURITY_ATTRIBUTES,
1031 pub fn CreateNamedPipeW(
1035 nMaxInstances: DWORD,
1036 nOutBufferSize: DWORD,
1037 nInBufferSize: DWORD,
1038 nDefaultTimeOut: DWORD,
1039 lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
1041 pub fn CancelIo(handle: HANDLE) -> BOOL;
1042 pub fn GetOverlappedResult(
1044 lpOverlapped: LPOVERLAPPED,
1045 lpNumberOfBytesTransferred: LPDWORD,
1048 pub fn CreateSymbolicLinkW(
1049 lpSymlinkFileName: LPCWSTR,
1050 lpTargetFileName: LPCWSTR,
1053 pub fn GetFinalPathNameByHandleW(
1055 lpszFilePath: LPCWSTR,
1059 pub fn GetFileInformationByHandleEx(
1061 fileInfoClass: FILE_INFO_BY_HANDLE_CLASS,
1062 lpFileInformation: LPVOID,
1063 dwBufferSize: DWORD,
1065 pub fn SetFileInformationByHandle(
1067 FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
1068 lpFileInformation: LPVOID,
1069 dwBufferSize: DWORD,
1071 pub fn SleepConditionVariableSRW(
1072 ConditionVariable: PCONDITION_VARIABLE,
1074 dwMilliseconds: DWORD,
1078 pub fn WakeConditionVariable(ConditionVariable: PCONDITION_VARIABLE);
1079 pub fn WakeAllConditionVariable(ConditionVariable: PCONDITION_VARIABLE);
1081 pub fn AcquireSRWLockExclusive(SRWLock: PSRWLOCK);
1082 pub fn AcquireSRWLockShared(SRWLock: PSRWLOCK);
1083 pub fn ReleaseSRWLockExclusive(SRWLock: PSRWLOCK);
1084 pub fn ReleaseSRWLockShared(SRWLock: PSRWLOCK);
1085 pub fn TryAcquireSRWLockExclusive(SRWLock: PSRWLOCK) -> BOOLEAN;
1086 pub fn TryAcquireSRWLockShared(SRWLock: PSRWLOCK) -> BOOLEAN;
1088 pub fn CompareStringOrdinal(
1095 pub fn GetFullPathNameW(
1096 lpFileName: LPCWSTR,
1097 nBufferLength: DWORD,
1099 lpFilePart: *mut LPWSTR,
1101 pub fn GetFileAttributesW(lpFileName: LPCWSTR) -> DWORD;
1104 #[link(name = "ws2_32")]
1106 pub fn WSAStartup(wVersionRequested: WORD, lpWSAData: LPWSADATA) -> c_int;
1107 pub fn WSACleanup() -> c_int;
1108 pub fn WSAGetLastError() -> c_int;
1109 pub fn WSADuplicateSocketW(
1112 lpProtocolInfo: LPWSAPROTOCOL_INFO,
1116 lpBuffers: LPWSABUF,
1117 dwBufferCount: DWORD,
1118 lpNumberOfBytesSent: LPDWORD,
1120 lpOverlapped: LPWSAOVERLAPPED,
1121 lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE,
1125 lpBuffers: LPWSABUF,
1126 dwBufferCount: DWORD,
1127 lpNumberOfBytesRecvd: LPDWORD,
1129 lpOverlapped: LPWSAOVERLAPPED,
1130 lpCompletionRoutine: LPWSAOVERLAPPED_COMPLETION_ROUTINE,
1136 lpProtocolInfo: LPWSAPROTOCOL_INFO,
1140 pub fn ioctlsocket(s: SOCKET, cmd: c_long, argp: *mut c_ulong) -> c_int;
1141 pub fn closesocket(socket: SOCKET) -> c_int;
1142 pub fn recv(socket: SOCKET, buf: *mut c_void, len: c_int, flags: c_int) -> c_int;
1143 pub fn send(socket: SOCKET, buf: *const c_void, len: c_int, flags: c_int) -> c_int;
1149 addr: *mut SOCKADDR,
1150 addrlen: *mut c_int,
1157 addr: *const SOCKADDR,
1160 pub fn shutdown(socket: SOCKET, how: c_int) -> c_int;
1161 pub fn accept(socket: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> SOCKET;
1166 optval: *mut c_char,
1173 optval: *const c_void,
1176 pub fn getsockname(socket: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> c_int;
1177 pub fn getpeername(socket: SOCKET, address: *mut SOCKADDR, address_len: *mut c_int) -> c_int;
1178 pub fn bind(socket: SOCKET, address: *const SOCKADDR, address_len: socklen_t) -> c_int;
1179 pub fn listen(socket: SOCKET, backlog: c_int) -> c_int;
1180 pub fn connect(socket: SOCKET, address: *const SOCKADDR, len: c_int) -> c_int;
1182 node: *const c_char,
1183 service: *const c_char,
1184 hints: *const ADDRINFOA,
1185 res: *mut *mut ADDRINFOA,
1187 pub fn freeaddrinfo(res: *mut ADDRINFOA);
1190 readfds: *mut fd_set,
1191 writefds: *mut fd_set,
1192 exceptfds: *mut fd_set,
1193 timeout: *const timeval,
1197 #[link(name = "bcrypt")]
1199 // >= Vista / Server 2008
1200 // https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom
1201 pub fn BCryptGenRandom(
1209 // Functions that aren't available on every version of Windows that we support,
1210 // but we still use them and just provide some form of a fallback implementation.
1215 // https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
1216 pub fn SetThreadDescription(hThread: HANDLE,
1217 lpThreadDescription: LPCWSTR) -> HRESULT {
1218 SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); E_NOTIMPL
1221 // >= Win8 / Server 2012
1222 // https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtimepreciseasfiletime
1223 pub fn GetSystemTimePreciseAsFileTime(lpSystemTimeAsFileTime: LPFILETIME)
1225 GetSystemTimeAsFileTime(lpSystemTimeAsFileTime)
1228 // >= Win11 / Server 2022
1229 // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppath2a
1230 pub fn GetTempPath2W(nBufferLength: DWORD, lpBuffer: LPCWSTR) -> DWORD {
1231 GetTempPathW(nBufferLength, lpBuffer)
1236 "api-ms-win-core-synch-l1-2-0":
1238 // >= Windows 8 / Server 2012
1239 // https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitonaddress
1240 pub fn WaitOnAddress(
1242 CompareAddress: LPVOID,
1243 AddressSize: SIZE_T,
1244 dwMilliseconds: DWORD
1246 panic!("WaitOnAddress not available")
1248 pub fn WakeByAddressSingle(Address: LPVOID) -> () {
1249 // If this api is unavailable, there cannot be anything waiting, because
1250 // WaitOnAddress would've panicked. So it's fine to do nothing here.
1256 pub fn NtCreateFile(
1257 FileHandle: *mut HANDLE,
1258 DesiredAccess: ACCESS_MASK,
1259 ObjectAttributes: *const OBJECT_ATTRIBUTES,
1260 IoStatusBlock: *mut IO_STATUS_BLOCK,
1261 AllocationSize: *mut i64,
1262 FileAttributes: ULONG,
1264 CreateDisposition: ULONG,
1265 CreateOptions: ULONG,
1266 EaBuffer: *mut c_void,
1269 panic!("`NtCreateFile` not available");
1272 FileHandle: BorrowedHandle<'_>,
1274 ApcRoutine: Option<IO_APC_ROUTINE>,
1275 ApcContext: *mut c_void,
1276 IoStatusBlock: &mut IO_STATUS_BLOCK,
1277 Buffer: *mut crate::mem::MaybeUninit<u8>,
1279 ByteOffset: Option<&LARGE_INTEGER>,
1282 panic!("`NtReadFile` not available");
1285 FileHandle: BorrowedHandle<'_>,
1287 ApcRoutine: Option<IO_APC_ROUTINE>,
1288 ApcContext: *mut c_void,
1289 IoStatusBlock: &mut IO_STATUS_BLOCK,
1292 ByteOffset: Option<&LARGE_INTEGER>,
1295 panic!("`NtWriteFile` not available");
1297 pub fn RtlNtStatusToDosError(
1300 panic!("`RtlNtStatusToDosError` not available");
1302 pub fn NtCreateKeyedEvent(
1303 KeyedEventHandle: LPHANDLE,
1304 DesiredAccess: ACCESS_MASK,
1305 ObjectAttributes: LPVOID,
1308 panic!("keyed events not available")
1310 pub fn NtReleaseKeyedEvent(
1311 EventHandle: HANDLE,
1314 Timeout: PLARGE_INTEGER
1316 panic!("keyed events not available")
1318 pub fn NtWaitForKeyedEvent(
1319 EventHandle: HANDLE,
1322 Timeout: PLARGE_INTEGER
1324 panic!("keyed events not available")