blob: d5c2867fcfd1f6473e7bb52a0e28f4df37caf761 [file] [log] [blame]
Josip Sokcevic14a83ae2020-05-21 01:36:34 +00001# Copyright 2020 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"""Win32 functions and constants."""
5
6import ctypes
7import ctypes.wintypes
8
9GENERIC_WRITE = 0x40000000
10CREATE_ALWAYS = 0x00000002
11FILE_ATTRIBUTE_NORMAL = 0x00000080
12LOCKFILE_EXCLUSIVE_LOCK = 0x00000002
13LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
14
15
16class Overlapped(ctypes.Structure):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000017 """Overlapped is required and used in LockFileEx and UnlockFileEx."""
18 _fields_ = [('Internal', ctypes.wintypes.LPVOID),
19 ('InternalHigh', ctypes.wintypes.LPVOID),
20 ('Offset', ctypes.wintypes.DWORD),
21 ('OffsetHigh', ctypes.wintypes.DWORD),
22 ('Pointer', ctypes.wintypes.LPVOID),
23 ('hEvent', ctypes.wintypes.HANDLE)]
Josip Sokcevic14a83ae2020-05-21 01:36:34 +000024
25
26# https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
27CreateFileW = ctypes.windll.kernel32.CreateFileW
28CreateFileW.argtypes = [
29 ctypes.wintypes.LPCWSTR, # lpFileName
30 ctypes.wintypes.DWORD, # dwDesiredAccess
31 ctypes.wintypes.DWORD, # dwShareMode
32 ctypes.wintypes.LPVOID, # lpSecurityAttributes
33 ctypes.wintypes.DWORD, # dwCreationDisposition
34 ctypes.wintypes.DWORD, # dwFlagsAndAttributes
35 ctypes.wintypes.LPVOID, # hTemplateFile
36]
37CreateFileW.restype = ctypes.wintypes.HANDLE
38
39# https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
40CloseHandle = ctypes.windll.kernel32.CloseHandle
41CloseHandle.argtypes = [
42 ctypes.wintypes.HANDLE, # hFile
43]
44CloseHandle.restype = ctypes.wintypes.BOOL
45
46# https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex
47LockFileEx = ctypes.windll.kernel32.LockFileEx
48LockFileEx.argtypes = [
49 ctypes.wintypes.HANDLE, # hFile
50 ctypes.wintypes.DWORD, # dwFlags
51 ctypes.wintypes.DWORD, # dwReserved
52 ctypes.wintypes.DWORD, # nNumberOfBytesToLockLow
53 ctypes.wintypes.DWORD, # nNumberOfBytesToLockHigh
54 ctypes.POINTER(Overlapped), # lpOverlapped
55]
56LockFileEx.restype = ctypes.wintypes.BOOL
57
58# Commonly used functions are listed here so callers don't need to import
59# ctypes.
60GetLastError = ctypes.GetLastError
61Handle = ctypes.wintypes.HANDLE