1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 """
32 Common definitions.
33 """
34
35
36
37
38 __revision__ = "$Id: defines.py 1299 2013-12-20 09:30:55Z qvasimodo $"
39
40 import ctypes
41 import functools
42
43
44
45
46 addressof = ctypes.addressof
47 sizeof = ctypes.sizeof
48 SIZEOF = ctypes.sizeof
49 POINTER = ctypes.POINTER
50 Structure = ctypes.Structure
51 Union = ctypes.Union
52 WINFUNCTYPE = ctypes.WINFUNCTYPE
53 windll = ctypes.windll
54
55
56
57 try:
58 ctypes.c_void_p(ctypes.byref(ctypes.c_char()))
59 byref = ctypes.byref
60 except TypeError:
61 byref = ctypes.pointer
62
63
64
65
66
67
68
69 WIN32_VERBOSE_MODE = False
70
71 if WIN32_VERBOSE_MODE:
78
87
90 self.__dllname = dllname
91 self.__funcname = funcname
92 self.__func = getattr(getattr(ctypes.windll, dllname), funcname)
93
95 try:
96 value = getattr(self, attribute)
97 setattr(self.__func, attribute, value)
98 except AttributeError:
99 try:
100 delattr(self.__func, attribute)
101 except AttributeError:
102 pass
103
105 self.__copy_attribute('argtypes')
106 self.__copy_attribute('restype')
107 self.__copy_attribute('errcheck')
108 print "-"*10
109 print "%s ! %s %r" % (self.__dllname, self.__funcname, argv)
110 retval = self.__func(*argv)
111 print "== %r" % (retval,)
112 return retval
113
114 windll = WinDllHook()
115
116
117
118 _all = None
119 _all = set(vars().keys())
120
121
122 -def RaiseIfZero(result, func = None, arguments = ()):
123 """
124 Error checking for most Win32 API calls.
125
126 The function is assumed to return an integer, which is C{0} on error.
127 In that case the C{WindowsError} exception is raised.
128 """
129 if not result:
130 raise ctypes.WinError()
131 return result
132
134 """
135 Error checking for some odd Win32 API calls.
136
137 The function is assumed to return an integer, which is zero on success.
138 If the return value is nonzero the C{WindowsError} exception is raised.
139
140 This is mostly useful for free() like functions, where the return value is
141 the pointer to the memory block on failure or a C{NULL} pointer on success.
142 """
143 if result:
144 raise ctypes.WinError()
145 return result
146
148 """
149 Error checking for Win32 Registry API calls.
150
151 The function is assumed to return a Win32 error code. If the code is not
152 C{ERROR_SUCCESS} then a C{WindowsError} exception is raised.
153 """
154 if result != ERROR_SUCCESS:
155 raise ctypes.WinError(result)
156 return result
157
159 """
160 Decorator that guesses the correct version (A or W) to call
161 based on the types of the strings passed as parameters.
162
163 Calls the B{ANSI} version if the only string types are ANSI.
164
165 Calls the B{Unicode} version if Unicode or mixed string types are passed.
166
167 The default if no string arguments are passed depends on the value of the
168 L{t_default} class variable.
169
170 @type fn_ansi: function
171 @ivar fn_ansi: ANSI version of the API function to call.
172 @type fn_unicode: function
173 @ivar fn_unicode: Unicode (wide) version of the API function to call.
174
175 @type t_default: type
176 @cvar t_default: Default string type to use.
177 Possible values are:
178 - type('') for ANSI
179 - type(u'') for Unicode
180 """
181
182
183 t_ansi = type('')
184 t_unicode = type(u'')
185
186
187 t_default = t_ansi
188
189 - def __init__(self, fn_ansi, fn_unicode):
190 """
191 @type fn_ansi: function
192 @param fn_ansi: ANSI version of the API function to call.
193 @type fn_unicode: function
194 @param fn_unicode: Unicode (wide) version of the API function to call.
195 """
196 self.fn_ansi = fn_ansi
197 self.fn_unicode = fn_unicode
198
199
200 try:
201 self.__name__ = self.fn_ansi.__name__[:-1]
202 except AttributeError:
203 pass
204 try:
205 self.__module__ = self.fn_ansi.__module__
206 except AttributeError:
207 pass
208 try:
209 self.__doc__ = self.fn_ansi.__doc__
210 except AttributeError:
211 pass
212
214
215
216 t_ansi = self.t_ansi
217
218
219 v_types = [ type(item) for item in argv ]
220 v_types.extend( [ type(value) for (key, value) in argd.iteritems() ] )
221
222
223 if self.t_default == t_ansi:
224 fn = self.fn_ansi
225 else:
226 fn = self.fn_unicode
227
228
229 if self.t_unicode in v_types:
230
231
232
233 if t_ansi in v_types:
234 argv = list(argv)
235 for index in xrange(len(argv)):
236 if v_types[index] == t_ansi:
237 argv[index] = unicode(argv[index])
238 for (key, value) in argd.items():
239 if type(value) == t_ansi:
240 argd[key] = unicode(value)
241
242
243 fn = self.fn_unicode
244
245
246
247 elif t_ansi in v_types:
248
249
250 fn = self.fn_ansi
251
252
253 return fn(*argv, **argd)
254
256 """
257 Decorator that uses the default version (A or W) to call
258 based on the configuration of the L{GuessStringType} decorator.
259
260 @see: L{GuessStringType.t_default}
261
262 @type fn_ansi: function
263 @ivar fn_ansi: ANSI version of the API function to call.
264 @type fn_unicode: function
265 @ivar fn_unicode: Unicode (wide) version of the API function to call.
266 """
267
268 - def __init__(self, fn_ansi, fn_unicode):
269 """
270 @type fn_ansi: function
271 @param fn_ansi: ANSI version of the API function to call.
272 @type fn_unicode: function
273 @param fn_unicode: Unicode (wide) version of the API function to call.
274 """
275 self.fn_ansi = fn_ansi
276 self.fn_unicode = fn_unicode
277
278
279 try:
280 self.__name__ = self.fn_ansi.__name__[:-1]
281 except AttributeError:
282 pass
283 try:
284 self.__module__ = self.fn_ansi.__module__
285 except AttributeError:
286 pass
287 try:
288 self.__doc__ = self.fn_ansi.__doc__
289 except AttributeError:
290 pass
291
293
294
295 if GuessStringType.t_default == GuessStringType.t_ansi:
296 fn = self.fn_ansi
297 else:
298 fn = self.fn_unicode
299
300
301 return fn(*argv, **argd)
302
304 """
305 Decorator that generates an ANSI version of a Unicode (wide) only API call.
306
307 @type fn: callable
308 @param fn: Unicode (wide) version of the API function to call.
309 """
310 @functools.wraps(fn)
311 def wrapper(*argv, **argd):
312 t_ansi = GuessStringType.t_ansi
313 t_unicode = GuessStringType.t_unicode
314 v_types = [ type(item) for item in argv ]
315 v_types.extend( [ type(value) for (key, value) in argd.iteritems() ] )
316 if t_ansi in v_types:
317 argv = list(argv)
318 for index in xrange(len(argv)):
319 if v_types[index] == t_ansi:
320 argv[index] = t_unicode(argv[index])
321 for key, value in argd.items():
322 if type(value) == t_ansi:
323 argd[key] = t_unicode(value)
324 return fn(*argv, **argd)
325 return wrapper
326
328 """
329 Decorator that generates a Unicode (wide) version of an ANSI only API call.
330
331 @type fn: callable
332 @param fn: ANSI version of the API function to call.
333 """
334 @functools.wraps(fn)
335 def wrapper(*argv, **argd):
336 t_ansi = GuessStringType.t_ansi
337 t_unicode = GuessStringType.t_unicode
338 v_types = [ type(item) for item in argv ]
339 v_types.extend( [ type(value) for (key, value) in argd.iteritems() ] )
340 if t_unicode in v_types:
341 argv = list(argv)
342 for index in xrange(len(argv)):
343 if v_types[index] == t_unicode:
344 argv[index] = t_ansi(argv[index])
345 for key, value in argd.items():
346 if type(value) == t_unicode:
347 argd[key] = t_ansi(value)
348 return fn(*argv, **argd)
349 return wrapper
350
351
352
353
354
355 LPVOID = ctypes.c_void_p
356 CHAR = ctypes.c_char
357 WCHAR = ctypes.c_wchar
358 BYTE = ctypes.c_ubyte
359 SBYTE = ctypes.c_byte
360 WORD = ctypes.c_uint16
361 SWORD = ctypes.c_int16
362 DWORD = ctypes.c_uint32
363 SDWORD = ctypes.c_int32
364 QWORD = ctypes.c_uint64
365 SQWORD = ctypes.c_int64
366 SHORT = ctypes.c_short
367 USHORT = ctypes.c_ushort
368 INT = ctypes.c_int
369 UINT = ctypes.c_uint
370 LONG = ctypes.c_long
371 ULONG = ctypes.c_ulong
372 LONGLONG = ctypes.c_int64
373 ULONGLONG = ctypes.c_uint64
374 LPSTR = ctypes.c_char_p
375 LPWSTR = ctypes.c_wchar_p
376 INT8 = ctypes.c_int8
377 INT16 = ctypes.c_int16
378 INT32 = ctypes.c_int32
379 INT64 = ctypes.c_int64
380 UINT8 = ctypes.c_uint8
381 UINT16 = ctypes.c_uint16
382 UINT32 = ctypes.c_uint32
383 UINT64 = ctypes.c_uint64
384 LONG32 = ctypes.c_int32
385 LONG64 = ctypes.c_int64
386 ULONG32 = ctypes.c_uint32
387 ULONG64 = ctypes.c_uint64
388 DWORD32 = ctypes.c_uint32
389 DWORD64 = ctypes.c_uint64
390 BOOL = ctypes.c_int
391 FLOAT = ctypes.c_float
392
393
394 try:
395 SIZE_T = ctypes.c_size_t
396 SSIZE_T = ctypes.c_ssize_t
397 except AttributeError:
398
399 SIZE_T = {1:BYTE, 2:WORD, 4:DWORD, 8:QWORD}[sizeof(LPVOID)]
400 SSIZE_T = {1:SBYTE, 2:SWORD, 4:SDWORD, 8:SQWORD}[sizeof(LPVOID)]
401 PSIZE_T = POINTER(SIZE_T)
402
403
404 DWORD_PTR = SIZE_T
405 ULONG_PTR = SIZE_T
406 LONG_PTR = SIZE_T
407
408
409 PVOID = LPVOID
410 PPVOID = POINTER(PVOID)
411 PSTR = LPSTR
412 PWSTR = LPWSTR
413 PCHAR = LPSTR
414 PWCHAR = LPWSTR
415 LPBYTE = POINTER(BYTE)
416 LPSBYTE = POINTER(SBYTE)
417 LPWORD = POINTER(WORD)
418 LPSWORD = POINTER(SWORD)
419 LPDWORD = POINTER(DWORD)
420 LPSDWORD = POINTER(SDWORD)
421 LPULONG = POINTER(ULONG)
422 LPLONG = POINTER(LONG)
423 PDWORD = LPDWORD
424 PDWORD_PTR = POINTER(DWORD_PTR)
425 PULONG = LPULONG
426 PLONG = LPLONG
427 CCHAR = CHAR
428 BOOLEAN = BYTE
429 PBOOL = POINTER(BOOL)
430 LPBOOL = PBOOL
431 TCHAR = CHAR
432 UCHAR = BYTE
433 DWORDLONG = ULONGLONG
434 LPDWORD32 = POINTER(DWORD32)
435 LPULONG32 = POINTER(ULONG32)
436 LPDWORD64 = POINTER(DWORD64)
437 LPULONG64 = POINTER(ULONG64)
438 PDWORD32 = LPDWORD32
439 PULONG32 = LPULONG32
440 PDWORD64 = LPDWORD64
441 PULONG64 = LPULONG64
442 ATOM = WORD
443 HANDLE = LPVOID
444 PHANDLE = POINTER(HANDLE)
445 LPHANDLE = PHANDLE
446 HMODULE = HANDLE
447 HINSTANCE = HANDLE
448 HTASK = HANDLE
449 HKEY = HANDLE
450 PHKEY = POINTER(HKEY)
451 HDESK = HANDLE
452 HRSRC = HANDLE
453 HSTR = HANDLE
454 HWINSTA = HANDLE
455 HKL = HANDLE
456 HDWP = HANDLE
457 HFILE = HANDLE
458 HRESULT = LONG
459 HGLOBAL = HANDLE
460 HLOCAL = HANDLE
461 HGDIOBJ = HANDLE
462 HDC = HGDIOBJ
463 HRGN = HGDIOBJ
464 HBITMAP = HGDIOBJ
465 HPALETTE = HGDIOBJ
466 HPEN = HGDIOBJ
467 HBRUSH = HGDIOBJ
468 HMF = HGDIOBJ
469 HEMF = HGDIOBJ
470 HENHMETAFILE = HGDIOBJ
471 HMETAFILE = HGDIOBJ
472 HMETAFILEPICT = HGDIOBJ
473 HWND = HANDLE
474 NTSTATUS = LONG
475 PNTSTATUS = POINTER(NTSTATUS)
476 KAFFINITY = ULONG_PTR
477 RVA = DWORD
478 RVA64 = QWORD
479 WPARAM = DWORD
480 LPARAM = LPVOID
481 LRESULT = LPVOID
482 ACCESS_MASK = DWORD
483 REGSAM = ACCESS_MASK
484 PACCESS_MASK = POINTER(ACCESS_MASK)
485 PREGSAM = POINTER(REGSAM)
486
487
488 PSID = PVOID
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509 -class FLOAT128 (Structure):
510 _fields_ = [
511 ("LowPart", QWORD),
512 ("HighPart", QWORD),
513 ]
514 PFLOAT128 = POINTER(FLOAT128)
515
516
517
518
519
520 -class M128A(Structure):
521 _fields_ = [
522 ("Low", ULONGLONG),
523 ("High", LONGLONG),
524 ]
525 PM128A = POINTER(M128A)
526
527
528
529 NULL = None
530 INFINITE = -1
531 TRUE = 1
532 FALSE = 0
533
534
535 ANYSIZE_ARRAY = 1
536
537
538 try:
539 INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value
540 except TypeError:
541 if sizeof(ctypes.c_void_p) == 4:
542 INVALID_HANDLE_VALUE = 0xFFFFFFFF
543 elif sizeof(ctypes.c_void_p) == 8:
544 INVALID_HANDLE_VALUE = 0xFFFFFFFFFFFFFFFF
545 else:
546 raise
547
548 MAX_MODULE_NAME32 = 255
549 MAX_PATH = 260
550
551
552
553
554
555 ERROR_SUCCESS = 0
556 ERROR_INVALID_FUNCTION = 1
557 ERROR_FILE_NOT_FOUND = 2
558 ERROR_PATH_NOT_FOUND = 3
559 ERROR_ACCESS_DENIED = 5
560 ERROR_INVALID_HANDLE = 6
561 ERROR_NOT_ENOUGH_MEMORY = 8
562 ERROR_INVALID_DRIVE = 15
563 ERROR_NO_MORE_FILES = 18
564 ERROR_BAD_LENGTH = 24
565 ERROR_HANDLE_EOF = 38
566 ERROR_HANDLE_DISK_FULL = 39
567 ERROR_NOT_SUPPORTED = 50
568 ERROR_FILE_EXISTS = 80
569 ERROR_INVALID_PARAMETER = 87
570 ERROR_BUFFER_OVERFLOW = 111
571 ERROR_DISK_FULL = 112
572 ERROR_CALL_NOT_IMPLEMENTED = 120
573 ERROR_SEM_TIMEOUT = 121
574 ERROR_INSUFFICIENT_BUFFER = 122
575 ERROR_INVALID_NAME = 123
576 ERROR_MOD_NOT_FOUND = 126
577 ERROR_PROC_NOT_FOUND = 127
578 ERROR_DIR_NOT_EMPTY = 145
579 ERROR_BAD_THREADID_ADDR = 159
580 ERROR_BAD_ARGUMENTS = 160
581 ERROR_BAD_PATHNAME = 161
582 ERROR_ALREADY_EXISTS = 183
583 ERROR_INVALID_FLAG_NUMBER = 186
584 ERROR_ENVVAR_NOT_FOUND = 203
585 ERROR_FILENAME_EXCED_RANGE = 206
586 ERROR_MORE_DATA = 234
587
588 WAIT_TIMEOUT = 258
589
590 ERROR_NO_MORE_ITEMS = 259
591 ERROR_PARTIAL_COPY = 299
592 ERROR_INVALID_ADDRESS = 487
593 ERROR_THREAD_NOT_IN_PROCESS = 566
594 ERROR_CONTROL_C_EXIT = 572
595 ERROR_UNHANDLED_EXCEPTION = 574
596 ERROR_ASSERTION_FAILURE = 668
597 ERROR_WOW_ASSERTION = 670
598
599 ERROR_DBG_EXCEPTION_NOT_HANDLED = 688
600 ERROR_DBG_REPLY_LATER = 689
601 ERROR_DBG_UNABLE_TO_PROVIDE_HANDLE = 690
602 ERROR_DBG_TERMINATE_THREAD = 691
603 ERROR_DBG_TERMINATE_PROCESS = 692
604 ERROR_DBG_CONTROL_C = 693
605 ERROR_DBG_PRINTEXCEPTION_C = 694
606 ERROR_DBG_RIPEXCEPTION = 695
607 ERROR_DBG_CONTROL_BREAK = 696
608 ERROR_DBG_COMMAND_EXCEPTION = 697
609 ERROR_DBG_EXCEPTION_HANDLED = 766
610 ERROR_DBG_CONTINUE = 767
611
612 ERROR_ELEVATION_REQUIRED = 740
613 ERROR_NOACCESS = 998
614
615 ERROR_CIRCULAR_DEPENDENCY = 1059
616 ERROR_SERVICE_DOES_NOT_EXIST = 1060
617 ERROR_SERVICE_CANNOT_ACCEPT_CTRL = 1061
618 ERROR_SERVICE_NOT_ACTIVE = 1062
619 ERROR_FAILED_SERVICE_CONTROLLER_CONNECT = 1063
620 ERROR_EXCEPTION_IN_SERVICE = 1064
621 ERROR_DATABASE_DOES_NOT_EXIST = 1065
622 ERROR_SERVICE_SPECIFIC_ERROR = 1066
623 ERROR_PROCESS_ABORTED = 1067
624 ERROR_SERVICE_DEPENDENCY_FAIL = 1068
625 ERROR_SERVICE_LOGON_FAILED = 1069
626 ERROR_SERVICE_START_HANG = 1070
627 ERROR_INVALID_SERVICE_LOCK = 1071
628 ERROR_SERVICE_MARKED_FOR_DELETE = 1072
629 ERROR_SERVICE_EXISTS = 1073
630 ERROR_ALREADY_RUNNING_LKG = 1074
631 ERROR_SERVICE_DEPENDENCY_DELETED = 1075
632 ERROR_BOOT_ALREADY_ACCEPTED = 1076
633 ERROR_SERVICE_NEVER_STARTED = 1077
634 ERROR_DUPLICATE_SERVICE_NAME = 1078
635 ERROR_DIFFERENT_SERVICE_ACCOUNT = 1079
636 ERROR_CANNOT_DETECT_DRIVER_FAILURE = 1080
637 ERROR_CANNOT_DETECT_PROCESS_ABORT = 1081
638 ERROR_NO_RECOVERY_PROGRAM = 1082
639 ERROR_SERVICE_NOT_IN_EXE = 1083
640 ERROR_NOT_SAFEBOOT_SERVICE = 1084
641
642 ERROR_DEBUGGER_INACTIVE = 1284
643
644 ERROR_PRIVILEGE_NOT_HELD = 1314
645
646 ERROR_NONE_MAPPED = 1332
647
648 RPC_S_SERVER_UNAVAILABLE = 1722
649
650
651 DELETE = 0x00010000L
652 READ_CONTROL = 0x00020000L
653 WRITE_DAC = 0x00040000L
654 WRITE_OWNER = 0x00080000L
655 SYNCHRONIZE = 0x00100000L
656 STANDARD_RIGHTS_REQUIRED = 0x000F0000L
657 STANDARD_RIGHTS_READ = READ_CONTROL
658 STANDARD_RIGHTS_WRITE = READ_CONTROL
659 STANDARD_RIGHTS_EXECUTE = READ_CONTROL
660 STANDARD_RIGHTS_ALL = 0x001F0000L
661 SPECIFIC_RIGHTS_ALL = 0x0000FFFFL
662
663
664
665
666
667
668
669
670
671
672
673 -class UNICODE_STRING(Structure):
674 _fields_ = [
675 ("Length", USHORT),
676 ("MaximumLength", USHORT),
677 ("Buffer", PVOID),
678 ]
679
680
681
682
683
684
685
686
687
688 -class GUID(Structure):
689 _fields_ = [
690 ("Data1", DWORD),
691 ("Data2", WORD),
692 ("Data3", WORD),
693 ("Data4", BYTE * 8),
694 ]
695
696
697
698
699
700
701
702 -class LIST_ENTRY(Structure):
703 _fields_ = [
704 ("Flink", PVOID),
705 ("Blink", PVOID),
706 ]
707
708
709
710 _all = set(vars().keys()).difference(_all)
711
712
713
714