Package winappdbg :: Package win32 :: Module gdi32
[hide private]
[frames] | no frames]

Source Code for Module winappdbg.win32.gdi32

  1  # Copyright (c) 2009-2010, Mario Vilas 
  2  # All rights reserved. 
  3  # 
  4  # Redistribution and use in source and binary forms, with or without 
  5  # modification, are permitted provided that the following conditions are met: 
  6  # 
  7  #     * Redistributions of source code must retain the above copyright notice, 
  8  #       this list of conditions and the following disclaimer. 
  9  #     * Redistributions in binary form must reproduce the above copyright 
 10  #       notice,this list of conditions and the following disclaimer in the 
 11  #       documentation and/or other materials provided with the distribution. 
 12  #     * Neither the name of the copyright holder nor the names of its 
 13  #       contributors may be used to endorse or promote products derived from 
 14  #       this software without specific prior written permission. 
 15  # 
 16  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 17  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 18  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 19  # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 20  # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 21  # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 22  # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 23  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 24  # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 25  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 26  # POSSIBILITY OF SUCH DAMAGE. 
 27   
 28  """ 
 29  Wrapper for gdi32.dll in ctypes. 
 30  """ 
 31   
 32  __revision__ = "$Id: gdi32.py 687 2010-06-06 16:09:38Z qvasimodo $" 
 33   
 34  from defines import * 
 35  from kernel32 import GetLastError, SetLastError 
 36   
 37  #--- Helpers ------------------------------------------------------------------ 
 38   
 39  #--- Types -------------------------------------------------------------------- 
 40   
 41  #--- Constants ---------------------------------------------------------------- 
 42   
 43  # GDI object types 
 44  OBJ_PEN             = 1 
 45  OBJ_BRUSH           = 2 
 46  OBJ_DC              = 3 
 47  OBJ_METADC          = 4 
 48  OBJ_PAL             = 5 
 49  OBJ_FONT            = 6 
 50  OBJ_BITMAP          = 7 
 51  OBJ_REGION          = 8 
 52  OBJ_METAFILE        = 9 
 53  OBJ_MEMDC           = 10 
 54  OBJ_EXTPEN          = 11 
 55  OBJ_ENHMETADC       = 12 
 56  OBJ_ENHMETAFILE     = 13 
 57  OBJ_COLORSPACE      = 14 
 58  GDI_OBJ_LAST        = OBJ_COLORSPACE 
 59   
 60  # Ternary raster operations 
 61  SRCCOPY         = 0x00CC0020 # dest = source 
 62  SRCPAINT        = 0x00EE0086 # dest = source OR dest 
 63  SRCAND          = 0x008800C6 # dest = source AND dest 
 64  SRCINVERT       = 0x00660046 # dest = source XOR dest 
 65  SRCERASE        = 0x00440328 # dest = source AND (NOT dest) 
 66  NOTSRCCOPY      = 0x00330008 # dest = (NOT source) 
 67  NOTSRCERASE     = 0x001100A6 # dest = (NOT src) AND (NOT dest) 
 68  MERGECOPY       = 0x00C000CA # dest = (source AND pattern) 
 69  MERGEPAINT      = 0x00BB0226 # dest = (NOT source) OR dest 
 70  PATCOPY         = 0x00F00021 # dest = pattern 
 71  PATPAINT        = 0x00FB0A09 # dest = DPSnoo 
 72  PATINVERT       = 0x005A0049 # dest = pattern XOR dest 
 73  DSTINVERT       = 0x00550009 # dest = (NOT dest) 
 74  BLACKNESS       = 0x00000042 # dest = BLACK 
 75  WHITENESS       = 0x00FF0062 # dest = WHITE 
 76  NOMIRRORBITMAP  = 0x80000000 # Do not Mirror the bitmap in this call 
 77  CAPTUREBLT      = 0x40000000 # Include layered windows 
 78   
 79  # Region flags 
 80  ERROR               = 0 
 81  NULLREGION          = 1 
 82  SIMPLEREGION        = 2 
 83  COMPLEXREGION       = 3 
 84  RGN_ERROR           = ERROR 
 85   
 86  # CombineRgn() styles 
 87  RGN_AND             = 1 
 88  RGN_OR              = 2 
 89  RGN_XOR             = 3 
 90  RGN_DIFF            = 4 
 91  RGN_COPY            = 5 
 92  RGN_MIN             = RGN_AND 
 93  RGN_MAX             = RGN_COPY 
 94   
 95  # StretchBlt() modes 
 96  BLACKONWHITE        = 1 
 97  WHITEONBLACK        = 2 
 98  COLORONCOLOR        = 3 
 99  HALFTONE            = 4 
