win32.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. from ctypes import *
  2. from ctypes.wintypes import HANDLE
  3. from ctypes.wintypes import BOOL
  4. from ctypes.wintypes import LPCWSTR
  5. _stdcall_libraries = {}
  6. _stdcall_libraries['kernel32'] = WinDLL('kernel32')
  7. from ctypes.wintypes import DWORD
  8. from ctypes.wintypes import WORD
  9. from ctypes.wintypes import BYTE
  10. INVALID_HANDLE_VALUE = HANDLE(-1).value
  11. # some details of the windows API differ between 32 and 64 bit systems..
  12. def is_64bit():
  13. """Returns true when running on a 64 bit system"""
  14. return sizeof(c_ulong) != sizeof(c_void_p)
  15. # ULONG_PTR is a an ordinary number, not a pointer and contrary to the name it
  16. # is either 32 or 64 bits, depending on the type of windows...
  17. # so test if this a 32 bit windows...
  18. if is_64bit():
  19. # assume 64 bits
  20. ULONG_PTR = c_int64
  21. else:
  22. # 32 bits
  23. ULONG_PTR = c_ulong
  24. class _SECURITY_ATTRIBUTES(Structure):
  25. pass
  26. LPSECURITY_ATTRIBUTES = POINTER(_SECURITY_ATTRIBUTES)
  27. try:
  28. CreateEventW = _stdcall_libraries['kernel32'].CreateEventW
  29. except AttributeError:
  30. # Fallback to non wide char version for old OS...
  31. from ctypes.wintypes import LPCSTR
  32. CreateEventA = _stdcall_libraries['kernel32'].CreateEventA
  33. CreateEventA.restype = HANDLE
  34. CreateEventA.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCSTR]
  35. CreateEvent=CreateEventA
  36. CreateFileA = _stdcall_libraries['kernel32'].CreateFileA
  37. CreateFileA.restype = HANDLE
  38. CreateFileA.argtypes = [LPCSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
  39. CreateFile = CreateFileA
  40. else:
  41. CreateEventW.restype = HANDLE
  42. CreateEventW.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
  43. CreateEvent = CreateEventW # alias
  44. CreateFileW = _stdcall_libraries['kernel32'].CreateFileW
  45. CreateFileW.restype = HANDLE
  46. CreateFileW.argtypes = [LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE]
  47. CreateFile = CreateFileW # alias
  48. class _OVERLAPPED(Structure):
  49. pass
  50. OVERLAPPED = _OVERLAPPED
  51. class _COMSTAT(Structure):
  52. pass
  53. COMSTAT = _COMSTAT
  54. class _DCB(Structure):
  55. pass
  56. DCB = _DCB
  57. class _COMMTIMEOUTS(Structure):
  58. pass
  59. COMMTIMEOUTS = _COMMTIMEOUTS
  60. GetLastError = _stdcall_libraries['kernel32'].GetLastError
  61. GetLastError.restype = DWORD
  62. GetLastError.argtypes = []
  63. LPOVERLAPPED = POINTER(_OVERLAPPED)
  64. LPDWORD = POINTER(DWORD)
  65. GetOverlappedResult = _stdcall_libraries['kernel32'].GetOverlappedResult
  66. GetOverlappedResult.restype = BOOL
  67. GetOverlappedResult.argtypes = [HANDLE, LPOVERLAPPED, LPDWORD, BOOL]
  68. ResetEvent = _stdcall_libraries['kernel32'].ResetEvent
  69. ResetEvent.restype = BOOL
  70. ResetEvent.argtypes = [HANDLE]
  71. LPCVOID = c_void_p
  72. WriteFile = _stdcall_libraries['kernel32'].WriteFile
  73. WriteFile.restype = BOOL
  74. WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, LPOVERLAPPED]
  75. LPVOID = c_void_p
  76. ReadFile = _stdcall_libraries['kernel32'].ReadFile
  77. ReadFile.restype = BOOL
  78. ReadFile.argtypes = [HANDLE, LPVOID, DWORD, LPDWORD, LPOVERLAPPED]
  79. CloseHandle = _stdcall_libraries['kernel32'].CloseHandle
  80. CloseHandle.restype = BOOL
  81. CloseHandle.argtypes = [HANDLE]
  82. ClearCommBreak = _stdcall_libraries['kernel32'].ClearCommBreak
  83. ClearCommBreak.restype = BOOL
  84. ClearCommBreak.argtypes = [HANDLE]
  85. LPCOMSTAT = POINTER(_COMSTAT)
  86. ClearCommError = _stdcall_libraries['kernel32'].ClearCommError
  87. ClearCommError.restype = BOOL
  88. ClearCommError.argtypes = [HANDLE, LPDWORD, LPCOMSTAT]
  89. SetupComm = _stdcall_libraries['kernel32'].SetupComm
  90. SetupComm.restype = BOOL
  91. SetupComm.argtypes = [HANDLE, DWORD, DWORD]
  92. EscapeCommFunction = _stdcall_libraries['kernel32'].EscapeCommFunction
  93. EscapeCommFunction.restype = BOOL
  94. EscapeCommFunction.argtypes = [HANDLE, DWORD]
  95. GetCommModemStatus = _stdcall_libraries['kernel32'].GetCommModemStatus
  96. GetCommModemStatus.restype = BOOL
  97. GetCommModemStatus.argtypes = [HANDLE, LPDWORD]
  98. LPDCB = POINTER(_DCB)
  99. GetCommState = _stdcall_libraries['kernel32'].GetCommState
  100. GetCommState.restype = BOOL
  101. GetCommState.argtypes = [HANDLE, LPDCB]
  102. LPCOMMTIMEOUTS = POINTER(_COMMTIMEOUTS)
  103. GetCommTimeouts = _stdcall_libraries['kernel32'].GetCommTimeouts
  104. GetCommTimeouts.restype = BOOL
  105. GetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
  106. PurgeComm = _stdcall_libraries['kernel32'].PurgeComm
  107. PurgeComm.restype = BOOL
  108. PurgeComm.argtypes = [HANDLE, DWORD]
  109. SetCommBreak = _stdcall_libraries['kernel32'].SetCommBreak
  110. SetCommBreak.restype = BOOL
  111. SetCommBreak.argtypes = [HANDLE]
  112. SetCommMask = _stdcall_libraries['kernel32'].SetCommMask
  113. SetCommMask.restype = BOOL
  114. SetCommMask.argtypes = [HANDLE, DWORD]
  115. SetCommState = _stdcall_libraries['kernel32'].SetCommState
  116. SetCommState.restype = BOOL
  117. SetCommState.argtypes = [HANDLE, LPDCB]
  118. SetCommTimeouts = _stdcall_libraries['kernel32'].SetCommTimeouts
  119. SetCommTimeouts.restype = BOOL
  120. SetCommTimeouts.argtypes = [HANDLE, LPCOMMTIMEOUTS]
  121. WaitForSingleObject = _stdcall_libraries['kernel32'].WaitForSingleObject
  122. WaitForSingleObject.restype = DWORD
  123. WaitForSingleObject.argtypes = [HANDLE, DWORD]
  124. ONESTOPBIT = 0 # Variable c_int
  125. TWOSTOPBITS = 2 # Variable c_int
  126. ONE5STOPBITS = 1
  127. NOPARITY = 0 # Variable c_int
  128. ODDPARITY = 1 # Variable c_int
  129. EVENPARITY = 2 # Variable c_int
  130. MARKPARITY = 3
  131. SPACEPARITY = 4
  132. RTS_CONTROL_HANDSHAKE = 2 # Variable c_int
  133. RTS_CONTROL_DISABLE = 0 # Variable c_int
  134. RTS_CONTROL_ENABLE = 1 # Variable c_int
  135. RTS_CONTROL_TOGGLE = 3 # Variable c_int
  136. SETRTS = 3
  137. CLRRTS = 4
  138. DTR_CONTROL_HANDSHAKE = 2 # Variable c_int
  139. DTR_CONTROL_DISABLE = 0 # Variable c_int
  140. DTR_CONTROL_ENABLE = 1 # Variable c_int
  141. SETDTR = 5
  142. CLRDTR = 6
  143. MS_DSR_ON = 32 # Variable c_ulong
  144. EV_RING = 256 # Variable c_int
  145. EV_PERR = 512 # Variable c_int
  146. EV_ERR = 128 # Variable c_int
  147. SETXOFF = 1 # Variable c_int
  148. EV_RXCHAR = 1 # Variable c_int
  149. GENERIC_WRITE = 1073741824 # Variable c_long
  150. PURGE_TXCLEAR = 4 # Variable c_int
  151. FILE_FLAG_OVERLAPPED = 1073741824 # Variable c_int
  152. EV_DSR = 16 # Variable c_int
  153. MAXDWORD = 4294967295L # Variable c_uint
  154. EV_RLSD = 32 # Variable c_int
  155. ERROR_IO_PENDING = 997 # Variable c_long
  156. MS_CTS_ON = 16 # Variable c_ulong
  157. EV_EVENT1 = 2048 # Variable c_int
  158. EV_RX80FULL = 1024 # Variable c_int
  159. PURGE_RXABORT = 2 # Variable c_int
  160. FILE_ATTRIBUTE_NORMAL = 128 # Variable c_int
  161. PURGE_TXABORT = 1 # Variable c_int
  162. SETXON = 2 # Variable c_int
  163. OPEN_EXISTING = 3 # Variable c_int
  164. MS_RING_ON = 64 # Variable c_ulong
  165. EV_TXEMPTY = 4 # Variable c_int
  166. EV_RXFLAG = 2 # Variable c_int
  167. MS_RLSD_ON = 128 # Variable c_ulong
  168. GENERIC_READ = 2147483648L # Variable c_ulong
  169. EV_EVENT2 = 4096 # Variable c_int
  170. EV_CTS = 8 # Variable c_int
  171. EV_BREAK = 64 # Variable c_int
  172. PURGE_RXCLEAR = 8 # Variable c_int
  173. INFINITE = 0xFFFFFFFFL
  174. class N11_OVERLAPPED4DOLLAR_48E(Union):
  175. pass
  176. class N11_OVERLAPPED4DOLLAR_484DOLLAR_49E(Structure):
  177. pass
  178. N11_OVERLAPPED4DOLLAR_484DOLLAR_49E._fields_ = [
  179. ('Offset', DWORD),
  180. ('OffsetHigh', DWORD),
  181. ]
  182. PVOID = c_void_p
  183. N11_OVERLAPPED4DOLLAR_48E._anonymous_ = ['_0']
  184. N11_OVERLAPPED4DOLLAR_48E._fields_ = [
  185. ('_0', N11_OVERLAPPED4DOLLAR_484DOLLAR_49E),
  186. ('Pointer', PVOID),
  187. ]
  188. _OVERLAPPED._anonymous_ = ['_0']
  189. _OVERLAPPED._fields_ = [
  190. ('Internal', ULONG_PTR),
  191. ('InternalHigh', ULONG_PTR),
  192. ('_0', N11_OVERLAPPED4DOLLAR_48E),
  193. ('hEvent', HANDLE),
  194. ]
  195. _SECURITY_ATTRIBUTES._fields_ = [
  196. ('nLength', DWORD),
  197. ('lpSecurityDescriptor', LPVOID),
  198. ('bInheritHandle', BOOL),
  199. ]
  200. _COMSTAT._fields_ = [
  201. ('fCtsHold', DWORD, 1),
  202. ('fDsrHold', DWORD, 1),
  203. ('fRlsdHold', DWORD, 1),
  204. ('fXoffHold', DWORD, 1),
  205. ('fXoffSent', DWORD, 1),
  206. ('fEof', DWORD, 1),
  207. ('fTxim', DWORD, 1),
  208. ('fReserved', DWORD, 25),
  209. ('cbInQue', DWORD),
  210. ('cbOutQue', DWORD),
  211. ]
  212. _DCB._fields_ = [
  213. ('DCBlength', DWORD),
  214. ('BaudRate', DWORD),
  215. ('fBinary', DWORD, 1),
  216. ('fParity', DWORD, 1),
  217. ('fOutxCtsFlow', DWORD, 1),
  218. ('fOutxDsrFlow', DWORD, 1),
  219. ('fDtrControl', DWORD, 2),
  220. ('fDsrSensitivity', DWORD, 1),
  221. ('fTXContinueOnXoff', DWORD, 1),
  222. ('fOutX', DWORD, 1),
  223. ('fInX', DWORD, 1),
  224. ('fErrorChar', DWORD, 1),
  225. ('fNull', DWORD, 1),
  226. ('fRtsControl', DWORD, 2),
  227. ('fAbortOnError', DWORD, 1),
  228. ('fDummy2', DWORD, 17),
  229. ('wReserved', WORD),
  230. ('XonLim', WORD),
  231. ('XoffLim', WORD),
  232. ('ByteSize', BYTE),
  233. ('Parity', BYTE),
  234. ('StopBits', BYTE),
  235. ('XonChar', c_char),
  236. ('XoffChar', c_char),
  237. ('ErrorChar', c_char),
  238. ('EofChar', c_char),
  239. ('EvtChar', c_char),
  240. ('wReserved1', WORD),
  241. ]
  242. _COMMTIMEOUTS._fields_ = [
  243. ('ReadIntervalTimeout', DWORD),
  244. ('ReadTotalTimeoutMultiplier', DWORD),
  245. ('ReadTotalTimeoutConstant', DWORD),
  246. ('WriteTotalTimeoutMultiplier', DWORD),
  247. ('WriteTotalTimeoutConstant', DWORD),
  248. ]
  249. __all__ = ['GetLastError', 'MS_CTS_ON', 'FILE_ATTRIBUTE_NORMAL',
  250. 'DTR_CONTROL_ENABLE', '_COMSTAT', 'MS_RLSD_ON',
  251. 'GetOverlappedResult', 'SETXON', 'PURGE_TXABORT',
  252. 'PurgeComm', 'N11_OVERLAPPED4DOLLAR_48E', 'EV_RING',
  253. 'ONESTOPBIT', 'SETXOFF', 'PURGE_RXABORT', 'GetCommState',
  254. 'RTS_CONTROL_ENABLE', '_DCB', 'CreateEvent',
  255. '_COMMTIMEOUTS', '_SECURITY_ATTRIBUTES', 'EV_DSR',
  256. 'EV_PERR', 'EV_RXFLAG', 'OPEN_EXISTING', 'DCB',
  257. 'FILE_FLAG_OVERLAPPED', 'EV_CTS', 'SetupComm',
  258. 'LPOVERLAPPED', 'EV_TXEMPTY', 'ClearCommBreak',
  259. 'LPSECURITY_ATTRIBUTES', 'SetCommBreak', 'SetCommTimeouts',
  260. 'COMMTIMEOUTS', 'ODDPARITY', 'EV_RLSD',
  261. 'GetCommModemStatus', 'EV_EVENT2', 'PURGE_TXCLEAR',
  262. 'EV_BREAK', 'EVENPARITY', 'LPCVOID', 'COMSTAT', 'ReadFile',
  263. 'PVOID', '_OVERLAPPED', 'WriteFile', 'GetCommTimeouts',
  264. 'ResetEvent', 'EV_RXCHAR', 'LPCOMSTAT', 'ClearCommError',
  265. 'ERROR_IO_PENDING', 'EscapeCommFunction', 'GENERIC_READ',
  266. 'RTS_CONTROL_HANDSHAKE', 'OVERLAPPED',
  267. 'DTR_CONTROL_HANDSHAKE', 'PURGE_RXCLEAR', 'GENERIC_WRITE',
  268. 'LPDCB', 'CreateEventW', 'SetCommMask', 'EV_EVENT1',
  269. 'SetCommState', 'LPVOID', 'CreateFileW', 'LPDWORD',
  270. 'EV_RX80FULL', 'TWOSTOPBITS', 'LPCOMMTIMEOUTS', 'MAXDWORD',
  271. 'MS_DSR_ON', 'MS_RING_ON',
  272. 'N11_OVERLAPPED4DOLLAR_484DOLLAR_49E', 'EV_ERR',
  273. 'ULONG_PTR', 'CreateFile', 'NOPARITY', 'CloseHandle']