win32imports.py 2.2 KB

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