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 Debugging API wrappers in ctypes.
30
31 @see: U{http://apps.sourceforge.net/trac/winappdbg/wiki/Win32APIWrappers}
32 """
33
34 __revision__ = "$Id: defines.py 478 2009-11-28 04:19:09Z qvasimodo $"
35
36 import time
37 import struct
38 import ctypes
39
40 sizeof = ctypes.sizeof
41 POINTER = ctypes.POINTER
42 Structure = ctypes.Structure
43 Union = ctypes.Union
44
45 try:
46 from ctypes import windll
47 except ImportError:
52 raise ctypes.WinError(50)
53 windll = FakeWinDll()
54
55 try:
56 WINFUNCTYPE = ctypes.WINFUNCTYPE
57 except AttributeError:
60 self.restype = restype
61 self.argtypes = argtypes
63 return ctypes.WINFUNCTYPE(self.restype, *self.argtypes)(*argv)
64
65 try:
66 callable
67 except NameError:
69 return hasattr(obj, '__call__')
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
113 """
114 Error checking for most Win32 API calls.
115
116 The function is assumed to return an integer, which is C{0} on error.
117 In that case the C{WindowsError} exception is raised.
118 """
119 if not result:
120 raise ctypes.WinError()
121 return result
122
124 """
125 Decorator that guesses the correct version (A or W) to call
126 based on the types of the strings passed as parameters.
127
128 Defaults to B{ANSI} if no string arguments are passed.
129
130 Defaults to B{Unicode} if mixed string types are passed.
131
132 @type fn_ansi: function
133 @ivar fn_ansi: ANSI version of the API function to call.
134 @type fn_unicode: function
135 @ivar fn_unicode: Unicode (wide) version of the API function to call.
136 """
137
138 - def __init__(self, fn_ansi, fn_unicode):
139 """
140 @type fn_ansi: function
141 @param fn_ansi: ANSI version of the API function to call.
142 @type fn_unicode: function
143 @param fn_unicode: Unicode (wide) version of the API function to call.
144 """
145 self.fn_ansi = fn_ansi
146 self.fn_unicode = fn_unicode
147
149 guessed = None
150 t_ansi = type('')
151 t_unicode = type(u'')
152 v_types = [ type(item) for item in argv ]
153 v_types.extend( [ type(value) for (key, value) in argd.iteritems() ] )
154 if t_unicode in v_types:
155 if t_ansi in v_types:
156 argv = list(argv)
157 for index in xrange(len(argv)):
158 if v_types[index] == t_ansi:
159 argv[index] = unicode(argv[index])
160 for key, value in argd.items():
161 if type(value) == t_ansi:
162 argd[key] = unicode(value)
163 return self.fn_unicode(*argv, **argd)
164 return self.fn_ansi(*argv, **argd)
165
167 """
168 Decorator that generates an ANSI version of a Unicode (wide) only API call.
169
170 @type fn: function
171 @ivar fn: Unicode (wide) version of the API function to call.
172 """
173
175 """
176 @type fn: function
177 @param fn: Unicode (wide) version of the API function to call.
178 """
179 self.fn = fn
180
182 t_ansi = type('')
183 v_types = [ type(item) for item in argv ]
184 v_types.extend( [ type(value) for (key, value) in argd.iteritems() ] )
185 if t_ansi in v_types:
186 argv = list(argv)
187 for index in xrange(len(argv)):
188 if v_types[index] == t_ansi:
189 argv[index] = unicode(argv[index])
190 for key, value in argd.items():
191 if type(value) == t_ansi:
192 argd[key] = unicode(value)
193 return self.fn(*argv, **argd)
194
195
196
197 LPVOID = ctypes.c_void_p
198 CHAR = ctypes.c_char
199 WCHAR = ctypes.c_wchar
200 BYTE = ctypes.c_ubyte
201 SBYTE = ctypes.c_byte
202 WORD = ctypes.c_ushort
203 SWORD = ctypes.c_short
204 DWORD = ctypes.c_uint
205 SDWORD = ctypes.c_int
206 QWORD = ctypes.c_ulonglong
207 SQWORD = ctypes.c_longlong
208 SHORT = ctypes.c_short
209 USHORT = ctypes.c_ushort
210 INT = ctypes.c_int
211 UINT = ctypes.c_uint
212 LONG = ctypes.c_long
213 ULONG = ctypes.c_ulong
214 LONGLONG = ctypes.c_longlong
215 ULONGLONG = ctypes.c_ulonglong
216 LPSTR = ctypes.c_char_p
217 LPWSTR = ctypes.c_wchar_p
218
219 try:
220 SIZE_T = ctypes.c_size_t
221 except AttributeError:
222
223 SIZE_T = {1:BYTE, 2:WORD, 4:DWORD, 8:QWORD}[sizeof(LPVOID)]
224 PSIZE_T = POINTER(SIZE_T)
225
226 PVOID = LPVOID
227 PPVOID = POINTER(PVOID)
228 PSTR = LPSTR
229 PWSTR = LPWSTR
230 PCHAR = LPSTR
231 PWCHAR = LPWSTR
232 LPBYTE = POINTER(BYTE)
233 LPSBYTE = POINTER(SBYTE)
234 LPWORD = POINTER(WORD)
235 LPSWORD = POINTER(SWORD)
236 LPDWORD = POINTER(DWORD)
237 LPSDWORD = POINTER(SDWORD)
238 DWORD_PTR = POINTER(DWORD)
239 ULONG_PTR = POINTER(ULONG)
240 LONG_PTR = POINTER(LONG)
241 PDWORD = DWORD_PTR
242 PULONG = ULONG_PTR
243 PLONG = LONG_PTR
244 BOOL = DWORD
245 BOOLEAN = BYTE
246 PBOOL = POINTER(BOOL)
247 LPBOOL = PBOOL
248 TCHAR = CHAR
249 UCHAR = BYTE
250 ULONG32 = DWORD
251 DWORD32 = DWORD
252 ULONG64 = QWORD
253 DWORD64 = QWORD
254 DWORDLONG = ULONGLONG
255 HANDLE = LPVOID
256 PHANDLE = POINTER(HANDLE)
257 LPHANDLE = PHANDLE
258 HMODULE = HANDLE
259 HINSTANCE = HANDLE
260 HRGN = HANDLE
261 HTASK = HANDLE
262 HKEY = HANDLE
263 HDESK = HANDLE
264 HMF = HANDLE
265 HEMF = HANDLE
266 HPEN = HANDLE
267 HRSRC = HANDLE
268 HSTR = HANDLE
269 HWINSTA = HANDLE
270 HKL = HANDLE
271 HGDIOBJ = HANDLE
272 HDWP = HANDLE
273 HFILE = HANDLE
274 HRESULT = LONG
275 HGLOBAL = HANDLE
276 HLOCAL = HANDLE
277 HBITMAP = HANDLE
278 HPALETTE = HANDLE
279 HENHMETAFILE = HANDLE
280 HMETAFILE = HANDLE
281 HMETAFILEPICT = HANDLE
282 HWND = HANDLE
283 NTSTATUS = LONG
284 PNTSTATUS = POINTER(NTSTATUS)
285 KAFFINITY = PVOID
286 RVA = DWORD
287 RVA64 = QWORD
288 WPARAM = DWORD
289 LPARAM = LPVOID
290 LRESULT = LPVOID
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
312 _fields_ = [
313 ("LowPart", QWORD),
314 ("HighPart", QWORD),
315 ]
316 PFLOAT128 = POINTER(FLOAT128)
317
318
319
320
321
323 _fields_ = [
324 ("Low", ULONGLONG),
325 ("High", LONGLONG),
326 ]
327 PM128A = POINTER(M128A)
328
329
330
331 NULL = None
332 INFINITE = -1
333 TRUE = 1
334 FALSE = 0
335
336
337 ANYSIZE_ARRAY = 1
338
339 INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value
340
341 MAX_MODULE_NAME32 = 255
342 MAX_PATH = 260
343
344
345
346 ERROR_SUCCESS = 0
347 ERROR_INVALID_FUNCTION = 1
348 ERROR_FILE_NOT_FOUND = 2
349 ERROR_PATH_NOT_FOUND = 3
350 ERROR_ACCESS_DENIED = 5
351 ERROR_INVALID_HANDLE = 6
352 ERROR_NOT_ENOUGH_MEMORY = 8
353 ERROR_INVALID_DRIVE = 15
354 ERROR_NO_MORE_FILES = 18
355 ERROR_BAD_LENGTH = 24
356 ERROR_HANDLE_EOF = 38
357 ERROR_HANDLE_DISK_FULL = 39
358 ERROR_NOT_SUPPORTED = 50
359 ERROR_FILE_EXISTS = 80
360 ERROR_INVALID_PARAMETER = 87
361 ERROR_BUFFER_OVERFLOW = 111
362 ERROR_DISK_FULL = 112
363 ERROR_CALL_NOT_IMPLEMENTED = 120
364 ERROR_SEM_TIMEOUT = 121
365 ERROR_INSUFFICIENT_BUFFER = 122
366 ERROR_INVALID_NAME = 123
367 ERROR_MOD_NOT_FOUND = 126
368 ERROR_PROC_NOT_FOUND = 127
369 ERROR_DIR_NOT_EMPTY = 145
370 ERROR_BAD_THREADID_ADDR = 159
371 ERROR_BAD_ARGUMENTS = 160
372 ERROR_BAD_PATHNAME = 161
373 ERROR_ALREADY_EXISTS = 183
374 ERROR_INVALID_FLAG_NUMBER = 186
375 ERROR_FILENAME_EXCED_RANGE = 206
376 WAIT_TIMEOUT = 258
377 ERROR_NO_MORE_ITEMS = 259
378 ERROR_PARTIAL_COPY = 299
379 ERROR_INVALID_ADDRESS = 487
380 ERROR_THREAD_NOT_IN_PROCESS = 566
381 ERROR_CONTROL_C_EXIT = 572
382 ERROR_UNHANDLED_EXCEPTION = 574
383 ERROR_ASSERTION_FAILURE = 668
384 ERROR_WOW_ASSERTION = 670
385
386 ERROR_DBG_EXCEPTION_NOT_HANDLED = 688
387 ERROR_DBG_REPLY_LATER = 689
388 ERROR_DBG_UNABLE_TO_PROVIDE_HANDLE = 690
389 ERROR_DBG_TERMINATE_THREAD = 691
390 ERROR_DBG_TERMINATE_PROCESS = 692
391 ERROR_DBG_CONTROL_C = 693
392 ERROR_DBG_PRINTEXCEPTION_C = 694
393 ERROR_DBG_RIPEXCEPTION = 695
394 ERROR_DBG_CONTROL_BREAK = 696
395 ERROR_DBG_COMMAND_EXCEPTION = 697
396 ERROR_DBG_EXCEPTION_HANDLED = 766
397 ERROR_DBG_CONTINUE = 767
398
399 ERROR_DEBUGGER_INACTIVE = 1284
400
401
402
403
404
405
406
407
408
409
410
412 _fields_ = [
413 ("Length", USHORT),
414 ("MaximumLength", USHORT),
415 ("Buffer", PVOID),
416 ]
417
418
419
420
421
422
423
424
425
426 -class GUID(Structure):
427 _fields_ = [
428 ("Data1", DWORD),
429 ("Data2", WORD),
430 ("Data3", WORD),
431 ("Data4", BYTE * 8),
432 ]
433
434
435
436
437
438
439
440 -class LIST_ENTRY(Structure):
442 LIST_ENTRY._fields_ = [
443 ("Flink", PVOID),
444 ("Blink", PVOID),
445 ]
446