Thursday, January 4, 2007

Facts of a GDI Handle

Yesterday i just read some pages from the book

Windows Graphics Programming Win32 GDI and DirectDraw®

Feng Yuan
The Feng Yuan is working in mircosoft.  In this space i am going to share some knowledge i got from this book. So Tell me what a GDI handle is ? like HPEN or HBRUSH or HGDIOBJ ? . Somebody may tell it is a pointer . me too thought like that till yesterday , Now its changed.

Actually Take HGIOBJ as a 32 bit number (it can be pointer or anything).  We all know about the functions GetStockObject(). it is returning a predefined handle. If we call this GetStockObject(WHITE_BRUSH)  from 2 process it will return the same handle(32 bit Number). So what does it means ? it is not residing in user address space. it is in Kernel address space. May be initialized during system startup.  The book says that the lower 12 bit of the GDi handle is a index to table. May be that tables stores all other information. The 24th bit position says whether it is a stock object or not. The GDI handle also store information about the type of the handle.
The following function Gives the handle type.

inline unsigned GetObjType(HGDIOBJ hGDIObj)
{
  return (((unsigned) hGDIObj) >> 16) & 0x7F;
}


The return value can be one of the following.

typedef enum
{
  gdi_objtypeb_dc          = 0x01,
  gdi_objtypeb_region      = 0x04,
  gdi_objtypeb_bitmap      = 0x05,
  gdi_objtypeb_palette     = 0x08,
  gdi_objtypeb_font        = 0x0a,
  gdi_objtypeb_brush       = 0x10,
  gdi_objtypeb_enhmetafile = 0x21,
  gdi_objtypeb_pen         = 0x30,
  gdi_objtypeb_extpen      = 0x50
};

Fortunatly there is an undocumented function in GDI32.dll named GdiQueryTable() . It will return the address of the Table. using that address we can access all handles created by our program. 



1 comment:

msnkd said...

Good information.. Thanks