100  MAXSTRETCHBLTMODE   = 4 
101  STRETCH_ANDSCANS    = BLACKONWHITE 
102  STRETCH_ORSCANS     = WHITEONBLACK 
103  STRETCH_DELETESCANS = COLORONCOLOR 
104  STRETCH_HALFTONE    = HALFTONE 
105   
106  # PolyFill() modes 
107  ALTERNATE       = 1 
108  WINDING         = 2 
109  POLYFILL_LAST   = 2 
110   
111  # Layout orientation options 
112  LAYOUT_RTL                         = 0x00000001 # Right to left 
113  LAYOUT_BTT                         = 0x00000002 # Bottom to top 
114  LAYOUT_VBH                         = 0x00000004 # Vertical before horizontal 
115  LAYOUT_ORIENTATIONMASK             = LAYOUT_RTL + LAYOUT_BTT + LAYOUT_VBH 
116  LAYOUT_BITMAPORIENTATIONPRESERVED  = 0x00000008 
117   
118  # Stock objects 
119  WHITE_BRUSH         = 0 
120  LTGRAY_BRUSH        = 1 
121  GRAY_BRUSH          = 2 
122  DKGRAY_BRUSH        = 3 
123  BLACK_BRUSH         = 4 
124  NULL_BRUSH          = 5 
125  HOLLOW_BRUSH        = NULL_BRUSH 
126  WHITE_PEN           = 6 
127  BLACK_PEN           = 7 
128  NULL_PEN            = 8 
129  OEM_FIXED_FONT      = 10 
130  ANSI_FIXED_FONT     = 11 
131  ANSI_VAR_FONT       = 12 
132  SYSTEM_FONT         = 13 
133  DEVICE_DEFAULT_FONT = 14 
134  DEFAULT_PALETTE     = 15 
135  SYSTEM_FIXED_FONT   = 16 
136   
137  # Metafile functions 
138  META_SETBKCOLOR              = 0x0201 
139  META_SETBKMODE               = 0x0102 
140  META_SETMAPMODE              = 0x0103 
141  META_SETROP2                 = 0x0104 
142  META_SETRELABS               = 0x0105 
143  META_SETPOLYFILLMODE         = 0x0106 
144  META_SETSTRETCHBLTMODE       = 0x0107 
145  META_SETTEXTCHAREXTRA        = 0x0108 
146  META_SETTEXTCOLOR            = 0x0209 
147  META_SETTEXTJUSTIFICATION    = 0x020A 
148  META_SETWINDOWORG            = 0x020B 
149  META_SETWINDOWEXT            = 0x020C 
150  META_SETVIEWPORTORG          = 0x020D 
151  META_SETVIEWPORTEXT          = 0x020E 
152  META_OFFSETWINDOWORG         = 0x020F 
153  META_SCALEWINDOWEXT          = 0x0410 
154  META_OFFSETVIEWPORTORG       = 0x0211 
155  META_SCALEVIEWPORTEXT        = 0x0412 
156  META_LINETO                  = 0x0213 
157  META_MOVETO                  = 0x0214 
158  META_EXCLUDECLIPRECT         = 0x0415 
159  META_INTERSECTCLIPRECT       = 0x0416 
160  META_ARC                     = 0x0817 
161  META_ELLIPSE                 = 0x0418 
162  META_FLOODFILL               = 0x0419 
163  META_PIE                     = 0x081A 
164  META_RECTANGLE               = 0x041B 
165  META_ROUNDRECT               = 0x061C 
166  META_PATBLT                  = 0x061D 
167  META_SAVEDC                  = 0x001E 
168  META_SETPIXEL                = 0x041F 
169  META_OFFSETCLIPRGN           = 0x0220 
170  META_TEXTOUT                 = 0x0521 
171  META_BITBLT                  = 0x0922 
172  META_STRETCHBLT              = 0x0B23 
173  META_POLYGON                 = 0x0324 
174  META_POLYLINE                = 0x0325 
175  META_ESCAPE                  = 0x0626 
176  META_RESTOREDC               = 0x0127 
177  META_FILLREGION              = 0x0228 
178  META_FRAMEREGION             = 0x0429 
179  META_INVERTREGION            = 0x012A 
180  META_PAINTREGION             = 0x012B 
181  META_SELECTCLIPREGION        = 0x012C 
182  META_SELECTOBJECT            = 0x012D 
183  META_SETTEXTALIGN            = 0x012E 
184  META_CHORD                   = 0x0830 
185  META_SETMAPPERFLAGS          = 0x0231 
186  META_EXTTEXTOUT              = 0x0a32 
187  META_SETDIBTODEV             = 0x0d33 
188  META_SELECTPALETTE           = 0x0234 
189  META_REALIZEPALETTE          = 0x0035 
190  META_ANIMATEPALETTE          = 0x0436 
191  META_SETPALENTRIES           = 0x0037 
192  META_POLYPOLYGON             = 0x0538 
193  META_RESIZEPALETTE           = 0x0139 
194  META_DIBBITBLT               = 0x0940 
195  META_DIBSTRETCHBLT           = 0x0b41 
196  META_DIBCREATEPATTERNBRUSH   = 0x0142 
197  META_STRETCHDIB              = 0x0f43 
198  META_EXTFLOODFILL            = 0x0548 
199  META_SETLAYOUT               = 0x0149 
200  META_DELETEOBJECT            = 0x01f0 
201  META_CREATEPALETTE           = 0x00f7 
202  META_CREATEPATTERNBRUSH      = 0x01F9 
203  META_CREATEPENINDIRECT       = 0x02FA 
204  META_CREATEFONTINDIRECT      = 0x02FB 
205  META_CREATEBRUSHINDIRECT     = 0x02FC 
206  META_CREATEREGION            = 0x06FF 
207   
208  # Metafile escape codes 
209  NEWFRAME                     = 1 
210  ABORTDOC                     = 2 
211  NEXTBAND                     = 3 
212  SETCOLORTABLE                = 4 
213  GETCOLORTABLE                = 5 
214  FLUSHOUTPUT                  = 6 
215  DRAFTMODE                    = 7 
216  QUERYESCSUPPORT              = 8 
217  SETABORTPROC                 = 9 
218  STARTDOC                     = 10 
219  ENDDOC                       = 11 
220  GETPHYSPAGESIZE              = 12 
221  GETPRINTINGOFFSET            = 13 
222  GETSCALINGFACTOR             = 14 
223  MFCOMMENT                    = 15 
224  GETPENWIDTH                  = 16 
225  SETCOPYCOUNT                 = 17 
226  SELECTPAPERSOURCE            = 18 
227  DEVICEDATA                   = 19 
228  PASSTHROUGH                  = 19 
229  GETTECHNOLGY                 = 20 
230  GETTECHNOLOGY                = 20 
231  SETLINECAP                   = 21 
232  SETLINEJOIN                  = 22 
233  SETMITERLIMIT                = 23 
234  BANDINFO                     = 24 
235  DRAWPATTERNRECT              = 25 
236  GETVECTORPENSIZE             = 26 
237  GETVECTORBRUSHSIZE           = 27 
238  ENABLEDUPLEX                 = 28 
239  GETSETPAPERBINS              = 29 
240  GETSETPRINTORIENT            = 30 
241  ENUMPAPERBINS                = 31 
242  SETDIBSCALING                = 32 
243  EPSPRINTING                  = 33 
244  ENUMPAPERMETRICS             = 34 
245  GETSETPAPERMETRICS           = 35 
246  POSTSCRIPT_DATA              = 37 
247  POSTSCRIPT_IGNORE            = 38 
248  MOUSETRAILS                  = 39 
249  GETDEVICEUNITS               = 42 
250  GETEXTENDEDTEXTMETRICS       = 256 
251  GETEXTENTTABLE               = 257 
252  GETPAIRKERNTABLE             = 258 
253  GETTRACKKERNTABLE            = 259 
254  EXTTEXTOUT                   = 512 
255  GETFACENAME                  = 513 
256  DOWNLOADFACE                 = 514 
257  ENABLERELATIVEWIDTHS         = 768 
258  ENABLEPAIRKERNING            = 769 
259  SETKERNTRACK                 = 770 
260  SETALLJUSTVALUES             = 771 
261  SETCHARSET                   = 772 
262  STRETCHBLT                   = 2048 
263  METAFILE_DRIVER              = 2049 
264  GETSETSCREENPARAMS           = 3072 
265  QUERYDIBSUPPORT              = 3073 
266  BEGIN_PATH                   = 4096 
267  CLIP_TO_PATH                 = 4097 
268  END_PATH                     = 4098 
269  EXT_DEVICE_CAPS              = 4099 
270  RESTORE_CTM                  = 4100 
271  SAVE_CTM                     = 4101 
272  SET_ARC_DIRECTION            = 4102 
273  SET_BACKGROUND_COLOR         = 4103 
274  SET_POLY_MODE                = 4104 
275  SET_SCREEN_ANGLE             = 4105 
276  SET_SPREAD                   = 4106 
277  TRANSFORM_CTM                = 4107 
278  SET_CLIP_BOX                 = 4108 
279  SET_BOUNDS                   = 4109 
280  SET_MIRROR_MODE              = 4110 
281  OPENCHANNEL                  = 4110 
282  DOWNLOADHEADER               = 4111 
283  CLOSECHANNEL                 = 4112 
284  POSTSCRIPT_PASSTHROUGH       = 4115 
285  ENCAPSULATED_POSTSCRIPT      = 4116 
286  POSTSCRIPT_IDENTIFY          = 4117 
287  POSTSCRIPT_INJECTION         = 4118 
288  CHECKJPEGFORMAT              = 4119 
289  CHECKPNGFORMAT               = 4120 
290  GET_PS_FEATURESETTING        = 4121 
291  GDIPLUS_TS_QUERYVER          = 4122 
292  GDIPLUS_TS_RECORD            = 4123 
293  SPCLPASSTHROUGH2             = 4568 
294   
295  #--- Structures --------------------------------------------------------------- 
296   
297  # typedef struct _RECT { 
298  #   LONG left; 
299  #   LONG top; 
300  #   LONG right; 
301  #   LONG bottom; 
302  # }RECT, *PRECT; 
303 -class RECT(Structure):
304 _fields_ = [ 305 ('left', LONG), 306 ('top', LONG), 307 ('right', LONG), 308 ('bottom', LONG), 309 ]
310 PRECT = POINTER(RECT) 311 LPRECT = PRECT 312 313 # typedef struct tagPOINT { 314 # LONG x; 315 # LONG y; 316 # } POINT;
317 -class POINT(Structure):
318 _fields_ = [ 319 ('x', LONG), 320 ('y', LONG), 321 ]
322 PPOINT = POINTER(POINT) 323 LPPOINT = PPOINT 324 325 # typedef struct tagBITMAP { 326 # LONG bmType; 327 # LONG bmWidth; 328 # LONG bmHeight; 329 # LONG bmWidthBytes; 330 # WORD bmPlanes; 331 # WORD bmBitsPixel; 332 # LPVOID bmBits; 333 # } BITMAP, *PBITMAP;
334 -class BITMAP(Structure):
335 _fields_ = [ 336 ("bmType", LONG), 337 ("bmWidth", LONG), 338 ("bmHeight", LONG), 339 ("bmWidthBytes", LONG), 340 ("bmPlanes", WORD), 341 ("bmBitsPixel", WORD), 342 ("bmBits", LPVOID), 343 ]
344 PBITMAP = POINTER(BITMAP) 345 LPBITMAP = PBITMAP 346 347 #--- High level classes ------------------------------------------------------- 348 349 #--- gdi32.dll ---------------------------------------------------------------- 350 351 # HDC GetDC( 352 # __in HWND hWnd 353 # );
354 -def GetDC(hWnd):
355 _GetDC = windll.gdi32.GetDC 356 _GetDC.argtypes = [HWND] 357 _GetDC.restype = HDC 358 _GetDC.errcheck = RaiseIfZero 359 return _GetDC(hWnd)
360 361 # HDC GetWindowDC( 362 # __in HWND hWnd 363 # );
364 -def GetWindowDC(hWnd):
365 _GetWindowDC = windll.gdi32.GetWindowDC 366 _GetWindowDC.argtypes = [HWND] 367 _GetWindowDC.restype = HDC 368 _GetWindowDC.errcheck = RaiseIfZero 369 return _GetWindowDC(hWnd)
370 371 # int ReleaseDC( 372 # __in HWND hWnd, 373 # __in HDC hDC 374 # );
375 -def ReleaseDC(hWnd, hDC):
376 _ReleaseDC = windll.gdi32.ReleaseDC 377 _ReleaseDC.argtypes = [HWND, HDC] 378 _ReleaseDC.restype = ctypes.c_int 379 _ReleaseDC.errcheck = RaiseIfZero 380 _ReleaseDC(hWnd, hDC)
381 382 # HGDIOBJ SelectObject( 383 # __in HDC hdc, 384 # __in HGDIOBJ hgdiobj 385 # );
386 -def SelectObject(hdc, hgdiobj):
387 _SelectObject = windll.gdi32.SelectObject 388 _SelectObject.argtypes = [HDC, HGDIOBJ] 389 _SelectObject.restype = HGDIOBJ 390 _SelectObject.errcheck = RaiseIfZero 391 return _SelectObject(hdc, hgdiobj)
392 393 # HGDIOBJ GetStockObject( 394 # __in int fnObject 395 # );
396 -def GetStockObject(fnObject):
397 _GetStockObject = windll.gdi32.GetStockObject 398 _GetStockObject.argtypes = [ctypes.c_int] 399 _GetStockObject.restype = HGDIOBJ 400 _GetStockObject.errcheck = RaiseIfZero 401 return _GetStockObject(fnObject)
402 403 # DWORD GetObjectType( 404 # __in HGDIOBJ h 405 # );
406 -def GetObjectType(h):
407 _GetObjectType = windll.gdi32.GetObjectType 408 _GetObjectType.argtypes = [HGDIOBJ] 409 _GetObjectType.restype = DWORD 410 _GetObjectType.errcheck = RaiseIfZero 411 return _GetObjectType(h)
412 413 # int GetObject( 414 # __in HGDIOBJ hgdiobj, 415 # __in int cbBuffer, 416 # __out LPVOID lpvObject 417 # );
418 -def GetObject(hgdiobj, cbBuffer = None, lpvObject = None):
419 _GetObject = windll.gdi32.GetObject 420 _GetObject.argtypes = [HGDIOBJ, ctypes.c_int, LPVOID] 421 _GetObject.restype = ctypes.c_int 422 _GetObject.errcheck = RaiseIfZero 423 424 # Both cbBuffer and lpvObject can be omitted, the correct 425 # size and structure to return are automatically deduced. 426 # If lpvObject is given it must be a ctypes object, not a pointer. 427 # Always returns a ctypes object. 428 429 if cbBuffer is not None: 430 if lpvObject is None: 431 lpvObject = ctypes.create_string_buffer("", cbBuffer) 432 elif lpvObject is not None: 433 cbBuffer = ctypes.sizeof(lpvObject) 434 else: # most likely case, both are None 435 t = GetObjectType(hgdiobj) 436 if t == OBJ_PEN: 437 cbBuffer = ctypes.sizeof(LOGPEN) 438 lpvObject = LOGPEN() 439 elif t == OBJ_BRUSH: 440 cbBuffer = ctypes.sizeof(LOGBRUSH) 441 lpvObject = LOGBRUSH() 442 elif t == OBJ_PAL: 443 cbBuffer = _GetObject(hgdiobj, 0, None) 444 lpvObject = (WORD * (cbBuffer // ctypes.sizeof(WORD)))() 445 elif t == OBJ_FONT: 446 cbBuffer = ctypes.sizeof(LOGFONT) 447 lpvObject = LOGFONT() 448 elif t == OBJ_BITMAP: # try the two possible types of bitmap 449 cbBuffer = ctypes.sizeof(DIBSECTION) 450 lpvObject = DIBSECTION() 451 try: 452 _GetObject(hgdiobj, cbBuffer, ctypes.byref(lpvObject)) 453 return lpvObject 454 except WindowsError: 455 cbBuffer = ctypes.sizeof(BITMAP) 456 lpvObject = BITMAP() 457 elif t == OBJ_EXTPEN: 458 cbBuffer = ctypes.sizeof(LOGEXTPEN) 459 lpvObject = LOGEXTPEN() 460 else: 461 cbBuffer = _GetObject(hgdiobj, 0, None) 462 lpvObject = ctypes.create_string_buffer("", cbBuffer) 463 _GetObject(hgdiobj, cbBuffer, ctypes.byref(lpvObject)) 464 return lpvObject
465 466 # LONG GetBitmapBits( 467 # __in HBITMAP hbmp, 468 # __in LONG cbBuffer, 469 # __out LPVOID lpvBits 470 # );
471 -def GetBitmapBits(hbmp):
472 _GetBitmapBits = windll.gdi32.GetBitmapBits 473 _GetBitmapBits.argtypes = [HBITMAP, LONG, LPVOID] 474 _GetBitmapBits.restype = LONG 475 _GetBitmapBits.errcheck = RaiseIfZero 476 477 bitmap = GetObject(hbmp, lpvObject = BITMAP()) 478 cbBuffer = bitmap.bmWidthBytes * bitmap.bmHeight 479 lpvBits = ctypes.create_string_buffer("", cbBuffer) 480 _GetBitmapBits(hbmp, cbBuffer, ctypes.byref(lpvBits)) 481 return lpvBits.raw
482 483 # HBITMAP CreateBitmapIndirect( 484 # __in const BITMAP *lpbm 485 # );
486 -def CreateBitmapIndirect(lpbm):
487 _CreateBitmapIndirect = windll.gdi32.CreateBitmapIndirect 488 _CreateBitmapIndirect.argtypes = [PBITMAP] 489 _CreateBitmapIndirect.restype = HBITMAP 490 _CreateBitmapIndirect.errcheck = RaiseIfZero 491 return _CreateBitmapIndirect(lpbm)
492