Josip Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 1 | # 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 | |
| 6 | import ctypes |
| 7 | import ctypes.wintypes |
| 8 | |
| 9 | GENERIC_WRITE = 0x40000000 |
| 10 | CREATE_ALWAYS = 0x00000002 |
| 11 | FILE_ATTRIBUTE_NORMAL = 0x00000080 |
| 12 | LOCKFILE_EXCLUSIVE_LOCK = 0x00000002 |
| 13 | LOCKFILE_FAIL_IMMEDIATELY = 0x00000001 |
| 14 | |
| 15 | |
| 16 | class Overlapped(ctypes.Structure): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 17 | """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 Sokcevic | 14a83ae | 2020-05-21 01:36:34 +0000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | # https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew |
| 27 | CreateFileW = ctypes.windll.kernel32.CreateFileW |
| 28 | CreateFileW.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 | ] |
| 37 | CreateFileW.restype = ctypes.wintypes.HANDLE |
| 38 | |
| 39 | # https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle |
| 40 | CloseHandle = ctypes.windll.kernel32.CloseHandle |
| 41 | CloseHandle.argtypes = [ |
| 42 | ctypes.wintypes.HANDLE, # hFile |
| 43 | ] |
| 44 | CloseHandle.restype = ctypes.wintypes.BOOL |
| 45 | |
| 46 | # https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex |
| 47 | LockFileEx = ctypes.windll.kernel32.LockFileEx |
| 48 | LockFileEx.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 | ] |
| 56 | LockFileEx.restype = ctypes.wintypes.BOOL |
| 57 | |
| 58 | # Commonly used functions are listed here so callers don't need to import |
| 59 | # ctypes. |
| 60 | GetLastError = ctypes.GetLastError |
| 61 | Handle = ctypes.wintypes.HANDLE |