Release 970824

Sat Aug 23 00:05:23 1997  Andreas Mohr <100.30936@germany.net>

	* [if1632/kernel.spec] [if1632/mmsystem.spec]
	Added some stubs.

	* [include/neexe.h] [loader/module.c]
	Added warning for OS/2 executables.

	* [multimedia/midi.c]
	Shortened MIDIOUT driver version string to be less than 31 chars.

	* [objects/gdiobj.c]
	Fixed DeleteObject32() to react properly when called with stock object.

Fri Aug 22 18:03:26 1997  Dimitrie O. Paun <dimi@cs.toronto.edu>

	* [controls/updown.c] [include/updown.h]
	First attempt at implementiong the UpDown class.

	* [controls/widgets.c]
	Added the UpDown class to be initialized by InitCommonControls().

Wed Aug 20 18:01:33 1997  Doug Ridgway <ridgway@routh.UCSD.EDU>

	* [graphics/*] [objects/*] [include/gdi.h]
	Made all GDI objects (except DCs) moveable.

Mon Aug 18 03:25:30 1997  Alex Korobka <alex@trantor.pharm.sunysb.edu>

	* [windows/event.c] [misc/winsock.c] [misc/winsock_dns.c]
	Removed IPC communication to speed up winsock services
	(tested only with 16-bit netscape 3.03).

	* [graphics/x11drv/xfont.c] [documentation/fonts]
	Miscellaneous improvements. Updated docs.

Sun Aug 17 20:39:55 1997  Ingo Schneider <schneidi@informatik.tu-muenchen.de>

	* [misc/comm.c]
	A couple of bug fixes.

Sun Aug 17 19:29:22 1997  Alexandre Julliard  <julliard@lrc.epfl.ch>

	* [debugger/dbg.y]
	Display next instruction after stepi/nexti.

	* [if1632/relay.c] [include/callback.h] [tools/build.c]
	Replaced CallTo32_LargeStack with the CALL_LARGE_STACK macro for
	better Winelib support.

	* [include/sigcontext.h]
	Renamed to sig_context.h to avoid conflicts with libc.

	* [*/*]
	All API functions are now prefixed with WINAPI in prevision of
	future Winelib integration.

	* [loader/signal.c] [memory/ldt.c]
	Fixed assembly code to be -fPIC compatible.

Thu Aug 14 14:38:15 1997  Marcus Meissner <msmeissn@cip.informatik.uni-erlangen.de>

	* [if1632/crtdll.spec][win32/except.c]
	_global_unwind, _local_unwind stub added.

	* [objects/dib.c]	
	Don't read memory you don't even need for the target bitmap (fixes
	one 'lazy' program).

	* [if1632/relay.c][if1632/thunk.c][if1632/kernel32.spec]
	  [win32/ordinals.c][memory/selector.c][memory/global.c]
	  [include/callback.h]
	Added/moved some more win95 ordinal stuff. Implemented QT_Thunk
	(not 100% correct yet) and some friends.

	* [loader/pe_image.c]
	Add possibility to break at the DLL entrypoint.

	* [controls/static.c][misc/commdlg.c][scheduler/thread.c]
	Misc bugfixes and additions.

	* [misc/registry.c]
	The registry seems to be case-preserving but case-insensitive.

	* [memory/global.c]	
	Adapted to new /proc/meminfo format.

	* [objects/font.c][graphics/x11drv/xfont.c]
	FONT_SelectObject and GetTextMetrics* get passed ranges in logical
 	and not device points (thanks to Marion Reyzl for pointing this
 	out).

	* [windows/caret.c]
	Use the windows own DC if present (The caret coordinates are
	logical coordinates based on it). Fixes another AMIPRO problem.

Wed Aug  6 18:22:22 1997  Morten Welinder  <terra@diku.dk>

	* [controls/menu.c]
	General clean-up and Win32 work: split item_flags into fType and
	fState; split item_id into wID and hSubMenu.  Improved
	debug-printing.  Implemented InsertMenuItem32[AW],
	SetMenuDefaultItem32, and SetMenuItemInfo32[AW].  Fixed
	GetMenuItemInfo32[AW].

	* [if1632/user32.spec]
	Define above new functions.

	* [include/windows.h]
	Define MF_DEFAULT and MF_RIGHTJUSTIFY.  Prototype above functions.

	* [include/menu.h]
	Don't prototype now-static MENU_InitSysMenuPopup.

	* [include/comm.h]
	Reduce MAX_PORTS to 9 (which the profile code can handle).

Tue Aug  5 20:16:22 1997  Victor Schneider <vischne@ibm.net>

	* [library/winestub.c] [libtest/expand.c]
	These patches let people porting Windows apps compile them using
	the same conventions regarding global _argc and _argv as those on
	Windows C/C++ compilers.
diff --git a/objects/bitmap.c b/objects/bitmap.c
index fdb59d2..6fe7a98 100644
--- a/objects/bitmap.c
+++ b/objects/bitmap.c
@@ -30,10 +30,31 @@
 
 
 /***********************************************************************
+ *           XPutImage_wrapper
+ *
+ * Wrapper to call XPutImage with CALL_LARGE_STACK.
+ */
+
+struct XPutImage_descr
+{
+    BITMAPOBJ *bmp;
+    XImage    *image;
+    INT32      width;
+    INT32      height;
+};
+
+static int XPutImage_wrapper( const struct XPutImage_descr *descr )
+{
+    return XPutImage( display, descr->bmp->pixmap, BITMAP_GC(descr->bmp),
+                      descr->image, 0, 0, 0, 0, descr->width, descr->height );
+}
+
+
+/***********************************************************************
  *           CreateBitmap16    (GDI.48)
  */
-HBITMAP16 CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
-                          UINT16 bpp, LPCVOID bits )
+HBITMAP16 WINAPI CreateBitmap16( INT16 width, INT16 height, UINT16 planes,
+                                 UINT16 bpp, LPCVOID bits )
 {
     return CreateBitmap32( width, height, planes, bpp, bits );
 }
@@ -42,8 +63,8 @@
 /***********************************************************************
  *           CreateBitmap32    (GDI32.25)
  */
-HBITMAP32 CreateBitmap32( INT32 width, INT32 height, UINT32 planes,
-                          UINT32 bpp, LPCVOID bits )
+HBITMAP32 WINAPI CreateBitmap32( INT32 width, INT32 height, UINT32 planes,
+                                 UINT32 bpp, LPCVOID bits )
 {
     BITMAPOBJ * bmpObjPtr;
     HBITMAP32 hbitmap;
@@ -63,7 +84,7 @@
       /* Create the BITMAPOBJ */
     hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
     if (!hbitmap) return 0;
-    bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LIN_ADDR( hbitmap );
+    bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
 
     bmpObjPtr->size.cx = 0;
     bmpObjPtr->size.cy = 0;
@@ -85,6 +106,7 @@
     else if (bits)  /* Set bitmap bits */
 	SetBitmapBits32( hbitmap, height * bmpObjPtr->bitmap.bmWidthBytes,
                          bits );
+    GDI_HEAP_UNLOCK( hbitmap );
     return hbitmap;
 }
 
@@ -92,7 +114,7 @@
 /***********************************************************************
  *           CreateCompatibleBitmap16    (GDI.51)
  */
-HBITMAP16 CreateCompatibleBitmap16( HDC16 hdc, INT16 width, INT16 height )
+HBITMAP16 WINAPI CreateCompatibleBitmap16(HDC16 hdc, INT16 width, INT16 height)
 {
     return CreateCompatibleBitmap32( hdc, width, height );
 }
@@ -101,7 +123,7 @@
 /***********************************************************************
  *           CreateCompatibleBitmap32    (GDI32.30)
  */
-HBITMAP32 CreateCompatibleBitmap32( HDC32 hdc, INT32 width, INT32 height )
+HBITMAP32 WINAPI CreateCompatibleBitmap32(HDC32 hdc, INT32 width, INT32 height)
 {
     HBITMAP32 hbmpRet = 0;
     DC *dc;
@@ -118,7 +140,7 @@
 /***********************************************************************
  *           CreateBitmapIndirect16    (GDI.49)
  */
-HBITMAP16 CreateBitmapIndirect16( const BITMAP16 * bmp )
+HBITMAP16 WINAPI CreateBitmapIndirect16( const BITMAP16 * bmp )
 {
     return CreateBitmap16( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
                            bmp->bmBitsPixel, PTR_SEG_TO_LIN( bmp->bmBits ) );
@@ -128,7 +150,7 @@
 /***********************************************************************
  *           CreateBitmapIndirect32    (GDI32.26)
  */
-HBITMAP32 CreateBitmapIndirect32( const BITMAP32 * bmp )
+HBITMAP32 WINAPI CreateBitmapIndirect32( const BITMAP32 * bmp )
 {
     return CreateBitmap32( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
                            bmp->bmBitsPixel, bmp->bmBits );
@@ -136,9 +158,21 @@
 
 
 /***********************************************************************
+ *           BITMAP_GetXImage
+ *
+ * Get an X image for a bitmap. For use with CALL_LARGE_STACK.
+ */
+XImage *BITMAP_GetXImage( const BITMAPOBJ *bmp )
+{
+    return XGetImage( display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
+                      bmp->bitmap.bmHeight, AllPlanes, ZPixmap );
+}
+
+
+/***********************************************************************
  *           GetBitmapBits16    (GDI.74)
  */
-LONG GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
+LONG WINAPI GetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPVOID buffer )
 {
     return GetBitmapBits32( hbitmap, count, buffer );
 }
@@ -147,12 +181,12 @@
 /***********************************************************************
  *           GetBitmapBits32    (GDI32.143)
  */
-LONG GetBitmapBits32( HBITMAP32 hbitmap, LONG count, LPVOID buffer )
+LONG WINAPI GetBitmapBits32( HBITMAP32 hbitmap, LONG count, LPVOID buffer )
 {
     BITMAPOBJ * bmp;
-    LONG height,widthbytes;
+    LONG height, old_height;
     XImage * image;
-    LPBYTE tmpbuffer,tbuf;
+    LPBYTE tbuf;
     int	h,w,pad;
     
     /* KLUDGE! */
@@ -169,7 +203,11 @@
     dprintf_bitmap(stddeb, "GetBitmapBits: %dx%d %d colors %p fetched height: %ld\n",
 	    bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
 	    1 << bmp->bitmap.bmBitsPixel, buffer, height );
-    if (!height) return 0;
+    if (!height) 
+    {
+      GDI_HEAP_UNLOCK( hbitmap );
+      return 0;
+    }
 
     switch (bmp->bitmap.bmBitsPixel) {
     case 1:
@@ -198,19 +236,16 @@
 	fprintf(stderr,"GetBitMapBits32: unknown depth %d, please report.\n",
 		bmp->bitmap.bmBitsPixel
 	);
+	GDI_HEAP_UNLOCK( hbitmap );
 	return 0;
     }
 
-    widthbytes	= DIB_GetXImageWidthBytes(bmp->bitmap.bmWidth,bmp->bitmap.bmBitsPixel);
-    tmpbuffer	= (LPBYTE)xmalloc(widthbytes*height);
-    image = XCreateImage( display, DefaultVisualOfScreen(screen),
-		  bmp->bitmap.bmBitsPixel, ZPixmap, 0, tmpbuffer,
-		  bmp->bitmap.bmWidth,height,32,widthbytes
-    );
-    
-    CallTo32_LargeStack( (int(*)())XGetSubImage, 11,
-                         display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
-                         height, AllPlanes, ZPixmap, image, 0, 0 );
+    /* Hack: change the bitmap height temporarily to avoid */
+    /*       getting unnecessary bitmap rows. */
+    old_height = bmp->bitmap.bmHeight;
+    bmp->bitmap.bmHeight = height;
+    image = (XImage *)CALL_LARGE_STACK( BITMAP_GetXImage, bmp );
+    bmp->bitmap.bmHeight = old_height;
 
     /* copy XImage to 16 bit padded image buffer with real bitsperpixel */
 
@@ -277,7 +312,8 @@
             tbuf += pad;
 	}
     }
-    XDestroyImage( image ); /* frees tbuffer too */
+    XDestroyImage( image );
+    GDI_HEAP_UNLOCK( hbitmap );
     return height * bmp->bitmap.bmWidthBytes;
 }
 
@@ -285,7 +321,7 @@
 /***********************************************************************
  *           SetBitmapBits16    (GDI.106)
  */
-LONG SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
+LONG WINAPI SetBitmapBits16( HBITMAP16 hbitmap, LONG count, LPCVOID buffer )
 {
     return SetBitmapBits32( hbitmap, count, buffer );
 }
@@ -294,8 +330,9 @@
 /***********************************************************************
  *           SetBitmapBits32    (GDI32.303)
  */
-LONG SetBitmapBits32( HBITMAP32 hbitmap, LONG count, LPCVOID buffer )
+LONG WINAPI SetBitmapBits32( HBITMAP32 hbitmap, LONG count, LPCVOID buffer )
 {
+    struct XPutImage_descr descr;
     BITMAPOBJ * bmp;
     LONG height;
     XImage * image;
@@ -317,8 +354,12 @@
       /* Only set entire lines */
     height = count / bmp->bitmap.bmWidthBytes;
     if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
-    if (!height) return 0;
-    	
+    if (!height) 
+    {
+      GDI_HEAP_UNLOCK( hbitmap );
+      return 0;
+    }
+	
     switch (bmp->bitmap.bmBitsPixel) {
     case 1:
 	if (!(bmp->bitmap.bmWidth & 15))
@@ -416,10 +457,14 @@
         break;
     }
 
-    CallTo32_LargeStack( XPutImage, 10,
-                         display, bmp->pixmap, BITMAP_GC(bmp), image, 0, 0,
-                         0, 0, bmp->bitmap.bmWidth, height );
+    descr.bmp    = bmp;
+    descr.image  = image;
+    descr.width  = bmp->bitmap.bmWidth;
+    descr.height = height;
+    CALL_LARGE_STACK( XPutImage_wrapper, &descr );
+
     XDestroyImage( image ); /* frees tmpbuffer too */
+    GDI_HEAP_UNLOCK( hbitmap );
     return height * bmp->bitmap.bmWidthBytes;
 }
 
@@ -429,10 +474,9 @@
  * defines in windows.h
  */
 
-HANDLE32 LoadImage32A(
-	HINSTANCE32 hinst,LPCSTR name,UINT32 type,INT32 desiredx,
-	INT32 desiredy,UINT32 loadflags
-) {
+HANDLE32 WINAPI LoadImage32A( HINSTANCE32 hinst, LPCSTR name, UINT32 type,
+                              INT32 desiredx, INT32 desiredy, UINT32 loadflags)
+{
 	if (HIWORD(name)) {
 		dprintf_resource(stddeb,"LoadImage32A(0x%04x,%s,%d,%d,%d,0x%08x)\n",
 			hinst,name,type,desiredx,desiredy,loadflags
@@ -459,8 +503,8 @@
  * FIXME: implementation still lacks nearly all features, see LR_*
  * defines in windows.h
  */
-HANDLE32 CopyImage32( HANDLE32 hnd, UINT32 type, INT32 desiredx,
-                      INT32 desiredy, UINT32 flags )
+HANDLE32 WINAPI CopyImage32( HANDLE32 hnd, UINT32 type, INT32 desiredx,
+                             INT32 desiredy, UINT32 flags )
 {
     switch (type)
     {
@@ -478,7 +522,7 @@
 /**********************************************************************
  *	    LoadBitmap16    (USER.175)
  */
-HBITMAP16 LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
+HBITMAP16 WINAPI LoadBitmap16( HINSTANCE16 instance, SEGPTR name )
 {
     HBITMAP32 hbitmap = 0;
     HDC32 hdc;
@@ -520,7 +564,7 @@
 /**********************************************************************
  *	    LoadBitmap32W   (USER32.357)
  */
-HBITMAP32 LoadBitmap32W( HINSTANCE32 instance, LPCWSTR name )
+HBITMAP32 WINAPI LoadBitmap32W( HINSTANCE32 instance, LPCWSTR name )
 {
     HBITMAP32 hbitmap = 0;
     HDC32 hdc;
@@ -553,7 +597,7 @@
 /**********************************************************************
  *	    LoadBitmap32A   (USER32.356)
  */
-HBITMAP32 LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
+HBITMAP32 WINAPI LoadBitmap32A( HINSTANCE32 instance, LPCSTR name )
 {
     HBITMAP32 res;
     if (!HIWORD(name)) res = LoadBitmap32W( instance, (LPWSTR)name );
@@ -629,7 +673,8 @@
 /***********************************************************************
  *           CreateDiscardableBitmap16    (GDI.156)
  */
-HBITMAP16 CreateDiscardableBitmap16( HDC16 hdc, INT16 width, INT16 height )
+HBITMAP16 WINAPI CreateDiscardableBitmap16( HDC16 hdc, INT16 width,
+                                            INT16 height )
 {
     return CreateCompatibleBitmap16( hdc, width, height );
 }
@@ -638,7 +683,8 @@
 /***********************************************************************
  *           CreateDiscardableBitmap32    (GDI32.38)
  */
-HBITMAP32 CreateDiscardableBitmap32( HDC32 hdc, INT32 width, INT32 height )
+HBITMAP32 WINAPI CreateDiscardableBitmap32( HDC32 hdc, INT32 width,
+                                            INT32 height )
 {
     return CreateCompatibleBitmap32( hdc, width, height );
 }
@@ -647,11 +693,12 @@
 /***********************************************************************
  *           GetBitmapDimensionEx16    (GDI.468)
  */
-BOOL16 GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
+BOOL16 WINAPI GetBitmapDimensionEx16( HBITMAP16 hbitmap, LPSIZE16 size )
 {
     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
     if (!bmp) return FALSE;
     *size = bmp->size;
+    GDI_HEAP_UNLOCK( hbitmap );
     return TRUE;
 }
 
@@ -659,12 +706,13 @@
 /***********************************************************************
  *           GetBitmapDimensionEx32    (GDI32.144)
  */
-BOOL32 GetBitmapDimensionEx32( HBITMAP32 hbitmap, LPSIZE32 size )
+BOOL32 WINAPI GetBitmapDimensionEx32( HBITMAP32 hbitmap, LPSIZE32 size )
 {
     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
     if (!bmp) return FALSE;
     size->cx = (INT32)bmp->size.cx;
     size->cy = (INT32)bmp->size.cy;
+    GDI_HEAP_UNLOCK( hbitmap );
     return TRUE;
 }
 
@@ -672,7 +720,7 @@
 /***********************************************************************
  *           GetBitmapDimension    (GDI.162)
  */
-DWORD GetBitmapDimension( HBITMAP16 hbitmap )
+DWORD WINAPI GetBitmapDimension( HBITMAP16 hbitmap )
 {
     SIZE16 size;
     if (!GetBitmapDimensionEx16( hbitmap, &size )) return 0;
@@ -683,14 +731,15 @@
 /***********************************************************************
  *           SetBitmapDimensionEx16    (GDI.478)
  */
-BOOL16 SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
-                               LPSIZE16 prevSize )
+BOOL16 WINAPI SetBitmapDimensionEx16( HBITMAP16 hbitmap, INT16 x, INT16 y,
+                                      LPSIZE16 prevSize )
 {
     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
     if (!bmp) return FALSE;
     if (prevSize) *prevSize = bmp->size;
     bmp->size.cx = x;
     bmp->size.cy = y;
+    GDI_HEAP_UNLOCK( hbitmap );
     return TRUE;
 }
 
@@ -698,14 +747,15 @@
 /***********************************************************************
  *           SetBitmapDimensionEx32    (GDI32.304)
  */
-BOOL32 SetBitmapDimensionEx32( HBITMAP32 hbitmap, INT32 x, INT32 y,
-                               LPSIZE32 prevSize )
+BOOL32 WINAPI SetBitmapDimensionEx32( HBITMAP32 hbitmap, INT32 x, INT32 y,
+                                      LPSIZE32 prevSize )
 {
     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
     if (!bmp) return FALSE;
     if (prevSize) CONV_SIZE16TO32( &bmp->size, prevSize );
     bmp->size.cx = (INT16)x;
     bmp->size.cy = (INT16)y;
+    GDI_HEAP_UNLOCK( hbitmap );
     return TRUE;
 }
 
@@ -713,9 +763,10 @@
 /***********************************************************************
  *           SetBitmapDimension    (GDI.163)
  */
-DWORD SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
+DWORD WINAPI SetBitmapDimension( HBITMAP16 hbitmap, INT16 x, INT16 y )
 {
     SIZE16 size;
     if (!SetBitmapDimensionEx16( hbitmap, x, y, &size )) return 0;
     return MAKELONG( size.cx, size.cy );
 }
+
diff --git a/objects/brush.c b/objects/brush.c
index 4a06005..5f216f2 100644
--- a/objects/brush.c
+++ b/objects/brush.c
@@ -16,15 +16,16 @@
 /***********************************************************************
  *           CreateBrushIndirect16    (GDI.50)
  */
-HBRUSH16 CreateBrushIndirect16( const LOGBRUSH16 * brush )
+HBRUSH16 WINAPI CreateBrushIndirect16( const LOGBRUSH16 * brush )
 {
     BRUSHOBJ * brushPtr;
     HBRUSH16 hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
     if (!hbrush) return 0;
-    brushPtr = (BRUSHOBJ *) GDI_HEAP_LIN_ADDR( hbrush );
+    brushPtr = (BRUSHOBJ *) GDI_HEAP_LOCK( hbrush );
     brushPtr->logbrush.lbStyle = brush->lbStyle;
     brushPtr->logbrush.lbColor = brush->lbColor;
     brushPtr->logbrush.lbHatch = brush->lbHatch;
+    GDI_HEAP_UNLOCK( hbrush );
     return hbrush;
 }
 
@@ -32,15 +33,16 @@
 /***********************************************************************
  *           CreateBrushIndirect32    (GDI32.27)
  */
-HBRUSH32 CreateBrushIndirect32( const LOGBRUSH32 * brush )
+HBRUSH32 WINAPI CreateBrushIndirect32( const LOGBRUSH32 * brush )
 {
     BRUSHOBJ * brushPtr;
     HBRUSH32 hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
     if (!hbrush) return 0;
-    brushPtr = (BRUSHOBJ *) GDI_HEAP_LIN_ADDR( hbrush );
+    brushPtr = (BRUSHOBJ *) GDI_HEAP_LOCK( hbrush );
     brushPtr->logbrush.lbStyle = brush->lbStyle;
     brushPtr->logbrush.lbColor = brush->lbColor;
     brushPtr->logbrush.lbHatch = brush->lbHatch;
+    GDI_HEAP_UNLOCK( hbrush );
     return hbrush;
 }
 
@@ -48,7 +50,7 @@
 /***********************************************************************
  *           CreateHatchBrush16    (GDI.58)
  */
-HBRUSH16 CreateHatchBrush16( INT16 style, COLORREF color )
+HBRUSH16 WINAPI CreateHatchBrush16( INT16 style, COLORREF color )
 {
     LOGBRUSH32 logbrush = { BS_HATCHED, color, style };
     dprintf_gdi(stddeb, "CreateHatchBrush16: %d %06lx\n", style, color );
@@ -60,7 +62,7 @@
 /***********************************************************************
  *           CreateHatchBrush32    (GDI32.48)
  */
-HBRUSH32 CreateHatchBrush32( INT32 style, COLORREF color )
+HBRUSH32 WINAPI CreateHatchBrush32( INT32 style, COLORREF color )
 {
     LOGBRUSH32 logbrush = { BS_HATCHED, color, style };
     dprintf_gdi(stddeb, "CreateHatchBrush32: %d %06lx\n", style, color );
@@ -72,7 +74,7 @@
 /***********************************************************************
  *           CreatePatternBrush16    (GDI.60)
  */
-HBRUSH16 CreatePatternBrush16( HBITMAP16 hbitmap )
+HBRUSH16 WINAPI CreatePatternBrush16( HBITMAP16 hbitmap )
 {
     return (HBRUSH16)CreatePatternBrush32( hbitmap );
 }
@@ -81,7 +83,7 @@
 /***********************************************************************
  *           CreatePatternBrush32    (GDI32.54)
  */
-HBRUSH32 CreatePatternBrush32( HBITMAP32 hbitmap )
+HBRUSH32 WINAPI CreatePatternBrush32( HBITMAP32 hbitmap )
 {
     LOGBRUSH32 logbrush = { BS_PATTERN, 0, 0 };
     BITMAPOBJ *bmp, *newbmp;
@@ -95,9 +97,15 @@
     logbrush.lbHatch = (INT32)CreateBitmapIndirect16( &bmp->bitmap );
     newbmp = (BITMAPOBJ *) GDI_GetObjPtr( (HGDIOBJ32)logbrush.lbHatch,
                                           BITMAP_MAGIC );
-    if (!newbmp) return 0;
+    if (!newbmp) 
+    {
+      GDI_HEAP_UNLOCK( hbitmap );
+      return 0;
+    }
     XCopyArea( display, bmp->pixmap, newbmp->pixmap, BITMAP_GC(bmp),
 	       0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 0, 0 );
+    GDI_HEAP_UNLOCK( hbitmap );
+    GDI_HEAP_UNLOCK( logbrush.lbHatch );
     return CreateBrushIndirect32( &logbrush );
 }
 
@@ -105,7 +113,7 @@
 /***********************************************************************
  *           CreateDIBPatternBrush16    (GDI.445)
  */
-HBRUSH16 CreateDIBPatternBrush16( HGLOBAL16 hbitmap, UINT16 coloruse )
+HBRUSH16 WINAPI CreateDIBPatternBrush16( HGLOBAL16 hbitmap, UINT16 coloruse )
 {
     LOGBRUSH32 logbrush = { BS_DIBPATTERN, coloruse, 0 };
     BITMAPINFO *info, *newInfo;
@@ -140,7 +148,7 @@
 /***********************************************************************
  *           CreateDIBPatternBrush32    (GDI32.34)
  */
-HBRUSH32 CreateDIBPatternBrush32( HGLOBAL32 hbitmap, UINT32 coloruse )
+HBRUSH32 WINAPI CreateDIBPatternBrush32( HGLOBAL32 hbitmap, UINT32 coloruse )
 {
     LOGBRUSH32 logbrush = { BS_DIBPATTERN, coloruse, 0 };
     BITMAPINFO *info, *newInfo;
@@ -175,7 +183,7 @@
 /***********************************************************************
  *           CreateSolidBrush    (GDI.66)
  */
-HBRUSH16 CreateSolidBrush16( COLORREF color )
+HBRUSH16 WINAPI CreateSolidBrush16( COLORREF color )
 {
     LOGBRUSH32 logbrush = { BS_SOLID, color, 0 };
     dprintf_gdi(stddeb, "CreateSolidBrush16: %06lx\n", color );
@@ -186,7 +194,7 @@
 /***********************************************************************
  *           CreateSolidBrush32    (GDI32.64)
  */
-HBRUSH32 CreateSolidBrush32( COLORREF color )
+HBRUSH32 WINAPI CreateSolidBrush32( COLORREF color )
 {
     LOGBRUSH32 logbrush = { BS_SOLID, color, 0 };
     dprintf_gdi(stddeb, "CreateSolidBrush32: %06lx\n", color );
@@ -197,7 +205,7 @@
 /***********************************************************************
  *           SetBrushOrg    (GDI.148)
  */
-DWORD SetBrushOrg( HDC16 hdc, INT16 x, INT16 y )
+DWORD WINAPI SetBrushOrg( HDC16 hdc, INT16 x, INT16 y )
 {
     DWORD retval;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -212,7 +220,7 @@
 /***********************************************************************
  *           SetBrushOrgEx    (GDI32.308)
  */
-BOOL32 SetBrushOrgEx( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 oldorg )
+BOOL32 WINAPI SetBrushOrgEx( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 oldorg )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
 
@@ -231,7 +239,7 @@
 /***********************************************************************
  *           GetSysColorBrush16    (USER.281)
  */
-HBRUSH16 GetSysColorBrush16( INT16 index )
+HBRUSH16 WINAPI GetSysColorBrush16( INT16 index )
 {
     fprintf( stderr, "Unimplemented stub: GetSysColorBrush16(%d)\n", index );
     return GetStockObject32(LTGRAY_BRUSH);
@@ -241,7 +249,7 @@
 /***********************************************************************
  *           GetSysColorBrush32    (USER32.289)
  */
-HBRUSH32 GetSysColorBrush32( INT32 index)
+HBRUSH32 WINAPI GetSysColorBrush32( INT32 index)
 {
     fprintf( stderr, "Unimplemented stub: GetSysColorBrush32(%d)\n", index );
     return GetStockObject32(LTGRAY_BRUSH);
diff --git a/objects/clipping.c b/objects/clipping.c
index e801cb0..d417c71 100644
--- a/objects/clipping.c
+++ b/objects/clipping.c
@@ -51,7 +51,7 @@
 /***********************************************************************
  *           SelectClipRgn16    (GDI.44)
  */
-INT16 SelectClipRgn16( HDC16 hdc, HRGN16 hrgn )
+INT16 WINAPI SelectClipRgn16( HDC16 hdc, HRGN16 hrgn )
 {
     return (INT16)SelectClipRgn32( hdc, hrgn );
 }
@@ -60,7 +60,7 @@
 /***********************************************************************
  *           SelectClipRgn32    (GDI32.297)
  */
-INT32 SelectClipRgn32( HDC32 hdc, HRGN32 hrgn )
+INT32 WINAPI SelectClipRgn32( HDC32 hdc, HRGN32 hrgn )
 {
     INT32 retval;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -81,6 +81,7 @@
     }
 
     CLIPPING_UpdateGCRegion( dc );
+    GDI_HEAP_UNLOCK( hdc );
     return retval;
 }
 
@@ -88,7 +89,7 @@
 /***********************************************************************
  *           SelectVisRgn    (GDI.105)
  */
-INT16 SelectVisRgn( HDC16 hdc, HRGN16 hrgn )
+INT16 WINAPI SelectVisRgn( HDC16 hdc, HRGN16 hrgn )
 {
     int retval;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -100,6 +101,7 @@
 
     retval = CombineRgn16( dc->w.hVisRgn, hrgn, 0, RGN_COPY );
     CLIPPING_UpdateGCRegion( dc );
+    GDI_HEAP_UNLOCK( hdc );
     return retval;
 }
 
@@ -107,7 +109,7 @@
 /***********************************************************************
  *           OffsetClipRgn16    (GDI.32)
  */
-INT16 OffsetClipRgn16( HDC16 hdc, INT16 x, INT16 y )
+INT16 WINAPI OffsetClipRgn16( HDC16 hdc, INT16 x, INT16 y )
 {
     return (INT16)OffsetClipRgn32( hdc, x, y );
 }
@@ -116,7 +118,7 @@
 /***********************************************************************
  *           OffsetClipRgn32    (GDI32.255)
  */
-INT32 OffsetClipRgn32( HDC32 hdc, INT32 x, INT32 y )
+INT32 WINAPI OffsetClipRgn32( HDC32 hdc, INT32 x, INT32 y )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) 
@@ -124,6 +126,7 @@
 	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
 	if (!dc) return ERROR;
 	MF_MetaParam2(dc, META_OFFSETCLIPRGN, x, y);
+	GDI_HEAP_UNLOCK( hdc );
 	return NULLREGION;   /* ?? */
     }
 
@@ -133,16 +136,18 @@
     {
 	INT32 ret = OffsetRgn32( dc->w.hClipRgn, XLPTODP(dc,x), YLPTODP(dc,y));
 	CLIPPING_UpdateGCRegion( dc );
+	GDI_HEAP_UNLOCK( hdc );
 	return ret;
     }
-    else return SIMPLEREGION; /* Clip region == client area */
+    GDI_HEAP_UNLOCK( hdc );
+    return SIMPLEREGION; /* Clip region == client area */
 }
 
 
 /***********************************************************************
  *           OffsetVisRgn    (GDI.102)
  */
-INT16 OffsetVisRgn( HDC16 hdc, INT16 x, INT16 y )
+INT16 WINAPI OffsetVisRgn( HDC16 hdc, INT16 x, INT16 y )
 {
     INT16 retval;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -150,6 +155,7 @@
     dprintf_clipping(stddeb, "OffsetVisRgn: %04x %d,%d\n", hdc, x, y );
     retval = OffsetRgn32( dc->w.hVisRgn, x, y );
     CLIPPING_UpdateGCRegion( dc );
+    GDI_HEAP_UNLOCK( hdc );
     return retval;
 }
 
@@ -194,8 +200,8 @@
 /***********************************************************************
  *           ExcludeClipRect16    (GDI.21)
  */
-INT16 ExcludeClipRect16( HDC16 hdc, INT16 left, INT16 top,
-                         INT16 right, INT16 bottom )
+INT16 WINAPI ExcludeClipRect16( HDC16 hdc, INT16 left, INT16 top,
+                                INT16 right, INT16 bottom )
 {
     return (INT16)ExcludeClipRect32( hdc, left, top, right, bottom );
 }
@@ -204,15 +210,17 @@
 /***********************************************************************
  *           ExcludeClipRect32    (GDI32.92)
  */
-INT32 ExcludeClipRect32( HDC32 hdc, INT32 left, INT32 top,
-                         INT32 right, INT32 bottom )
+INT32 WINAPI ExcludeClipRect32( HDC32 hdc, INT32 left, INT32 top,
+                                INT32 right, INT32 bottom )
 {
+    INT32 ret;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) 
     {
 	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
 	if (!dc) return ERROR;
 	MF_MetaParam4(dc, META_EXCLUDECLIPRECT, left, top, right, bottom);
+	GDI_HEAP_UNLOCK( hdc );
 	return NULLREGION;   /* ?? */
     }
 
@@ -223,15 +231,17 @@
 
     dprintf_clipping(stddeb, "ExcludeClipRect: %04x %dx%d,%dx%d\n",
 	    hdc, left, top, right, bottom );
-    return CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_EXCLUDE );
+    ret = CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_EXCLUDE );
+    GDI_HEAP_UNLOCK( hdc );
+    return ret;
 }
 
 
 /***********************************************************************
  *           IntersectClipRect16    (GDI.22)
  */
-INT16 IntersectClipRect16( HDC16 hdc, INT16 left, INT16 top,
-                           INT16 right, INT16 bottom )
+INT16 WINAPI IntersectClipRect16( HDC16 hdc, INT16 left, INT16 top,
+                                  INT16 right, INT16 bottom )
 {
     return (INT16)IntersectClipRect32( hdc, left, top, right, bottom );
 }
@@ -240,15 +250,17 @@
 /***********************************************************************
  *           IntersectClipRect32    (GDI32.245)
  */
-INT32 IntersectClipRect32( HDC32 hdc, INT32 left, INT32 top,
-                           INT32 right, INT32 bottom )
+INT32 WINAPI IntersectClipRect32( HDC32 hdc, INT32 left, INT32 top,
+                                  INT32 right, INT32 bottom )
 {
+    INT32 ret;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) 
     {
 	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
 	if (!dc) return ERROR;
 	MF_MetaParam4(dc, META_INTERSECTCLIPRECT, left, top, right, bottom);
+	GDI_HEAP_UNLOCK( hdc );
 	return NULLREGION;   /* ?? */
     }
 
@@ -259,7 +271,9 @@
 
     dprintf_clipping(stddeb, "IntersectClipRect: %04x %dx%d,%dx%d\n",
 	    hdc, left, top, right, bottom );
-    return CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_INTERSECT );
+    ret = CLIPPING_IntersectClipRect( dc, left, top, right, bottom, CLIP_INTERSECT );
+    GDI_HEAP_UNLOCK( hdc );
+    return ret;
 }
 
 
@@ -298,6 +312,7 @@
         DeleteObject32( dc->w.hVisRgn );
         dc->w.hVisRgn = newRgn;    
         CLIPPING_UpdateGCRegion( dc );
+	GDI_HEAP_UNLOCK( newRgn );
     }
     else DeleteObject32( newRgn );
     return ret;
@@ -307,8 +322,8 @@
 /***********************************************************************
  *           ExcludeVisRect    (GDI.73)
  */
-INT16 ExcludeVisRect( HDC16 hdc, INT16 left, INT16 top,
-                      INT16 right, INT16 bottom )
+INT16 WINAPI ExcludeVisRect( HDC16 hdc, INT16 left, INT16 top,
+                             INT16 right, INT16 bottom )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) return ERROR;    
@@ -322,8 +337,8 @@
 /***********************************************************************
  *           IntersectVisRect    (GDI.98)
  */
-INT16 IntersectVisRect( HDC16 hdc, INT16 left, INT16 top,
-                        INT16 right, INT16 bottom )
+INT16 WINAPI IntersectVisRect( HDC16 hdc, INT16 left, INT16 top,
+                               INT16 right, INT16 bottom )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) return ERROR;    
@@ -337,7 +352,7 @@
 /***********************************************************************
  *           PtVisible16    (GDI.103)
  */
-BOOL16 PtVisible16( HDC16 hdc, INT16 x, INT16 y )
+BOOL16 WINAPI PtVisible16( HDC16 hdc, INT16 x, INT16 y )
 {
     return PtVisible32( hdc, x, y );
 }
@@ -346,7 +361,7 @@
 /***********************************************************************
  *           PtVisible32    (GDI32.279)
  */
-BOOL32 PtVisible32( HDC32 hdc, INT32 x, INT32 y )
+BOOL32 WINAPI PtVisible32( HDC32 hdc, INT32 x, INT32 y )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) return ERROR;    
@@ -364,7 +379,7 @@
 /***********************************************************************
  *           RectVisible16    (GDI.104)
  */
-BOOL16 RectVisible16( HDC16 hdc, LPRECT16 rect )
+BOOL16 WINAPI RectVisible16( HDC16 hdc, LPRECT16 rect )
 {
     RECT16 tmpRect;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -382,7 +397,7 @@
 /***********************************************************************
  *           RectVisible32    (GDI32.282)
  */
-BOOL32 RectVisible32( HDC32 hdc, LPRECT32 rect )
+BOOL32 WINAPI RectVisible32( HDC32 hdc, LPRECT32 rect )
 {
     RECT16 rect16;
     CONV_RECT32TO16( rect, &rect16 );
@@ -393,7 +408,7 @@
 /***********************************************************************
  *           GetClipBox16    (GDI.77)
  */
-INT16 GetClipBox16( HDC16 hdc, LPRECT16 rect )
+INT16 WINAPI GetClipBox16( HDC16 hdc, LPRECT16 rect )
 {
     int ret;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -407,7 +422,7 @@
 /***********************************************************************
  *           GetClipBox32    (GDI32.162)
  */
-INT32 GetClipBox32( HDC32 hdc, LPRECT32 rect )
+INT32 WINAPI GetClipBox32( HDC32 hdc, LPRECT32 rect )
 {
     INT32 ret;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -421,7 +436,7 @@
 /***********************************************************************
  *           GetClipRgn32  (GDI32.163)
  */
-INT32 GetClipRgn32( HDC32 hdc, HRGN32 hRgn )
+INT32 WINAPI GetClipRgn32( HDC32 hdc, HRGN32 hRgn )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if( dc && hRgn )
@@ -440,7 +455,7 @@
 /***********************************************************************
  *           SaveVisRgn    (GDI.129)
  */
-HRGN16 SaveVisRgn( HDC16 hdc )
+HRGN16 WINAPI SaveVisRgn( HDC16 hdc )
 {
     HRGN32 copy;
     RGNOBJ *obj, *copyObj;
@@ -456,13 +471,28 @@
     dc->w.flags &= ~DC_DIRTY;
 
     if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
+    {
+        GDI_HEAP_UNLOCK( hdc );
 	return 0;
-    if (!(copy = CreateRectRgn32( 0, 0, 0, 0 ))) return 0;
+    }
+    if (!(copy = CreateRectRgn32( 0, 0, 0, 0 )))
+    {
+        GDI_HEAP_UNLOCK( dc->w.hVisRgn );
+        GDI_HEAP_UNLOCK( hdc );
+        return 0;
+    }  
     CombineRgn32( copy, dc->w.hVisRgn, 0, RGN_COPY );
     if (!(copyObj = (RGNOBJ *) GDI_GetObjPtr( copy, REGION_MAGIC )))
+    {
+        GDI_HEAP_UNLOCK( dc->w.hVisRgn );
+        GDI_HEAP_UNLOCK( hdc );
 	return 0;
+    }
     copyObj->header.hNext = obj->header.hNext;
     obj->header.hNext = copy;
+    GDI_HEAP_UNLOCK( dc->w.hVisRgn );
+    GDI_HEAP_UNLOCK( hdc );
+    GDI_HEAP_UNLOCK( copy );
     return copy;
 }
 
@@ -470,20 +500,39 @@
 /***********************************************************************
  *           RestoreVisRgn    (GDI.130)
  */
-INT16 RestoreVisRgn( HDC16 hdc )
+INT16 WINAPI RestoreVisRgn( HDC16 hdc )
 {
     HRGN32 saved;
     RGNOBJ *obj, *savedObj;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
-    if (!dc || !dc->w.hVisRgn) return ERROR;    
+    if (!dc) return ERROR;    
+    if (!dc->w.hVisRgn)
+    {
+        GDI_HEAP_UNLOCK( hdc );
+        return ERROR;    
+    }
     dprintf_clipping(stddeb, "RestoreVisRgn: %04x\n", hdc );
     if (!(obj = (RGNOBJ *) GDI_GetObjPtr( dc->w.hVisRgn, REGION_MAGIC )))
+    {
+        GDI_HEAP_UNLOCK( hdc );
 	return ERROR;
-    if (!(saved = obj->header.hNext)) return ERROR;
+    }
+    if (!(saved = obj->header.hNext)) 
+    {
+        GDI_HEAP_UNLOCK( dc->w.hVisRgn );
+        GDI_HEAP_UNLOCK( hdc );
+        return ERROR;
+    }
     if (!(savedObj = (RGNOBJ *) GDI_GetObjPtr( saved, REGION_MAGIC )))
-	return ERROR;
+    {
+        GDI_HEAP_UNLOCK( dc->w.hVisRgn );
+        GDI_HEAP_UNLOCK( hdc );
+        return ERROR;
+    }
     DeleteObject32( dc->w.hVisRgn );
     dc->w.hVisRgn = saved;
     CLIPPING_UpdateGCRegion( dc );
+    GDI_HEAP_UNLOCK( hdc );
+    GDI_HEAP_UNLOCK( saved );
     return savedObj->xrgn ? COMPLEXREGION : NULLREGION;
 }
diff --git a/objects/color.c b/objects/color.c
index 7d5c0bd..b07b790 100644
--- a/objects/color.c
+++ b/objects/color.c
@@ -857,6 +857,7 @@
     WORD 		 index = 0;
     HPALETTE16		 hPal = (dc)? dc->w.hPalette: STOCK_DEFAULT_PALETTE;
     unsigned char	 spec_type = color >> 24;
+    PALETTEOBJ* 	 palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
 
     if ( cSpace.flags & COLOR_FIXED )
     {
@@ -866,7 +867,6 @@
 
 	unsigned 	long red, green, blue;
 	unsigned 	idx = 0;
-	PALETTEOBJ * 	palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
 
 	switch(spec_type)
         {
@@ -876,7 +876,11 @@
 					    palPtr->logpalette.palNumEntries,
 					    NULL, color, FALSE);
 
-            if( palPtr->mapping ) return palPtr->mapping[idx];
+            if( palPtr->mapping )
+	    {
+	        GDI_HEAP_UNLOCK( hPal );
+		return palPtr->mapping[idx];
+	    }
 
 	    color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
 	    break;
@@ -886,11 +890,15 @@
             if( (idx = color & 0xffff) >= palPtr->logpalette.palNumEntries)
             {
                 fprintf(stderr, "\tRGB(%lx) : idx %d is out of bounds, assuming black\n", color, idx);
+		GDI_HEAP_UNLOCK( hPal );
                 return 0;
             }
 
-            if( palPtr->mapping ) return palPtr->mapping[idx];
-
+            if( palPtr->mapping ) 
+	    {
+		GDI_HEAP_UNLOCK( hPal );
+		return palPtr->mapping[idx];
+	    }
 	    color = *(COLORREF*)(palPtr->logpalette.palPalEntry + idx);
 	    break;
 
@@ -900,8 +908,12 @@
 
 	  case 0: /* RGB */
 	    if( dc && (dc->w.bitsPerPixel == 1) )
+	    {
+		GDI_HEAP_UNLOCK( hPal );
 		return (((color >> 16) & 0xff) +
 			((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
+	    }
+
 	}
 
         red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color);
@@ -909,6 +921,7 @@
 	if (COLOR_Graymax)
         {
 	    /* grayscale only; return scaled value */
+	    GDI_HEAP_UNLOCK( hPal );
             return ( (red * 30 + green * 69 + blue * 11) * COLOR_Graymax) / 25500;
 	}
 	else
@@ -918,12 +931,12 @@
             if (COLOR_Greenmax != 255) green = (green * COLOR_Greenmax) / 255;
 	    if (COLOR_Bluemax != 255) blue = (blue * COLOR_Bluemax) / 255;
 
+	    GDI_HEAP_UNLOCK( hPal );
 	    return (red << COLOR_Redshift) | (green << COLOR_Greenshift) | (blue << COLOR_Blueshift);
         }
     }
     else 
     {
-	PALETTEOBJ* palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC);
 
 	/* palPtr can be NULL when DC is being destroyed */
 
@@ -939,8 +952,11 @@
 
        	    case 0:  /* RGB */
 		if( dc && (dc->w.bitsPerPixel == 1) )
+		{
+		    GDI_HEAP_UNLOCK( hPal );
 		    return (((color >> 16) & 0xff) +
 			    ((color >> 8) & 0xff) + (color & 0xff) > 255*3/2) ? 1 : 0;
+		}
 
 	    	index = COLOR_PaletteLookupPixel( COLOR_sysPal, 256, 
 						  COLOR_PaletteToPixel, color, FALSE);
@@ -968,6 +984,7 @@
 	}
     }
 
+    GDI_HEAP_UNLOCK( hPal );
     return index;
 }
 
@@ -977,12 +994,11 @@
  * Set the color-mapping table for selected palette. 
  * Return number of entries which mapping has changed.
  */
-int COLOR_SetMapping( PALETTEOBJ* palPtr, BOOL32 mapOnly )
+int COLOR_SetMapping( PALETTEOBJ* palPtr, UINT32 uStart, UINT32 uNum, BOOL32 mapOnly )
 {
-    int  i, index;
     char flag;
     int  prevMapping = (palPtr->mapping) ? 1 : 0;
-    int  iRemapped = 0;
+    int  index, iRemapped = 0;
 
     /* reset dynamic system palette entries */
 
@@ -994,15 +1010,15 @@
     palPtr->mapping = (int*)xrealloc(palPtr->mapping, sizeof(int)*
                                      palPtr->logpalette.palNumEntries);
 
-    for( i = 0; i < palPtr->logpalette.palNumEntries; i++ )
+    for( uNum += uStart; uStart < uNum; uStart++ )
     {
         index = -1;
         flag = PC_SYS_USED;
 
-        switch( palPtr->logpalette.palPalEntry[i].peFlags & 0x07 )
+        switch( palPtr->logpalette.palPalEntry[uStart].peFlags & 0x07 )
         {
 	case PC_EXPLICIT:   /* palette entries are indices into system palette */
-            index = *(WORD*)(palPtr->logpalette.palPalEntry + i);
+            index = *(WORD*)(palPtr->logpalette.palPalEntry + uStart);
             if( index > 255 || (index >= COLOR_gapStart && index <= COLOR_gapEnd) ) 
             {
                 fprintf(stderr,"PC_EXPLICIT: idx %d out of system palette, assuming black.\n", index); 
@@ -1016,7 +1032,7 @@
             /* fall through */
 	default:	    /* try to collapse identical colors */
             index = COLOR_PaletteLookupExactIndex(COLOR_sysPal, 256,  
-                             *(COLORREF*)(palPtr->logpalette.palPalEntry + i));
+                             *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
             /* fall through */
 	case PC_NOCOLLAPSE: 
             if( index < 0 )
@@ -1028,13 +1044,13 @@
                     COLOR_firstFree = COLOR_freeList[index];
 
                     color.pixel = (COLOR_PaletteToPixel) ? COLOR_PaletteToPixel[index] : index;
-                    color.red = palPtr->logpalette.palPalEntry[i].peRed << 8;
-                    color.green = palPtr->logpalette.palPalEntry[i].peGreen << 8;
-                    color.blue = palPtr->logpalette.palPalEntry[i].peBlue << 8;
+                    color.red = palPtr->logpalette.palPalEntry[uStart].peRed << 8;
+                    color.green = palPtr->logpalette.palPalEntry[uStart].peGreen << 8;
+                    color.blue = palPtr->logpalette.palPalEntry[uStart].peBlue << 8;
                     color.flags = DoRed | DoGreen | DoBlue;
                     XStoreColor(display, cSpace.colorMap, &color);
 
-                    COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[i];
+                    COLOR_sysPal[index] = palPtr->logpalette.palPalEntry[uStart];
                     COLOR_sysPal[index].peFlags = flag;
 		    COLOR_freeList[index] = 0;
 
@@ -1044,27 +1060,28 @@
                 else if ( cSpace.flags & COLOR_VIRTUAL ) 
                 {
                     index = COLOR_ToPhysical( NULL, 0x00ffffff &
-                             *(COLORREF*)(palPtr->logpalette.palPalEntry + i));
+                             *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart));
                     break;     
                 }
 
                 /* we have to map to existing entry in the system palette */
 
                 index = COLOR_PaletteLookupPixel(COLOR_sysPal, 256, NULL, 
-                       *(COLORREF*)(palPtr->logpalette.palPalEntry + i), TRUE);
+                       *(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), TRUE);
             }
-	    palPtr->logpalette.palPalEntry[i].peFlags |= PC_SYS_USED;
+	    palPtr->logpalette.palPalEntry[uStart].peFlags |= PC_SYS_USED;
 
             if( COLOR_PaletteToPixel ) index = COLOR_PaletteToPixel[index];
             break;
         }
 
-        if( !prevMapping || palPtr->mapping[i] != index ) iRemapped++;
-        palPtr->mapping[i] = index;
+        if( !prevMapping || palPtr->mapping[uStart] != index ) iRemapped++;
+        palPtr->mapping[uStart] = index;
 
-        dprintf_palette(stddeb,"\tentry %i (%lx) -> pixel %i\n", i, 
-				*(COLORREF*)(palPtr->logpalette.palPalEntry + i), index);
+        dprintf_palette(stddeb,"\tentry %i (%lx) -> pixel %i\n", uStart, 
+				*(COLORREF*)(palPtr->logpalette.palPalEntry + uStart), index);
 	
     }
     return iRemapped;
 }
+
diff --git a/objects/cursoricon.c b/objects/cursoricon.c
index e8a9537..317f163 100644
--- a/objects/cursoricon.c
+++ b/objects/cursoricon.c
@@ -522,7 +522,7 @@
 /***********************************************************************
  *           LoadCursor16    (USER.173)
  */
-HCURSOR16 LoadCursor16( HINSTANCE16 hInstance, SEGPTR name )
+HCURSOR16 WINAPI LoadCursor16( HINSTANCE16 hInstance, SEGPTR name )
 {
     if (HIWORD(name))
         dprintf_cursor( stddeb, "LoadCursor16: %04x '%s'\n",
@@ -539,7 +539,7 @@
 /***********************************************************************
  *           LoadIcon16    (USER.174)
  */
-HICON16 LoadIcon16( HINSTANCE16 hInstance, SEGPTR name )
+HICON16 WINAPI LoadIcon16( HINSTANCE16 hInstance, SEGPTR name )
 {
     if (HIWORD(name))
         dprintf_icon( stddeb, "LoadIcon: %04x '%s'\n",
@@ -557,9 +557,10 @@
 /***********************************************************************
  *           CreateCursor16    (USER.406)
  */
-HCURSOR16 CreateCursor16(HINSTANCE16 hInstance, INT16 xHotSpot, INT16 yHotSpot,
-                         INT16 nWidth, INT16 nHeight,
-                         LPCVOID lpANDbits, LPCVOID lpXORbits )
+HCURSOR16 WINAPI CreateCursor16( HINSTANCE16 hInstance,
+                                 INT16 xHotSpot, INT16 yHotSpot,
+                                 INT16 nWidth, INT16 nHeight,
+                                 LPCVOID lpANDbits, LPCVOID lpXORbits )
 {
     CURSORICONINFO info = { { xHotSpot, yHotSpot }, nWidth, nHeight, 0, 1, 1 };
 
@@ -572,9 +573,10 @@
 /***********************************************************************
  *           CreateCursor32    (USER32.66)
  */
-HCURSOR32 CreateCursor32(HINSTANCE32 hInstance, INT32 xHotSpot, INT32 yHotSpot,
-                         INT32 nWidth, INT32 nHeight,
-                         LPCVOID lpANDbits, LPCVOID lpXORbits )
+HCURSOR32 WINAPI CreateCursor32( HINSTANCE32 hInstance,
+                                 INT32 xHotSpot, INT32 yHotSpot,
+                                 INT32 nWidth, INT32 nHeight,
+                                 LPCVOID lpANDbits, LPCVOID lpXORbits )
 {
     CURSORICONINFO info = { { xHotSpot, yHotSpot }, nWidth, nHeight, 0, 1, 1 };
 
@@ -587,9 +589,9 @@
 /***********************************************************************
  *           CreateIcon16    (USER.407)
  */
-HICON16 CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth, INT16 nHeight,
-                      BYTE bPlanes, BYTE bBitsPixel,
-                      LPCVOID lpANDbits, LPCVOID lpXORbits )
+HICON16 WINAPI CreateIcon16( HINSTANCE16 hInstance, INT16 nWidth,
+                             INT16 nHeight, BYTE bPlanes, BYTE bBitsPixel,
+                             LPCVOID lpANDbits, LPCVOID lpXORbits )
 {
     CURSORICONINFO info = { { 0, 0 }, nWidth, nHeight, 0, bPlanes, bBitsPixel};
 
@@ -602,9 +604,9 @@
 /***********************************************************************
  *           CreateIcon32    (USER32.74)
  */
-HICON32 CreateIcon32( HINSTANCE32 hInstance, INT32 nWidth, INT32 nHeight,
-                      BYTE bPlanes, BYTE bBitsPixel,
-                      LPCVOID lpANDbits, LPCVOID lpXORbits )
+HICON32 WINAPI CreateIcon32( HINSTANCE32 hInstance, INT32 nWidth,
+                             INT32 nHeight, BYTE bPlanes, BYTE bBitsPixel,
+                             LPCVOID lpANDbits, LPCVOID lpXORbits )
 {
     CURSORICONINFO info = { { 0, 0 }, nWidth, nHeight, 0, bPlanes, bBitsPixel};
 
@@ -617,8 +619,10 @@
 /***********************************************************************
  *           CreateCursorIconIndirect    (USER.408)
  */
-HGLOBAL16 CreateCursorIconIndirect(HINSTANCE16 hInstance, CURSORICONINFO *info,
-                                   LPCVOID lpANDbits, LPCVOID lpXORbits )
+HGLOBAL16 WINAPI CreateCursorIconIndirect( HINSTANCE16 hInstance,
+                                           CURSORICONINFO *info,
+                                           LPCVOID lpANDbits,
+                                           LPCVOID lpXORbits )
 {
     HGLOBAL16 handle;
     char *ptr;
@@ -644,7 +648,7 @@
 /***********************************************************************
  *           CopyIcon16    (USER.368)
  */
-HICON16 CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
+HICON16 WINAPI CopyIcon16( HINSTANCE16 hInstance, HICON16 hIcon )
 {
     dprintf_icon( stddeb, "CopyIcon16: %04x %04x\n", hInstance, hIcon );
     return CURSORICON_Copy( hInstance, hIcon );
@@ -654,7 +658,7 @@
 /***********************************************************************
  *           CopyIcon32    (USER32.59)
  */
-HICON32 CopyIcon32( HICON32 hIcon )
+HICON32 WINAPI CopyIcon32( HICON32 hIcon )
 {
     dprintf_icon( stddeb, "CopyIcon32: %04x\n", hIcon );
     return CURSORICON_Copy( 0, hIcon );
@@ -664,7 +668,7 @@
 /***********************************************************************
  *           CopyCursor16    (USER.369)
  */
-HCURSOR16 CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
+HCURSOR16 WINAPI CopyCursor16( HINSTANCE16 hInstance, HCURSOR16 hCursor )
 {
     dprintf_cursor( stddeb, "CopyCursor16: %04x %04x\n", hInstance, hCursor );
     return CURSORICON_Copy( hInstance, hCursor );
@@ -674,7 +678,7 @@
 /***********************************************************************
  *           DestroyIcon16    (USER.457)
  */
-BOOL16 DestroyIcon16( HICON16 hIcon )
+BOOL16 WINAPI DestroyIcon16( HICON16 hIcon )
 {
     return DestroyIcon32( hIcon );
 }
@@ -683,7 +687,7 @@
 /***********************************************************************
  *           DestroyIcon32    (USER32.132)
  */
-BOOL32 DestroyIcon32( HICON32 hIcon )
+BOOL32 WINAPI DestroyIcon32( HICON32 hIcon )
 {
     dprintf_icon( stddeb, "DestroyIcon: %04x\n", hIcon );
     /* FIXME: should check for OEM icon here */
@@ -694,7 +698,7 @@
 /***********************************************************************
  *           DestroyCursor16    (USER.458)
  */
-BOOL16 DestroyCursor16( HCURSOR16 hCursor )
+BOOL16 WINAPI DestroyCursor16( HCURSOR16 hCursor )
 {
     return DestroyCursor32( hCursor );
 }
@@ -703,7 +707,7 @@
 /***********************************************************************
  *           DestroyCursor32    (USER32.131)
  */
-BOOL32 DestroyCursor32( HCURSOR32 hCursor )
+BOOL32 WINAPI DestroyCursor32( HCURSOR32 hCursor )
 {
     dprintf_cursor( stddeb, "DestroyCursor: %04x\n", hCursor );
     /* FIXME: should check for OEM cursor here */
@@ -714,7 +718,7 @@
 /***********************************************************************
  *           DrawIcon16    (USER.84)
  */
-BOOL16 DrawIcon16( HDC16 hdc, INT16 x, INT16 y, HICON16 hIcon )
+BOOL16 WINAPI DrawIcon16( HDC16 hdc, INT16 x, INT16 y, HICON16 hIcon )
 {
     return DrawIcon32( hdc, x, y, hIcon );
 }
@@ -723,7 +727,7 @@
 /***********************************************************************
  *           DrawIcon32    (USER32.158)
  */
-BOOL32 DrawIcon32( HDC32 hdc, INT32 x, INT32 y, HICON32 hIcon )
+BOOL32 WINAPI DrawIcon32( HDC32 hdc, INT32 x, INT32 y, HICON32 hIcon )
 {
     CURSORICONINFO *ptr;
     HDC32 hMemDC;
@@ -761,8 +765,8 @@
 /***********************************************************************
  *           DumpIcon    (USER.459)
  */
-DWORD DumpIcon( SEGPTR pInfo, WORD *lpLen,
-                SEGPTR *lpXorBits, SEGPTR *lpAndBits )
+DWORD WINAPI DumpIcon( SEGPTR pInfo, WORD *lpLen,
+                       SEGPTR *lpXorBits, SEGPTR *lpAndBits )
 {
     CURSORICONINFO *info = PTR_SEG_TO_LIN( pInfo );
     int sizeAnd, sizeXor;
@@ -833,9 +837,8 @@
             image->bitmap_unit = 16;
             _XInitImageFuncPtrs(image);
             if (pixmapAll)
-                CallTo32_LargeStack( XPutImage, 10,
-                                     display, pixmapAll, BITMAP_monoGC, image,
-                                     0, 0, 0, 0, ptr->nWidth, ptr->nHeight*2 );
+                XPutImage( display, pixmapAll, BITMAP_monoGC, image,
+                           0, 0, 0, 0, ptr->nWidth, ptr->nHeight * 2 );
             image->data = NULL;
             XDestroyImage( image );
         }
@@ -925,7 +928,7 @@
 /***********************************************************************
  *           SetCursor16    (USER.69)
  */
-HCURSOR16 SetCursor16( HCURSOR16 hCursor )
+HCURSOR16 WINAPI SetCursor16( HCURSOR16 hCursor )
 {
     return (HCURSOR16)SetCursor32( hCursor );
 }
@@ -934,7 +937,7 @@
 /***********************************************************************
  *           SetCursor32    (USER32.471)
  */
-HCURSOR32 SetCursor32( HCURSOR32 hCursor )
+HCURSOR32 WINAPI SetCursor32( HCURSOR32 hCursor )
 {
     HCURSOR32 hOldCursor;
 
@@ -943,7 +946,8 @@
     hOldCursor = hActiveCursor;
     hActiveCursor = hCursor;
     /* Change the cursor shape only if it is visible */
-    if (CURSOR_ShowCount >= 0) CURSORICON_SetCursor( hActiveCursor );
+    if (CURSOR_ShowCount >= 0)
+        CALL_LARGE_STACK( CURSORICON_SetCursor, hActiveCursor );
     return hOldCursor;
 }
 
@@ -951,7 +955,7 @@
 /***********************************************************************
  *           SetCursorPos16    (USER.70)
  */
-void SetCursorPos16( INT16 x, INT16 y )
+void WINAPI SetCursorPos16( INT16 x, INT16 y )
 {
     SetCursorPos32( x, y );
 }
@@ -960,7 +964,7 @@
 /***********************************************************************
  *           SetCursorPos32    (USER32.473)
  */
-BOOL32 SetCursorPos32( INT32 x, INT32 y )
+BOOL32 WINAPI SetCursorPos32( INT32 x, INT32 y )
 {
     dprintf_cursor( stddeb, "SetCursorPos: x=%d y=%d\n", x, y );
     XWarpPointer( display, rootWindow, rootWindow, 0, 0, 0, 0, x, y );
@@ -971,7 +975,7 @@
 /***********************************************************************
  *           ShowCursor16    (USER.71)
  */
-INT16 ShowCursor16( BOOL16 bShow )
+INT16 WINAPI ShowCursor16( BOOL16 bShow )
 {
     return ShowCursor32( bShow );
 }
@@ -980,20 +984,20 @@
 /***********************************************************************
  *           ShowCursor32    (USER32.529)
  */
-INT32 ShowCursor32( BOOL32 bShow )
+INT32 WINAPI ShowCursor32( BOOL32 bShow )
 {
     dprintf_cursor( stddeb, "ShowCursor: %d, count=%d\n",
                     bShow, CURSOR_ShowCount );
 
     if (bShow)
     {
-        if (++CURSOR_ShowCount == 0)
-            CURSORICON_SetCursor( hActiveCursor );  /* Show it */
+        if (++CURSOR_ShowCount == 0)  /* Show it */
+            CALL_LARGE_STACK( CURSORICON_SetCursor, hActiveCursor );
     }
     else
     {
-        if (--CURSOR_ShowCount == -1)
-            CURSORICON_SetCursor( 0 );  /* Hide it */
+        if (--CURSOR_ShowCount == -1)  /* Hide it */
+            CALL_LARGE_STACK( CURSORICON_SetCursor, 0 );
     }
     return CURSOR_ShowCount;
 }
@@ -1002,7 +1006,7 @@
 /***********************************************************************
  *           GetCursor16    (USER.247)
  */
-HCURSOR16 GetCursor16(void)
+HCURSOR16 WINAPI GetCursor16(void)
 {
     return hActiveCursor;
 }
@@ -1011,7 +1015,7 @@
 /***********************************************************************
  *           GetCursor32    (USER32.226)
  */
-HCURSOR32 GetCursor32(void)
+HCURSOR32 WINAPI GetCursor32(void)
 {
     return hActiveCursor;
 }
@@ -1020,7 +1024,7 @@
 /***********************************************************************
  *           ClipCursor16    (USER.16)
  */
-BOOL16 ClipCursor16( const RECT16 *rect )
+BOOL16 WINAPI ClipCursor16( const RECT16 *rect )
 {
     if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
     else CONV_RECT16TO32( rect, &CURSOR_ClipRect );
@@ -1031,7 +1035,7 @@
 /***********************************************************************
  *           ClipCursor32    (USER32.52)
  */
-BOOL32 ClipCursor32( const RECT32 *rect )
+BOOL32 WINAPI ClipCursor32( const RECT32 *rect )
 {
     if (!rect) SetRectEmpty32( &CURSOR_ClipRect );
     else CopyRect32( &CURSOR_ClipRect, rect );
@@ -1042,7 +1046,7 @@
 /***********************************************************************
  *           GetCursorPos16    (USER.17)
  */
-void GetCursorPos16( POINT16 *pt )
+void WINAPI GetCursorPos16( POINT16 *pt )
 {
     Window root, child;
     int rootX, rootY, childX, childY;
@@ -1064,7 +1068,7 @@
 /***********************************************************************
  *           GetCursorPos32    (USER32.228)
  */
-void GetCursorPos32( POINT32 *pt )
+void WINAPI GetCursorPos32( POINT32 *pt )
 {
     POINT16 pt16;
     GetCursorPos16( &pt16 );
@@ -1075,7 +1079,7 @@
 /***********************************************************************
  *           GetClipCursor16    (USER.309)
  */
-void GetClipCursor16( RECT16 *rect )
+void WINAPI GetClipCursor16( RECT16 *rect )
 {
     if (rect) CONV_RECT32TO16( &CURSOR_ClipRect, rect );
 }
@@ -1084,7 +1088,7 @@
 /***********************************************************************
  *           GetClipCursor32    (USER32.220)
  */
-void GetClipCursor32( RECT32 *rect )
+void WINAPI GetClipCursor32( RECT32 *rect )
 {
     if (rect) CopyRect32( rect, &CURSOR_ClipRect );
 }
@@ -1093,7 +1097,7 @@
 /**********************************************************************
  *	    GetIconID    (USER.455)
  */
-WORD GetIconID( HGLOBAL16 hResource, DWORD resType )
+WORD WINAPI GetIconID( HGLOBAL16 hResource, DWORD resType )
 {
     CURSORICONDIR *lpDir = (CURSORICONDIR *)GlobalLock16(hResource);
 /* LockResource16(hResource); */
@@ -1132,7 +1136,7 @@
 /**********************************************************************
  *	    LoadIconHandler    (USER.456)
  */
-HICON16 LoadIconHandler( HGLOBAL16 hResource, BOOL16 bNew )
+HICON16 WINAPI LoadIconHandler( HGLOBAL16 hResource, BOOL16 bNew )
 {
     dprintf_cursor(stddeb,"LoadIconHandler: hRes=%04x\n",hResource);
 
diff --git a/objects/dc.c b/objects/dc.c
index 0210fc3..77d3996 100644
--- a/objects/dc.c
+++ b/objects/dc.c
@@ -141,7 +141,7 @@
     DC *dc;
 
     if (!(hdc = GDI_AllocObject( sizeof(DC), DC_MAGIC ))) return NULL;
-    dc = (DC *) GDI_HEAP_LIN_ADDR( hdc );
+    dc = (DC *) GDI_HEAP_LOCK( hdc );
 
     dc->hSelf      = hdc;
     dc->funcs      = funcs;
@@ -163,15 +163,17 @@
 }
 
 
+
 /***********************************************************************
  *           DC_GetDCPtr
  */
 DC *DC_GetDCPtr( HDC32 hdc )
 {
-    GDIOBJHDR *ptr = (GDIOBJHDR *)GDI_HEAP_LIN_ADDR( hdc );
+    GDIOBJHDR *ptr = (GDIOBJHDR *)GDI_HEAP_LOCK( hdc );
     if (!ptr) return NULL;
     if ((ptr->wMagic == DC_MAGIC) || (ptr->wMagic == METAFILE_DC_MAGIC))
         return (DC *)ptr;
+    GDI_HEAP_UNLOCK( hdc );
     return NULL;
 }
 
@@ -380,14 +382,18 @@
 /***********************************************************************
  *           GetDCState    (GDI.179)
  */
-HDC16 GetDCState( HDC16 hdc )
+HDC16 WINAPI GetDCState( HDC16 hdc )
 {
     DC * newdc, * dc;
     HGDIOBJ16 handle;
     
     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
-    if (!(handle = GDI_AllocObject( sizeof(DC), DC_MAGIC ))) return 0;
-    newdc = (DC *) GDI_HEAP_LIN_ADDR( handle );
+    if (!(handle = GDI_AllocObject( sizeof(DC), DC_MAGIC )))
+    {
+      GDI_HEAP_UNLOCK( hdc );
+      return 0;
+    }
+    newdc = (DC *) GDI_HEAP_LOCK( handle );
 
     dprintf_dc(stddeb, "GetDCState(%04x): returning %04x\n", hdc, handle );
 
@@ -446,6 +452,8 @@
     }
     else
 	newdc->w.hClipRgn = 0;
+    GDI_HEAP_UNLOCK( handle );
+    GDI_HEAP_UNLOCK( hdc );
     return handle;
 }
 
@@ -453,13 +461,22 @@
 /***********************************************************************
  *           SetDCState    (GDI.180)
  */
-void SetDCState( HDC16 hdc, HDC16 hdcs )
+void WINAPI SetDCState( HDC16 hdc, HDC16 hdcs )
 {
     DC *dc, *dcs;
     
     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return;
-    if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) return;
-    if (!dcs->w.flags & DC_SAVED) return;
+    if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
+    {
+      GDI_HEAP_UNLOCK( hdc );
+      return;
+    }
+    if (!dcs->w.flags & DC_SAVED)
+    {
+      GDI_HEAP_UNLOCK( hdc );
+      GDI_HEAP_UNLOCK( hdcs );
+      return;
+    }
     dprintf_dc(stddeb, "SetDCState: %04x %04x\n", hdc, hdcs );
 
     dc->w.flags           = dcs->w.flags & ~DC_SAVED;
@@ -506,13 +523,15 @@
     SelectObject32( hdc, dcs->w.hFont );
     SelectObject32( hdc, dcs->w.hPen );
     GDISelectPalette( hdc, dcs->w.hPalette, FALSE );
+    GDI_HEAP_UNLOCK( hdc );
+    GDI_HEAP_UNLOCK( hdcs );
 }
 
 
 /***********************************************************************
  *           SaveDC16    (GDI.30)
  */
-INT16 SaveDC16( HDC16 hdc )
+INT16 WINAPI SaveDC16( HDC16 hdc )
 {
     return (INT16)SaveDC32( hdc );
 }
@@ -521,10 +540,11 @@
 /***********************************************************************
  *           SaveDC32    (GDI32.292)
  */
-INT32 SaveDC32( HDC32 hdc )
+INT32 WINAPI SaveDC32( HDC32 hdc )
 {
     HDC32 hdcs;
     DC * dc, * dcs;
+    INT32 ret;
 
     dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) 
@@ -532,21 +552,29 @@
 	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
 	if (!dc) return 0;
 	MF_MetaParam0(dc, META_SAVEDC);
+	GDI_HEAP_UNLOCK( hdc );
 	return 1;  /* ?? */
     }
-    if (!(hdcs = GetDCState( hdc ))) return 0;
-    dcs = (DC *) GDI_HEAP_LIN_ADDR( hdcs );
+    if (!(hdcs = GetDCState( hdc )))
+    {
+      GDI_HEAP_UNLOCK( hdc );
+      return 0;
+    }
+    dcs = (DC *) GDI_HEAP_LOCK( hdcs );
     dcs->header.hNext = dc->header.hNext;
     dc->header.hNext = hdcs;
     dprintf_dc(stddeb, "SaveDC(%04x): returning %d\n", hdc, dc->saveLevel+1 );
-    return ++dc->saveLevel;
+    ret = ++dc->saveLevel;
+    GDI_HEAP_UNLOCK( hdcs );
+    GDI_HEAP_UNLOCK( hdc );
+    return ret;
 }
 
 
 /***********************************************************************
  *           RestoreDC16    (GDI.39)
  */
-BOOL16 RestoreDC16( HDC16 hdc, INT16 level )
+BOOL16 WINAPI RestoreDC16( HDC16 hdc, INT16 level )
 {
     return RestoreDC32( hdc, level );
 }
@@ -555,7 +583,7 @@
 /***********************************************************************
  *           RestoreDC32    (GDI32.290)
  */
-BOOL32 RestoreDC32( HDC32 hdc, INT32 level )
+BOOL32 WINAPI RestoreDC32( HDC32 hdc, INT32 level )
 {
     DC * dc, * dcs;
 
@@ -565,21 +593,35 @@
     {
 	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
 	if (!dc) return FALSE;
-	if (level != -1) return FALSE;
+	if (level != -1) 
+	{
+	  GDI_HEAP_UNLOCK( hdc );
+	  return FALSE;
+	}
 	MF_MetaParam1(dc, META_RESTOREDC, level);
+	GDI_HEAP_UNLOCK( hdc );
 	return TRUE;
     }
     if (level == -1) level = dc->saveLevel;
-    if ((level < 1) || (level > dc->saveLevel)) return FALSE;
+    if ((level < 1) || (level > dc->saveLevel))
+    {
+      GDI_HEAP_UNLOCK( hdc );
+      return FALSE;
+    }
     
     while (dc->saveLevel >= level)
     {
 	HDC16 hdcs = dc->header.hNext;
-	if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC ))) return FALSE;
+	if (!(dcs = (DC *) GDI_GetObjPtr( hdcs, DC_MAGIC )))
+	{
+	  GDI_HEAP_UNLOCK( hdc );
+	  return FALSE;
+	}	
 	dc->header.hNext = dcs->header.hNext;
 	if (--dc->saveLevel < level) SetDCState( hdc, hdcs );
 	DeleteDC32( hdcs );
     }
+    GDI_HEAP_UNLOCK( hdc );
     return TRUE;
 }
 
@@ -587,8 +629,8 @@
 /***********************************************************************
  *           CreateDC16    (GDI.53)
  */
-HDC16 CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
-                  const DEVMODE16 *initData )
+HDC16 WINAPI CreateDC16( LPCSTR driver, LPCSTR device, LPCSTR output,
+                         const DEVMODE16 *initData )
 {
     DC * dc;
     const DC_FUNCTIONS *funcs;
@@ -609,6 +651,7 @@
     }
 
     DC_InitDC( dc );
+    GDI_HEAP_UNLOCK( dc->hSelf );
     return dc->hSelf;
 }
 
@@ -616,8 +659,8 @@
 /***********************************************************************
  *           CreateDC32A    (GDI32.)
  */
-HDC32 CreateDC32A( LPCSTR driver, LPCSTR device, LPCSTR output,
-                   const DEVMODE32A *initData )
+HDC32 WINAPI CreateDC32A( LPCSTR driver, LPCSTR device, LPCSTR output,
+                          const DEVMODE32A *initData )
 {
     return CreateDC16( driver, device, output, (const DEVMODE16 *)initData );
 }
@@ -626,8 +669,8 @@
 /***********************************************************************
  *           CreateDC32W    (GDI32.)
  */
-HDC32 CreateDC32W( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
-                   const DEVMODE32W *initData )
+HDC32 WINAPI CreateDC32W( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
+                          const DEVMODE32W *initData )
 { 
     LPSTR driverA = HEAP_strdupWtoA( GetProcessHeap(), 0, driver );
     LPSTR deviceA = HEAP_strdupWtoA( GetProcessHeap(), 0, device );
@@ -644,8 +687,8 @@
 /***********************************************************************
  *           CreateIC16    (GDI.153)
  */
-HDC16 CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
-                  const DEVMODE16* initData )
+HDC16 WINAPI CreateIC16( LPCSTR driver, LPCSTR device, LPCSTR output,
+                         const DEVMODE16* initData )
 {
       /* Nothing special yet for ICs */
     return CreateDC16( driver, device, output, initData );
@@ -655,8 +698,8 @@
 /***********************************************************************
  *           CreateIC32A    (GDI32.49)
  */
-HDC32 CreateIC32A( LPCSTR driver, LPCSTR device, LPCSTR output,
-                   const DEVMODE32A* initData )
+HDC32 WINAPI CreateIC32A( LPCSTR driver, LPCSTR device, LPCSTR output,
+                          const DEVMODE32A* initData )
 {
       /* Nothing special yet for ICs */
     return CreateDC32A( driver, device, output, initData );
@@ -666,8 +709,8 @@
 /***********************************************************************
  *           CreateIC32W    (GDI32.50)
  */
-HDC32 CreateIC32W( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
-                   const DEVMODE32W* initData )
+HDC32 WINAPI CreateIC32W( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
+                          const DEVMODE32W* initData )
 {
       /* Nothing special yet for ICs */
     return CreateDC32W( driver, device, output, initData );
@@ -677,7 +720,7 @@
 /***********************************************************************
  *           CreateCompatibleDC16    (GDI.52)
  */
-HDC16 CreateCompatibleDC16( HDC16 hdc )
+HDC16 WINAPI CreateCompatibleDC16( HDC16 hdc )
 {
     return (HDC16)CreateCompatibleDC32( hdc );
 }
@@ -686,7 +729,7 @@
 /***********************************************************************
  *           CreateCompatibleDC32   (GDI32.31)
  */
-HDC32 CreateCompatibleDC32( HDC32 hdc )
+HDC32 WINAPI CreateCompatibleDC32( HDC32 hdc )
 {
     DC *dc, *origDC;
     HBITMAP32 hbitmap;
@@ -722,6 +765,7 @@
     }
 
     DC_InitDC( dc );
+    GDI_HEAP_UNLOCK( dc->hSelf );
     return dc->hSelf;
 }
 
@@ -729,7 +773,7 @@
 /***********************************************************************
  *           DeleteDC16    (GDI.68)
  */
-BOOL16 DeleteDC16( HDC16 hdc )
+BOOL16 WINAPI DeleteDC16( HDC16 hdc )
 {
     return DeleteDC32( hdc );
 }
@@ -738,7 +782,7 @@
 /***********************************************************************
  *           DeleteDC32    (GDI32.67)
  */
-BOOL32 DeleteDC32( HDC32 hdc )
+BOOL32 WINAPI DeleteDC32( HDC32 hdc )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) return FALSE;
@@ -775,7 +819,7 @@
 /***********************************************************************
  *           ResetDC16    (GDI.376)
  */
-HDC16 ResetDC16( HDC16 hdc, const DEVMODE16 *devmode )
+HDC16 WINAPI ResetDC16( HDC16 hdc, const DEVMODE16 *devmode )
 {
     fprintf( stderr, "ResetDC16: empty stub!\n" );
     return hdc;
@@ -785,7 +829,7 @@
 /***********************************************************************
  *           ResetDC32A    (GDI32.287)
  */
-HDC32 ResetDC32A( HDC32 hdc, const DEVMODE32A *devmode )
+HDC32 WINAPI ResetDC32A( HDC32 hdc, const DEVMODE32A *devmode )
 {
     fprintf( stderr, "ResetDC32A: empty stub!\n" );
     return hdc;
@@ -795,7 +839,7 @@
 /***********************************************************************
  *           ResetDC32W    (GDI32.288)
  */
-HDC32 ResetDC32W( HDC32 hdc, const DEVMODE32W *devmode )
+HDC32 WINAPI ResetDC32W( HDC32 hdc, const DEVMODE32W *devmode )
 {
     fprintf( stderr, "ResetDC32A: empty stub!\n" );
     return hdc;
@@ -805,7 +849,7 @@
 /***********************************************************************
  *           GetDeviceCaps16    (GDI.80)
  */
-INT16 GetDeviceCaps16( HDC16 hdc, INT16 cap )
+INT16 WINAPI GetDeviceCaps16( HDC16 hdc, INT16 cap )
 {
     return GetDeviceCaps32( hdc, cap );
 }
@@ -814,23 +858,31 @@
 /***********************************************************************
  *           GetDeviceCaps32    (GDI32.171)
  */
-INT32 GetDeviceCaps32( HDC32 hdc, INT32 cap )
+INT32 WINAPI GetDeviceCaps32( HDC32 hdc, INT32 cap )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
+    INT32 ret;
+
     if (!dc) return 0;
 
-    if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD))) return 0;
+    if ((cap < 0) || (cap > sizeof(DeviceCaps)-sizeof(WORD)))
+    {
+      GDI_HEAP_UNLOCK( hdc );
+      return 0;
+    }
     
     dprintf_dc(stddeb, "GetDeviceCaps(%04x,%d): returning %d\n",
 	    hdc, cap, *(WORD *)(((char *)dc->w.devCaps) + cap) );
-    return *(WORD *)(((char *)dc->w.devCaps) + cap);
+    ret = *(WORD *)(((char *)dc->w.devCaps) + cap);
+    GDI_HEAP_UNLOCK( hdc );
+    return ret;
 }
 
 
 /***********************************************************************
  *           SetBkColor16    (GDI.1)
  */
-COLORREF SetBkColor16( HDC16 hdc, COLORREF color )
+COLORREF WINAPI SetBkColor16( HDC16 hdc, COLORREF color )
 {
     return SetBkColor32( hdc, color );
 }
@@ -839,7 +891,7 @@
 /***********************************************************************
  *           SetBkColor32    (GDI32.305)
  */
-COLORREF SetBkColor32( HDC32 hdc, COLORREF color )
+COLORREF WINAPI SetBkColor32( HDC32 hdc, COLORREF color )
 {
     COLORREF oldColor;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -848,12 +900,14 @@
 	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
 	if (!dc) return 0x80000000;
 	MF_MetaParam2(dc, META_SETBKCOLOR, HIWORD(color), LOWORD(color));
+	GDI_HEAP_UNLOCK( hdc );
 	return 0;  /* ?? */
     }
 
     oldColor = dc->w.backgroundColor;
     dc->w.backgroundColor = color;
     dc->w.backgroundPixel = COLOR_ToPhysical( dc, color );
+    GDI_HEAP_UNLOCK( hdc );
     return oldColor;
 }
 
@@ -861,7 +915,7 @@
 /***********************************************************************
  *           SetTextColor16    (GDI.9)
  */
-COLORREF SetTextColor16( HDC16 hdc, COLORREF color )
+COLORREF WINAPI SetTextColor16( HDC16 hdc, COLORREF color )
 {
     return SetTextColor32( hdc, color );
 }
@@ -870,7 +924,7 @@
 /***********************************************************************
  *           SetTextColor32    (GDI32.338)
  */
-COLORREF SetTextColor32( HDC32 hdc, COLORREF color )
+COLORREF WINAPI SetTextColor32( HDC32 hdc, COLORREF color )
 {
     COLORREF oldColor;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -879,12 +933,14 @@
 	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
 	if (!dc) return 0x80000000;
 	MF_MetaParam2(dc, META_SETTEXTCOLOR, HIWORD(color), LOWORD(color));
+	GDI_HEAP_UNLOCK( hdc );
 	return 0;  /* ?? */
     }
 
     oldColor = dc->w.textColor;
     dc->w.textColor = color;
     dc->w.textPixel = COLOR_ToPhysical( dc, color );
+    GDI_HEAP_UNLOCK( hdc );
     return oldColor;
 }
 
@@ -892,7 +948,7 @@
 /***********************************************************************
  *           SetTextAlign16    (GDI.346)
  */
-UINT16 SetTextAlign16( HDC16 hdc, UINT16 textAlign )
+UINT16 WINAPI SetTextAlign16( HDC16 hdc, UINT16 textAlign )
 {
     return SetTextAlign32( hdc, textAlign );
 }
@@ -901,7 +957,7 @@
 /***********************************************************************
  *           SetTextAlign32    (GDI32.336)
  */
-UINT32 SetTextAlign32( HDC32 hdc, UINT32 textAlign )
+UINT32 WINAPI SetTextAlign32( HDC32 hdc, UINT32 textAlign )
 {
     UINT32 prevAlign;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -909,10 +965,12 @@
     {
 	if (!(dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC ))) return 0;
 	MF_MetaParam1( dc, META_SETTEXTALIGN, textAlign );
+	GDI_HEAP_UNLOCK( hdc );
 	return 1;
     }
     prevAlign = dc->w.textAlign;
     dc->w.textAlign = textAlign;
+    GDI_HEAP_UNLOCK( hdc );
     return prevAlign;
 }
 
@@ -920,10 +978,11 @@
 /***********************************************************************
  *           GetDCOrgEx  (GDI32.168)
  */
-BOOL32 GetDCOrgEx( HDC32 hDC, LPPOINT32 lpp )
+BOOL32 WINAPI GetDCOrgEx( HDC32 hDC, LPPOINT32 lpp )
 {
-    DC * dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC );
-    if (!dc || !lpp) return FALSE;
+    DC * dc;
+    if (!lpp) return FALSE;
+    if (!(dc = (DC *) GDI_GetObjPtr( hDC, DC_MAGIC ))) return FALSE;
 
     if (!(dc->w.flags & DC_MEMORY))
     {
@@ -935,6 +994,7 @@
     }
     else lpp->x = lpp->y = 0;
     lpp->x += dc->w.DCOrgX; lpp->y += dc->w.DCOrgY;
+    GDI_HEAP_UNLOCK( hDC );
     return TRUE;
 }
 
@@ -942,7 +1002,7 @@
 /***********************************************************************
  *           GetDCOrg    (GDI.79)
  */
-DWORD GetDCOrg( HDC16 hdc )
+DWORD WINAPI GetDCOrg( HDC16 hdc )
 {
     POINT32	pt;
     if( GetDCOrgEx( hdc, &pt) )
@@ -954,7 +1014,7 @@
 /***********************************************************************
  *           SetDCOrg    (GDI.117)
  */
-DWORD SetDCOrg( HDC16 hdc, INT16 x, INT16 y )
+DWORD WINAPI SetDCOrg( HDC16 hdc, INT16 x, INT16 y )
 {
     DWORD prevOrg;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -962,6 +1022,7 @@
     prevOrg = dc->w.DCOrgX | (dc->w.DCOrgY << 16);
     dc->w.DCOrgX = x;
     dc->w.DCOrgY = y;
+    GDI_HEAP_UNLOCK( hdc );
     return prevOrg;
 }
 
@@ -969,7 +1030,7 @@
 /***********************************************************************
  *           SetDCHook   (GDI.190)
  */
-BOOL16 SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
+BOOL16 WINAPI SetDCHook( HDC16 hdc, FARPROC16 hookProc, DWORD dwHookData )
 {
     DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
 
@@ -979,6 +1040,7 @@
     if (!dc) return FALSE;
     dc->hookProc = hookProc;
     dc->dwHookData = dwHookData;
+    GDI_HEAP_UNLOCK( hdc );
     return TRUE;
 }
 
@@ -986,11 +1048,12 @@
 /***********************************************************************
  *           GetDCHook   (GDI.191)
  */
-DWORD GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
+DWORD WINAPI GetDCHook( HDC16 hdc, FARPROC16 *phookProc )
 {
     DC *dc = (DC *)GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) return 0;
     *phookProc = dc->hookProc;
+    GDI_HEAP_UNLOCK( hdc );
     return dc->dwHookData;
 }
 
@@ -998,7 +1061,7 @@
 /***********************************************************************
  *           SetHookFlags       (GDI.192)
  */
-WORD SetHookFlags(HDC16 hDC, WORD flags)
+WORD WINAPI SetHookFlags(HDC16 hDC, WORD flags)
 {
   DC* dc = (DC*)GDI_GetObjPtr( hDC, DC_MAGIC );
 
@@ -1016,6 +1079,7 @@
             dc->w.flags |= DC_DIRTY;
         else if( flags & DCHF_VALIDATEVISRGN || !flags )
             dc->w.flags &= ~DC_DIRTY;
+	GDI_HEAP_UNLOCK( hDC );
         return wRet;
     }
   return 0;
diff --git a/objects/dcvalues.c b/objects/dcvalues.c
index 9ea8221..df7de09 100644
--- a/objects/dcvalues.c
+++ b/objects/dcvalues.c
@@ -10,7 +10,7 @@
 
 
 #define DC_GET_VAL_16( func_type, func_name, dc_field ) \
-func_type func_name( HDC16 hdc ) \
+func_type WINAPI func_name( HDC16 hdc ) \
 { \
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ); \
     if (!dc) return 0; \
@@ -18,7 +18,7 @@
 }
 
 #define DC_GET_VAL_32( func_type, func_name, dc_field ) \
-func_type func_name( HDC32 hdc ) \
+func_type WINAPI func_name( HDC32 hdc ) \
 { \
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ); \
     if (!dc) return 0; \
@@ -26,7 +26,7 @@
 }
 
 #define DC_GET_X_Y( func_type, func_name, ret_x, ret_y ) \
-func_type func_name( HDC16 hdc ) \
+func_type WINAPI func_name( HDC16 hdc ) \
 { \
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ); \
     if (!dc) return 0; \
@@ -34,7 +34,7 @@
 }
 
 #define DC_GET_VAL_EX( func_name, ret_x, ret_y ) \
-BOOL16 func_name##16( HDC16 hdc, LPPOINT16 pt ) \
+BOOL16 WINAPI func_name##16( HDC16 hdc, LPPOINT16 pt ) \
 { \
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ); \
     if (!dc) return FALSE; \
@@ -43,7 +43,7 @@
     return TRUE; \
 } \
  \
-BOOL32 func_name##32( HDC32 hdc, LPPOINT32 pt ) \
+BOOL32 WINAPI func_name##32( HDC32 hdc, LPPOINT32 pt ) \
 { \
     DC * dc = (DC *) GDI_GetObjPtr( (HDC16)hdc, DC_MAGIC ); \
     if (!dc) return FALSE; \
@@ -53,7 +53,7 @@
 }
 
 #define DC_SET_MODE_16( func_name, dc_field, min_val, max_val, meta_func ) \
-INT16 func_name( HDC16 hdc, INT16 mode ) \
+INT16 WINAPI func_name( HDC16 hdc, INT16 mode ) \
 { \
     INT16 prevMode; \
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ); \
@@ -70,7 +70,7 @@
 }
 
 #define DC_SET_MODE_32( func_name, dc_field, min_val, max_val, meta_func ) \
-INT32 func_name( HDC32 hdc, INT32 mode ) \
+INT32 WINAPI func_name( HDC32 hdc, INT32 mode ) \
 { \
     INT32 prevMode; \
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ); \
diff --git a/objects/dib.c b/objects/dib.c
index 433cf71..9746225 100644
--- a/objects/dib.c
+++ b/objects/dib.c
@@ -18,8 +18,31 @@
 
 extern void CLIPPING_UpdateGCRegion(DC* );
 
-static int	bitmapDepthTable[] = { 8, 1, 32, 16, 24, 15, 4, 0 };
-static int	ximageDepthTable[] = { 0, 0, 0,  0,  0,  0,  0 };
+static int bitmapDepthTable[] = { 8, 1, 32, 16, 24, 15, 4, 0 };
+static int ximageDepthTable[] = { 0, 0, 0,  0,  0,  0,  0 };
+
+
+/* This structure holds the arguments for DIB_SetImageBits() */
+typedef struct
+{
+    DC               *dc;
+    LPCVOID           bits;
+    DWORD             lines;
+    DWORD             infoWidth;
+    WORD              depth;
+    WORD              infoBpp;
+    const BITMAPINFO *info;
+    WORD              coloruse;
+    Drawable          drawable;
+    GC                gc;
+    int               xSrc;
+    int               ySrc;
+    int               xDest;
+    int               yDest;
+    int               width;
+    int               height;
+} DIB_SETIMAGEBITS_DESCR;
+
 
 /***********************************************************************
  *           DIB_Init
@@ -152,7 +175,7 @@
  * for a 24-bit deep bitmap.
  */
 static int *DIB_BuildColorMap( DC *dc, WORD coloruse, WORD depth,
-                               BITMAPINFO *info )
+                               const BITMAPINFO *info )
 {
     int i, colors;
     BOOL32 isInfo;
@@ -217,18 +240,20 @@
  *
  * SetDIBits for a 1-bit deep DIB.
  */
-static void DIB_SetImageBits_1( DWORD lines, BYTE *bits, DWORD width,
+static void DIB_SetImageBits_1( DWORD lines, const BYTE *srcbits,
+                                DWORD srcwidth, DWORD dstwidth,
                                 int *colors, XImage *bmpImage )
 {
     DWORD i, x;
-    BYTE pad, pix;
+    BYTE pix;
+    const BYTE *bits = srcbits;
 
-    if (!(width & 31)) pad = 0;
-    else pad = ((32 - (width & 31)) + 7) / 8;
+    /* 32 bit aligned */
+    DWORD linebytes = ((srcwidth + 31) & ~31) / 8;
 
     while (lines--)
     {
-	for (i = width/8, x = 0; (i > 0); i--)
+	for (i = dstwidth/8, x = 0; (i > 0); i--)
 	{
 	    pix = *bits++;
 	    XPutPixel( bmpImage, x++, lines, colors[pix >> 7] );
@@ -241,7 +266,7 @@
 	    XPutPixel( bmpImage, x++, lines, colors[pix & 1] );
 	}
 	pix = *bits;
-	switch(width & 7)
+	switch(dstwidth & 7)
 	{
 	case 7: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
 	case 6: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
@@ -251,7 +276,8 @@
 	case 2: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] ); pix <<= 1;
 	case 1: XPutPixel( bmpImage, x++, lines, colors[pix >> 7] );
 	}
-	bits += pad;
+	srcbits += linebytes;
+	bits	 = srcbits;
     }
 }
 
@@ -261,25 +287,27 @@
  *
  * SetDIBits for a 4-bit deep DIB.
  */
-static void DIB_SetImageBits_4( DWORD lines, BYTE *bits, DWORD width,
+static void DIB_SetImageBits_4( DWORD lines, const BYTE *srcbits,
+                                DWORD srcwidth, DWORD dstwidth,
                                 int *colors, XImage *bmpImage )
 {
     DWORD i, x;
-    BYTE pad;
-
-    if (!(width & 7)) pad = 0;
-    else pad = ((8 - (width & 7)) + 1) / 2;
+    const BYTE *bits = srcbits;
+  
+    /* 32 bit aligned */
+    DWORD linebytes = ((srcwidth+7)&~7)/2;
 
     while (lines--)
     {
-	for (i = width/2, x = 0; i > 0; i--)
+	for (i = srcwidth/2, x = 0; i > 0; i--)
 	{
 	    BYTE pix = *bits++;
 	    XPutPixel( bmpImage, x++, lines, colors[pix >> 4] );
 	    XPutPixel( bmpImage, x++, lines, colors[pix & 0x0f] );
 	}
-	if (width & 1) XPutPixel( bmpImage, x, lines, colors[*bits >> 4] );
-	bits += pad;
+        if (srcwidth & 1) XPutPixel( bmpImage, x, lines, colors[*bits >> 4] );
+        srcbits += linebytes;
+        bits	 = srcbits;
     }
 }
 
@@ -295,11 +323,11 @@
  *
  * SetDIBits for a 4-bit deep compressed DIB.
  */
-static void DIB_SetImageBits_RLE4( DWORD lines, BYTE *bits, DWORD width,
-                                   int *colors, XImage *bmpImage )
+static void DIB_SetImageBits_RLE4( DWORD lines, const BYTE *bits, DWORD width,
+                                DWORD dstwidth, int *colors, XImage *bmpImage )
 {
 	int x = 0, c, length;
-	BYTE *begin = bits;
+	const BYTE *begin = bits;
 
         lines--;
 	while ((int)lines >= 0)
@@ -355,17 +383,22 @@
  *
  * SetDIBits for an 8-bit deep DIB.
  */
-static void DIB_SetImageBits_8( DWORD lines, BYTE *bits, DWORD width,
+static void DIB_SetImageBits_8( DWORD lines, const BYTE *srcbits,
+                                DWORD srcwidth, DWORD dstwidth,
                                 int *colors, XImage *bmpImage )
 {
     DWORD x;
-    BYTE pad = (4 - (width & 3)) & 3;
+    const BYTE *bits = srcbits;
+
+    /* align to 32 bit */
+    DWORD linebytes = (srcwidth + 3) & ~3;
 
     while (lines--)
     {
-	for (x = 0; x < width; x++)
+	for (x = 0; x < dstwidth; x++)
 	    XPutPixel( bmpImage, x, lines, colors[*bits++] );
-	bits += pad;
+        srcbits += linebytes;
+        bits     = srcbits;
     }
 }
 
@@ -400,12 +433,12 @@
   RleDelta	= 2		/* Delta */
 };
   
-static void DIB_SetImageBits_RLE8( DWORD lines, BYTE *bits, DWORD width,
-                                   int *colors, XImage *bmpImage )
+static void DIB_SetImageBits_RLE8( DWORD lines, const BYTE *bits, DWORD width,
+                                DWORD dstwidth, int *colors, XImage *bmpImage )
 {
     int x;			/* X-positon on each line.  Increases. */
     int line;			/* Line #.  Starts at lines-1, decreases */
-    BYTE *pIn = bits;		/* Pointer to current position in bits */
+    const BYTE *pIn = bits;     /* Pointer to current position in bits */
     BYTE length;		/* The length pf a run */
     BYTE color_index;		/* index into colors[] as read from bits */
     BYTE escape_code;		/* See enum Rle8_EscapeCodes.*/
@@ -553,21 +586,25 @@
  *
  * SetDIBits for a 24-bit deep DIB.
  */
-static void DIB_SetImageBits_24( DWORD lines, BYTE *bits, DWORD width,
+static void DIB_SetImageBits_24( DWORD lines, const BYTE *srcbits,
+                                 DWORD srcwidth, DWORD dstwidth,
 				 DC *dc, XImage *bmpImage )
 {
     DWORD x;
-    BYTE pad = (4 - ((width*3) & 3)) & 3;
+    const BYTE *bits = srcbits;
+  
+    /* align to 32 bit */
+    DWORD linebytes = (srcwidth * 3 + 3) & ~3;
 
     /* "bits" order is reversed for some reason */
 
     while (lines--)
     {
-	for (x = 0; x < width; x++, bits += 3)
+	for (x = 0; x < dstwidth; x++, bits += 3)
 	    XPutPixel( bmpImage, x, lines, 
 		       COLOR_ToPhysical(dc, RGB(bits[2],bits[1],bits[0])) );
 
-	bits += pad;
+        bits = (srcbits += linebytes);
     }
 }
 
@@ -578,70 +615,68 @@
  * Transfer the bits to an X image.
  * Helper function for SetDIBits() and SetDIBitsToDevice().
  */
-static int DIB_SetImageBits( DC *dc, DWORD lines, WORD depth, LPSTR bits,
-                             DWORD infoWidth, WORD infoBpp,
-                             BITMAPINFO *info, WORD coloruse,
-			     Drawable drawable, GC gc, int xSrc, int ySrc,
-			     int xDest, int yDest, int width, int height )
+static int DIB_SetImageBits( const DIB_SETIMAGEBITS_DESCR *descr )
 {
     int *colorMapping;
     XImage *bmpImage;
     DWORD compression = 0;
 
-    if (info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
-        compression = info->bmiHeader.biCompression;
+    if (descr->info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
+        compression = descr->info->bmiHeader.biCompression;
 
       /* Build the color mapping table */
 
-    if (infoBpp > 8) {
-	    	colorMapping = NULL;
-    } else {
-        if (!(colorMapping = DIB_BuildColorMap( dc, coloruse, depth, info )))
-            return 0;
-    }
+    if (descr->infoBpp > 8) colorMapping = NULL;
+    else if (!(colorMapping = DIB_BuildColorMap( descr->dc, descr->coloruse,
+                                                 descr->depth, descr->info )))
+        return 0;
 
-    if( dc->w.flags & DC_DIRTY ) CLIPPING_UpdateGCRegion(dc);
+    if( descr->dc->w.flags & DC_DIRTY ) CLIPPING_UpdateGCRegion(descr->dc);
 
       /* Transfer the pixels */
-    XCREATEIMAGE(bmpImage, infoWidth, lines, depth );
+    XCREATEIMAGE(bmpImage, descr->infoWidth, descr->lines, descr->depth );
 
-    switch(infoBpp)
+    switch(descr->infoBpp)
     {
     case 1:
-	DIB_SetImageBits_1( lines, bits, infoWidth,
-			    colorMapping, bmpImage );
+	DIB_SetImageBits_1( descr->lines, descr->bits, descr->infoWidth,
+			    descr->width, colorMapping, bmpImage );
 	break;
     case 4:
-	if (compression) DIB_SetImageBits_RLE4( lines, bits, infoWidth,
+	if (compression) DIB_SetImageBits_RLE4( descr->lines, descr->bits,
+                                                descr->infoWidth, descr->width,
                                                 colorMapping, bmpImage );
-	else DIB_SetImageBits_4( lines, bits, infoWidth,
-                                 colorMapping, bmpImage );
+	else DIB_SetImageBits_4( descr->lines, descr->bits, descr->infoWidth,
+                                 descr->width, colorMapping, bmpImage );
 	break;
     case 8:
-	if (compression) DIB_SetImageBits_RLE8( lines, bits, infoWidth,
+	if (compression) DIB_SetImageBits_RLE8( descr->lines, descr->bits,
+                                                descr->infoWidth, descr->width,
                                                 colorMapping, bmpImage );
-	else DIB_SetImageBits_8( lines, bits, infoWidth,
-                                 colorMapping, bmpImage );
+	else DIB_SetImageBits_8( descr->lines, descr->bits, descr->infoWidth,
+                                 descr->width, colorMapping, bmpImage );
 	break;
     case 24:
-	DIB_SetImageBits_24( lines, bits, infoWidth, dc, bmpImage );
+	DIB_SetImageBits_24( descr->lines, descr->bits, descr->infoWidth,
+                             descr->width, descr->dc, bmpImage );
 	break;
     default:
-        fprintf( stderr, "Invalid depth %d for SetDIBits!\n", infoBpp );
+        fprintf( stderr, "Invalid depth %d for SetDIBits!\n", descr->infoBpp );
         break;
     }
     if (colorMapping) free(colorMapping);
-    XPutImage( display, drawable, gc, bmpImage, xSrc, ySrc,
-	       xDest, yDest, width, height );
+    XPutImage( display, descr->drawable, descr->gc, bmpImage,
+               descr->xSrc, descr->ySrc, descr->xDest, descr->yDest,
+               descr->width, descr->height );
     XDestroyImage( bmpImage );
-    return lines;
+    return descr->lines;
 }
 
 
 /***********************************************************************
  *           StretchDIBits16   (GDI.439)
  */
-INT16 StretchDIBits16( HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
+INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
                        INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
                        INT16 heightSrc, const VOID *bits,
                        const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
@@ -655,7 +690,7 @@
 /***********************************************************************
  *           StretchDIBits32   (GDI32.351)
  */
-INT32 StretchDIBits32( HDC32 hdc, INT32 xDst, INT32 yDst, INT32 widthDst,
+INT32 WINAPI StretchDIBits32(HDC32 hdc, INT32 xDst, INT32 yDst, INT32 widthDst,
                        INT32 heightDst, INT32 xSrc, INT32 ySrc, INT32 widthSrc,
                        INT32 heightSrc, const void *bits,
                        const BITMAPINFO *info, UINT32 wUsage, DWORD dwRop )
@@ -679,9 +714,9 @@
 /***********************************************************************
  *           SetDIBits16    (GDI.440)
  */
-INT16 SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
-                   UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
-                   UINT16 coloruse )
+INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
+                          UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
+                          UINT16 coloruse )
 {
     return SetDIBits32( hdc, hbitmap, startscan, lines, bits, info, coloruse );
 }
@@ -690,43 +725,68 @@
 /***********************************************************************
  *           SetDIBits32   (GDI32.312)
  */
-INT32 SetDIBits32( HDC32 hdc, HBITMAP32 hbitmap, UINT32 startscan,
-                   UINT32 lines, LPCVOID bits, const BITMAPINFO *info,
-                   UINT32 coloruse )
+INT32 WINAPI SetDIBits32( HDC32 hdc, HBITMAP32 hbitmap, UINT32 startscan,
+                          UINT32 lines, LPCVOID bits, const BITMAPINFO *info,
+                          UINT32 coloruse )
 {
-    DC * dc;
+    DIB_SETIMAGEBITS_DESCR descr;
     BITMAPOBJ * bmp;
-    DWORD width, height;
-    WORD bpp;
+    DWORD height;
+    INT32 result;
 
       /* Check parameters */
 
-    dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
-    if (!dc) 
+    descr.dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
+    if (!descr.dc) 
     {
-	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
-	if (!dc) return 0;
+	descr.dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
+	if (!descr.dc) return 0;
     }
     if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
-	return 0;
-    if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, &bpp ) == -1)
+    {
+        GDI_HEAP_UNLOCK( hdc );
         return 0;
-    if (!lines || (startscan >= (WORD)height)) return 0;
+    }
+    if (DIB_GetBitmapInfo( &info->bmiHeader, &descr.infoWidth, &height,
+                           &descr.infoBpp ) == -1)
+    {
+        GDI_HEAP_UNLOCK( hbitmap );
+        GDI_HEAP_UNLOCK( hdc );
+        return 0;
+    }
+    if (!lines || (startscan >= height))
+    {
+        GDI_HEAP_UNLOCK( hbitmap );
+        GDI_HEAP_UNLOCK( hdc );
+        return 0;
+    }
     if (startscan + lines > height) lines = height - startscan;
 
-    return CallTo32_LargeStack( (int(*)())DIB_SetImageBits, 16,
-                                dc, lines, bmp->bitmap.bmBitsPixel,
-                                bits, width, bpp, info,
-                                coloruse, bmp->pixmap, BITMAP_GC(bmp), 0, 0, 0,
-                                height - startscan - lines,
-                                bmp->bitmap.bmWidth, lines );
+    descr.bits      = bits;
+    descr.lines     = lines;
+    descr.depth     = bmp->bitmap.bmBitsPixel;
+    descr.info      = info;
+    descr.coloruse  = coloruse;
+    descr.drawable  = bmp->pixmap;
+    descr.gc        = BITMAP_GC(bmp);
+    descr.xSrc      = 0;
+    descr.ySrc      = 0;
+    descr.xDest     = 0;
+    descr.yDest     = height - startscan - lines;
+    descr.width     = bmp->bitmap.bmWidth;
+    descr.height    = lines;
+
+    result = CALL_LARGE_STACK( DIB_SetImageBits, &descr );
+    GDI_HEAP_UNLOCK( hdc );
+    GDI_HEAP_UNLOCK( hbitmap );
+    return result;
 }
 
 
 /***********************************************************************
  *           SetDIBitsToDevice16    (GDI.443)
  */
-INT16 SetDIBitsToDevice16( HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
+INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
                            INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
                            UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
                            UINT16 coloruse )
@@ -739,14 +799,14 @@
 /***********************************************************************
  *           SetDIBitsToDevice32   (GDI32.313)
  */
-INT32 SetDIBitsToDevice32( HDC32 hdc, INT32 xDest, INT32 yDest, DWORD cx,
+INT32 WINAPI SetDIBitsToDevice32(HDC32 hdc, INT32 xDest, INT32 yDest, DWORD cx,
                            DWORD cy, INT32 xSrc, INT32 ySrc, UINT32 startscan,
                            UINT32 lines, LPCVOID bits, const BITMAPINFO *info,
                            UINT32 coloruse )
 {
+    DIB_SETIMAGEBITS_DESCR descr;
     DC * dc;
     DWORD width, height;
-    WORD bpp;
 
       /* Check parameters */
 
@@ -756,7 +816,8 @@
 	dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
 	if (!dc) return 0;
     }
-    if (DIB_GetBitmapInfo( &info->bmiHeader, &width, &height, &bpp ) == -1)
+    if (DIB_GetBitmapInfo( &info->bmiHeader, &width,
+                           &height, &descr.infoBpp ) == -1)
         return 0;
     if (!lines || (startscan >= height)) return 0;
     if (startscan + lines > height) lines = height - startscan;
@@ -769,14 +830,24 @@
 
     DC_SetupGCForText( dc );  /* To have the correct colors */
     XSetFunction( display, dc->u.x.gc, DC_XROPfunction[dc->w.ROPmode-1] );
-    return CallTo32_LargeStack( (int(*)())DIB_SetImageBits, 16,
-                                dc, lines, dc->w.bitsPerPixel, bits, width,
-                                bpp, info, coloruse,
-                                dc->u.x.drawable, dc->u.x.gc,
-                                xSrc, ySrc - startscan,
-                                dc->w.DCOrgX + XLPTODP( dc, xDest ),
-                                dc->w.DCOrgY + YLPTODP( dc, yDest ),
-                                cx, cy );
+
+    descr.dc        = dc;
+    descr.bits      = bits;
+    descr.lines     = lines;
+    descr.infoWidth = width;
+    descr.depth     = dc->w.bitsPerPixel;
+    descr.info      = info;
+    descr.coloruse  = coloruse;
+    descr.drawable  = dc->u.x.drawable;
+    descr.gc        = dc->u.x.gc;
+    descr.xSrc      = xSrc;
+    descr.ySrc      = ySrc - startscan;
+    descr.xDest     = dc->w.DCOrgX + XLPTODP( dc, xDest );
+    descr.yDest     = dc->w.DCOrgY + YLPTODP( dc, yDest );
+    descr.width     = cx;
+    descr.height    = cy;
+
+    return CALL_LARGE_STACK( DIB_SetImageBits, &descr );
 }
 
 
@@ -784,9 +855,9 @@
 /***********************************************************************
  *           GetDIBits16    (GDI.441)
  */
-INT16 GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
-                   UINT16 lines, LPSTR bits, BITMAPINFO * info,
-                   UINT16 coloruse )
+INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
+                          UINT16 lines, LPSTR bits, BITMAPINFO * info,
+                          UINT16 coloruse )
 {
     return GetDIBits32( hdc, hbitmap, startscan, lines, bits, info, coloruse );
 }
@@ -797,9 +868,9 @@
  *
  * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
  */
-INT32 GetDIBits32( HDC32 hdc, HBITMAP32 hbitmap, UINT32 startscan,
-                   UINT32 lines, LPSTR bits, BITMAPINFO * info,
-                   UINT32 coloruse )
+INT32 WINAPI GetDIBits32( HDC32 hdc, HBITMAP32 hbitmap, UINT32 startscan,
+                          UINT32 lines, LPSTR bits, BITMAPINFO * info,
+                          UINT32 coloruse )
 {
     DC * dc;
     BITMAPOBJ * bmp;
@@ -818,7 +889,10 @@
     if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
 	return 0;
     if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC )))
-	return 0;
+    {
+        GDI_HEAP_UNLOCK( hbitmap );
+        return 0;
+    }
 
       /* Transfer color info */
     
@@ -851,7 +925,11 @@
 	if( lines > info->bmiHeader.biHeight ) lines = info->bmiHeader.biHeight;
 	yend = startscan + lines;
 	if( startscan >= bmp->bitmap.bmHeight ) 
-	    return FALSE;
+        {
+            GDI_HEAP_UNLOCK( hbitmap );
+            GDI_HEAP_UNLOCK( dc->w.hPalette );
+            return FALSE;
+        }
 	if( yend > bmp->bitmap.bmHeight ) yend = bmp->bitmap.bmHeight;
 
 	/* adjust scanline width */
@@ -861,12 +939,11 @@
 	{
 	    /* bitmap is wider than DIB, copy only a part */
 
-	    pad = 0; xend = info->bmiHeader.biWidth;
+	    pad = 0;
+            xend = info->bmiHeader.biWidth;
 	}
 
-	bmpImage = (XImage *)CallTo32_LargeStack( (int (*)())XGetImage, 8, 
-		               display, bmp->pixmap, 0, 0, bmp->bitmap.bmWidth,
-		               bmp->bitmap.bmHeight, AllPlanes, ZPixmap );
+	bmpImage = (XImage *)CALL_LARGE_STACK( BITMAP_GetXImage, bmp );
 
 	switch( info->bmiHeader.biBitCount )
 	{
@@ -965,6 +1042,8 @@
 	info->bmiHeader.biCompression = 0;
     }
 
+    GDI_HEAP_UNLOCK( hbitmap );
+    GDI_HEAP_UNLOCK( dc->w.hPalette );
     return lines;
 }
 
@@ -972,7 +1051,7 @@
 /***********************************************************************
  *           CreateDIBitmap16    (GDI.442)
  */
-HBITMAP16 CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
+HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
                             DWORD init, LPCVOID bits, const BITMAPINFO * data,
                             UINT16 coloruse )
 {
@@ -983,7 +1062,7 @@
 /***********************************************************************
  *           CreateDIBitmap32    (GDI32.37)
  */
-HBITMAP32 CreateDIBitmap32( HDC32 hdc, const BITMAPINFOHEADER *header,
+HBITMAP32 WINAPI CreateDIBitmap32( HDC32 hdc, const BITMAPINFOHEADER *header,
                             DWORD init, LPCVOID bits, const BITMAPINFO *data,
                             UINT32 coloruse )
 {
diff --git a/objects/font.c b/objects/font.c
index 75dcf63..216962f 100644
--- a/objects/font.c
+++ b/objects/font.c
@@ -177,7 +177,7 @@
 /***********************************************************************
  *           CreateFontIndirect16   (GDI.57)
  */
-HFONT16 CreateFontIndirect16( const LOGFONT16 *font )
+HFONT16 WINAPI CreateFontIndirect16( const LOGFONT16 *font )
 {
     HFONT16 hFont = 0;
 
@@ -187,7 +187,7 @@
 	if( hFont )
 	{
 	    FONTOBJ* fontPtr;
-	    fontPtr = (FONTOBJ *) GDI_HEAP_LIN_ADDR( hFont );
+	    fontPtr = (FONTOBJ *) GDI_HEAP_LOCK( hFont );
 	    memcpy( &fontPtr->logfont, font, sizeof(LOGFONT16) );
 
 	    dprintf_font(stddeb,"CreateFontIndirect(%i %i) '%s' %s %s => %04x\n",
@@ -196,6 +196,7 @@
 				 font->lfWeight > 400 ? "Bold" : "",
 				 font->lfItalic ? "Italic" : "",
 				 hFont);
+	    GDI_HEAP_UNLOCK( hFont );
 	}
     }
     else fprintf(stderr,"CreateFontIndirect(NULL) => NULL\n");
@@ -206,7 +207,7 @@
 /***********************************************************************
  *           CreateFontIndirect32A   (GDI32.44)
  */
-HFONT32 CreateFontIndirect32A( const LOGFONT32A *font )
+HFONT32 WINAPI CreateFontIndirect32A( const LOGFONT32A *font )
 {
     LOGFONT16 font16;
 
@@ -217,7 +218,7 @@
 /***********************************************************************
  *           CreateFontIndirect32W   (GDI32.45)
  */
-HFONT32 CreateFontIndirect32W( const LOGFONT32W *font )
+HFONT32 WINAPI CreateFontIndirect32W( const LOGFONT32W *font )
 {
     LOGFONT16 font16;
 
@@ -228,10 +229,11 @@
 /***********************************************************************
  *           CreateFont16    (GDI.56)
  */
-HFONT16 CreateFont16( INT16 height, INT16 width, INT16 esc, INT16 orient,
-                      INT16 weight, BYTE italic, BYTE underline,
-                      BYTE strikeout, BYTE charset, BYTE outpres,
-                      BYTE clippres, BYTE quality, BYTE pitch, LPCSTR name )
+HFONT16 WINAPI CreateFont16(INT16 height, INT16 width, INT16 esc, INT16 orient,
+                            INT16 weight, BYTE italic, BYTE underline,
+                            BYTE strikeout, BYTE charset, BYTE outpres,
+                            BYTE clippres, BYTE quality, BYTE pitch,
+                            LPCSTR name )
 {
     LOGFONT16 logfont = { height, width, esc, orient, weight, italic, underline,
                           strikeout, charset, outpres, clippres, quality, pitch, };
@@ -247,10 +249,11 @@
 /*************************************************************************
  *           CreateFont32A    (GDI32.43)
  */
-HFONT32 CreateFont32A( INT32 height, INT32 width, INT32 esc, INT32 orient,
-                       INT32 weight, DWORD italic, DWORD underline,
-                       DWORD strikeout, DWORD charset, DWORD outpres,
-                       DWORD clippres, DWORD quality, DWORD pitch, LPCSTR name)
+HFONT32 WINAPI CreateFont32A( INT32 height, INT32 width, INT32 esc,
+                              INT32 orient, INT32 weight, DWORD italic,
+                              DWORD underline, DWORD strikeout, DWORD charset,
+                              DWORD outpres, DWORD clippres, DWORD quality,
+                              DWORD pitch, LPCSTR name )
 {
     return (HFONT32)CreateFont16( height, width, esc, orient, weight, italic,
                                   underline, strikeout, charset, outpres,
@@ -260,11 +263,11 @@
 /*************************************************************************
  *           CreateFont32W    (GDI32.46)
  */
-HFONT32 CreateFont32W( INT32 height, INT32 width, INT32 esc, INT32 orient,
-                       INT32 weight, DWORD italic, DWORD underline,
-                       DWORD strikeout, DWORD charset, DWORD outpres,
-                       DWORD clippres, DWORD quality, DWORD pitch,
-                       LPCWSTR name )
+HFONT32 WINAPI CreateFont32W( INT32 height, INT32 width, INT32 esc,
+                              INT32 orient, INT32 weight, DWORD italic,
+                              DWORD underline, DWORD strikeout, DWORD charset,
+                              DWORD outpres, DWORD clippres, DWORD quality,
+                              DWORD pitch, LPCWSTR name )
 {
     LPSTR namea = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
     HFONT32 ret = (HFONT32)CreateFont16( height, width, esc, orient, weight,
@@ -358,7 +361,9 @@
 /***********************************************************************
  *              EnumFontFamiliesEx16	(GDI.613)
  */
-INT16 EnumFontFamiliesEx16( HDC16 hDC, LPLOGFONT16 plf, FONTENUMPROCEX16 efproc, LPARAM lParam, DWORD dwFlags)
+INT16 WINAPI EnumFontFamiliesEx16( HDC16 hDC, LPLOGFONT16 plf,
+                                   FONTENUMPROCEX16 efproc, LPARAM lParam,
+                                   DWORD dwFlags)
 {
     INT16	retVal = 0;
     DC* 	dc = (DC*) GDI_GetObjPtr( hDC, DC_MAGIC );
@@ -419,8 +424,9 @@
 /***********************************************************************
  *              EnumFontFamiliesEx32W	(GDI32.82)
  */
-INT32 EnumFontFamiliesEx32W( HDC32 hDC, LPLOGFONT32W plf, FONTENUMPROCEX32W efproc, 
-					LPARAM lParam, DWORD dwFlags )
+INT32 WINAPI EnumFontFamiliesEx32W( HDC32 hDC, LPLOGFONT32W plf,
+                                    FONTENUMPROCEX32W efproc, 
+                                    LPARAM lParam, DWORD dwFlags )
 {
     return  FONT_EnumFontFamiliesEx32( hDC, plf, (FONTENUMPROC32W)efproc, 
 						  lParam, ENUM_UNICODE );
@@ -429,8 +435,9 @@
 /***********************************************************************
  *              EnumFontFamiliesEx32A	(GDI32.81)
  */
-INT32 EnumFontFamiliesEx32A( HDC32 hDC, LPLOGFONT32A plf, FONTENUMPROCEX32A efproc, 
-					LPARAM lParam, DWORD dwFlags)
+INT32 WINAPI EnumFontFamiliesEx32A( HDC32 hDC, LPLOGFONT32A plf,
+                                    FONTENUMPROCEX32A efproc, 
+                                    LPARAM lParam, DWORD dwFlags)
 {
     return  FONT_EnumFontFamiliesEx32( hDC, (LPLOGFONT32W)plf, 
 				      (FONTENUMPROC32W)efproc, lParam, 0);
@@ -439,7 +446,8 @@
 /***********************************************************************
  *              EnumFontFamilies16	(GDI.330)
  */
-INT16 EnumFontFamilies16( HDC16 hDC, LPCSTR lpFamily, FONTENUMPROC16 efproc, LPARAM lpData )
+INT16 WINAPI EnumFontFamilies16( HDC16 hDC, LPCSTR lpFamily,
+                                 FONTENUMPROC16 efproc, LPARAM lpData )
 {
     LOGFONT16	lf;
 
@@ -453,7 +461,8 @@
 /***********************************************************************
  *              EnumFontFamilies32A	(GDI32.80)
  */
-INT32 EnumFontFamilies32A( HDC32 hDC, LPCSTR lpFamily, FONTENUMPROC32A efproc, LPARAM lpData )
+INT32 WINAPI EnumFontFamilies32A( HDC32 hDC, LPCSTR lpFamily,
+                                  FONTENUMPROC32A efproc, LPARAM lpData )
 {
     LOGFONT32A	lf;
 
@@ -468,7 +477,8 @@
 /***********************************************************************
  *              EnumFontFamilies32W	(GDI32.83)
  */
-INT32 EnumFontFamilies32W( HDC32 hDC, LPCWSTR lpFamily, FONTENUMPROC32W efproc, LPARAM lpData )
+INT32 WINAPI EnumFontFamilies32W( HDC32 hDC, LPCWSTR lpFamily,
+                                  FONTENUMPROC32W efproc, LPARAM lpData )
 {
     LOGFONT32W  lf;
 
@@ -482,7 +492,8 @@
 /***********************************************************************
  *              EnumFonts16		(GDI.70)
  */
-INT16 EnumFonts16( HDC16 hDC, LPCSTR lpName, FONTENUMPROC16 efproc, LPARAM lpData )
+INT16 WINAPI EnumFonts16( HDC16 hDC, LPCSTR lpName, FONTENUMPROC16 efproc,
+                          LPARAM lpData )
 {
     return EnumFontFamilies16( hDC, lpName, (FONTENUMPROCEX16)efproc, lpData );
 }
@@ -490,7 +501,8 @@
 /***********************************************************************
  *              EnumFonts32A		(GDI32.84)
  */
-INT32 EnumFonts32A( HDC32 hDC, LPCSTR lpName, FONTENUMPROC32A efproc, LPARAM lpData )
+INT32 WINAPI EnumFonts32A( HDC32 hDC, LPCSTR lpName, FONTENUMPROC32A efproc,
+                           LPARAM lpData )
 {
     return EnumFontFamilies32A( hDC, lpName, efproc, lpData );
 }
@@ -498,7 +510,8 @@
 /***********************************************************************
  *              EnumFonts32W		(GDI32.85)
  */
-INT32 EnumFonts32W( HDC32 hDC, LPCWSTR lpName, FONTENUMPROC32W efproc, LPARAM lpData )
+INT32 WINAPI EnumFonts32W( HDC32 hDC, LPCWSTR lpName, FONTENUMPROC32W efproc,
+                           LPARAM lpData )
 {
     return EnumFontFamilies32W( hDC, lpName, efproc, lpData );
 }
@@ -507,7 +520,7 @@
 /***********************************************************************
  *           GetTextCharacterExtra16    (GDI.89)
  */
-INT16 GetTextCharacterExtra16( HDC16 hdc )
+INT16 WINAPI GetTextCharacterExtra16( HDC16 hdc )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) return 0;
@@ -519,7 +532,7 @@
 /***********************************************************************
  *           GetTextCharacterExtra32    (GDI32.225)
  */
-INT32 GetTextCharacterExtra32( HDC32 hdc )
+INT32 WINAPI GetTextCharacterExtra32( HDC32 hdc )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) return 0;
@@ -531,7 +544,7 @@
 /***********************************************************************
  *           SetTextCharacterExtra16    (GDI.8)
  */
-INT16 SetTextCharacterExtra16( HDC16 hdc, INT16 extra )
+INT16 WINAPI SetTextCharacterExtra16( HDC16 hdc, INT16 extra )
 {
     return (INT16)SetTextCharacterExtra32( hdc, extra );
 }
@@ -540,7 +553,7 @@
 /***********************************************************************
  *           SetTextCharacterExtra32    (GDI32.337)
  */
-INT32 SetTextCharacterExtra32( HDC32 hdc, INT32 extra )
+INT32 WINAPI SetTextCharacterExtra32( HDC32 hdc, INT32 extra )
 {
     INT32 prev;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -555,7 +568,7 @@
 /***********************************************************************
  *           SetTextJustification16    (GDI.10)
  */
-INT16 SetTextJustification16( HDC16 hdc, INT16 extra, INT16 breaks )
+INT16 WINAPI SetTextJustification16( HDC16 hdc, INT16 extra, INT16 breaks )
 {
     return SetTextJustification32( hdc, extra, breaks );
 }
@@ -564,7 +577,7 @@
 /***********************************************************************
  *           SetTextJustification32    (GDI32.339)
  */
-BOOL32 SetTextJustification32( HDC32 hdc, INT32 extra, INT32 breaks )
+BOOL32 WINAPI SetTextJustification32( HDC32 hdc, INT32 extra, INT32 breaks )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc) return 0;
@@ -590,7 +603,7 @@
 /***********************************************************************
  *           GetTextFace16    (GDI.92)
  */
-INT16 GetTextFace16( HDC16 hdc, INT16 count, LPSTR name )
+INT16 WINAPI GetTextFace16( HDC16 hdc, INT16 count, LPSTR name )
 {
         return GetTextFace32A(hdc,count,name);
 }
@@ -598,7 +611,7 @@
 /***********************************************************************
  *           GetTextFace32A    (GDI32.234)
  */
-INT32 GetTextFace32A( HDC32 hdc, INT32 count, LPSTR name )
+INT32 WINAPI GetTextFace32A( HDC32 hdc, INT32 count, LPSTR name )
 {
     FONTOBJ *font;
 
@@ -607,13 +620,14 @@
     if (!(font = (FONTOBJ *) GDI_GetObjPtr( dc->w.hFont, FONT_MAGIC )))
         return 0;
     lstrcpyn32A( name, font->logfont.lfFaceName, count );
+    GDI_HEAP_UNLOCK( dc->w.hFont );
     return strlen(name);
 }
 
 /***********************************************************************
  *           GetTextFace32W    (GDI32.235)
  */
-INT32 GetTextFace32W( HDC32 hdc, INT32 count, LPWSTR name )
+INT32 WINAPI GetTextFace32W( HDC32 hdc, INT32 count, LPWSTR name )
 {
     LPSTR nameA = HeapAlloc( GetProcessHeap(), 0, count );
     INT32 res = GetTextFace32A(hdc,count,nameA);
@@ -626,7 +640,7 @@
 /***********************************************************************
  *           GetTextExtent    (GDI.91)
  */
-DWORD GetTextExtent( HDC16 hdc, LPCSTR str, INT16 count )
+DWORD WINAPI GetTextExtent( HDC16 hdc, LPCSTR str, INT16 count )
 {
     SIZE16 size;
     if (!GetTextExtentPoint16( hdc, str, count, &size )) return 0;
@@ -641,7 +655,8 @@
  * Original Windows versions of GetTextExtentPoint{A,W} have documented
  * bugs.
  */
-BOOL16 GetTextExtentPoint16( HDC16 hdc, LPCSTR str, INT16 count, LPSIZE16 size)
+BOOL16 WINAPI GetTextExtentPoint16( HDC16 hdc, LPCSTR str, INT16 count,
+                                    LPSIZE16 size )
 {
     SIZE32 size32;
     BOOL32 ret = GetTextExtentPoint32A( hdc, str, count, &size32 );
@@ -653,8 +668,8 @@
 /***********************************************************************
  *           GetTextExtentPoint32A    (GDI32.230)
  */
-BOOL32 GetTextExtentPoint32A( HDC32 hdc, LPCSTR str, INT32 count,
-                              LPSIZE32 size )
+BOOL32 WINAPI GetTextExtentPoint32A( HDC32 hdc, LPCSTR str, INT32 count,
+                                     LPSIZE32 size )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc)
@@ -676,8 +691,8 @@
 /***********************************************************************
  *           GetTextExtentPoint32W    (GDI32.231)
  */
-BOOL32 GetTextExtentPoint32W( HDC32 hdc, LPCWSTR str, INT32 count,
-                              LPSIZE32 size )
+BOOL32 WINAPI GetTextExtentPoint32W( HDC32 hdc, LPCWSTR str, INT32 count,
+                                     LPSIZE32 size )
 {
     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
     BOOL32 ret = GetTextExtentPoint32A( hdc, p, count, size );
@@ -689,8 +704,8 @@
 /***********************************************************************
  *           GetTextExtentPoint32ABuggy    (GDI32.232)
  */
-BOOL32 GetTextExtentPoint32ABuggy( HDC32 hdc, LPCSTR str, INT32 count,
-                                   LPSIZE32 size )
+BOOL32 WINAPI GetTextExtentPoint32ABuggy( HDC32 hdc, LPCSTR str, INT32 count,
+                                          LPSIZE32 size )
 {
     dprintf_font( stddeb, "GetTextExtentPoint32ABuggy: not bug compatible.\n");
     return GetTextExtentPoint32A( hdc, str, count, size );
@@ -699,8 +714,8 @@
 /***********************************************************************
  *           GetTextExtentPoint32WBuggy    (GDI32.233)
  */
-BOOL32 GetTextExtentPoint32WBuggy( HDC32 hdc, LPCWSTR str, INT32 count,
-                                   LPSIZE32 size )
+BOOL32 WINAPI GetTextExtentPoint32WBuggy( HDC32 hdc, LPCWSTR str, INT32 count,
+                                          LPSIZE32 size )
 {
     dprintf_font( stddeb, "GetTextExtentPoint32WBuggy: not bug compatible.\n");
     return GetTextExtentPoint32W( hdc, str, count, size );
@@ -710,9 +725,9 @@
 /***********************************************************************
  *           GetTextExtentExPoint32A    (GDI32.228)
  */
-BOOL32 GetTextExtentExPoint32A( HDC32 hdc, LPCSTR str, INT32 count,
-                                INT32 maxExt,LPINT32 lpnFit, LPINT32 alpDx,
-                                LPSIZE32 size )
+BOOL32 WINAPI GetTextExtentExPoint32A( HDC32 hdc, LPCSTR str, INT32 count,
+                                       INT32 maxExt, LPINT32 lpnFit,
+                                       LPINT32 alpDx, LPSIZE32 size )
 {
     int index, nFit, extent;
     SIZE32 tSize;
@@ -752,9 +767,9 @@
  *           GetTextExtentExPoint32W    (GDI32.229)
  */
 
-BOOL32 GetTextExtentExPoint32W( HDC32 hdc, LPCWSTR str, INT32 count,
-                                INT32 maxExt, LPINT32 lpnFit, LPINT32 alpDx,
-                                LPSIZE32 size )
+BOOL32 WINAPI GetTextExtentExPoint32W( HDC32 hdc, LPCWSTR str, INT32 count,
+                                       INT32 maxExt, LPINT32 lpnFit,
+                                       LPINT32 alpDx, LPSIZE32 size )
 {
     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
     BOOL32 ret = GetTextExtentExPoint32A( hdc, p, count, maxExt,
@@ -766,7 +781,7 @@
 /***********************************************************************
  *           GetTextMetrics16    (GDI.93)
  */
-BOOL16 GetTextMetrics16( HDC16 hdc, TEXTMETRIC16 *metrics )
+BOOL16 WINAPI GetTextMetrics16( HDC16 hdc, TEXTMETRIC16 *metrics )
 {
     TEXTMETRIC32A tm32;
 
@@ -779,7 +794,7 @@
 /***********************************************************************
  *           GetTextMetrics32A    (GDI32.236)
  */
-BOOL32 GetTextMetrics32A( HDC32 hdc, TEXTMETRIC32A *metrics )
+BOOL32 WINAPI GetTextMetrics32A( HDC32 hdc, TEXTMETRIC32A *metrics )
 {
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
     if (!dc)
@@ -791,8 +806,26 @@
     if (!dc->funcs->pGetTextMetrics ||
         !dc->funcs->pGetTextMetrics( dc,metrics ))
         return FALSE;
+    /* map them from device to logic coordinatesystem before returning.
+     * FIXME: should this be in the device driver?
+     */
+#define WDPTOLP(x) ((x<0)?					\
+		(-abs((x)*dc->wndExtX/dc->vportExtX)):		\
+		(abs((x)*dc->wndExtX/dc->vportExtX)))
+#define HDPTOLP(y) ((y<0)?					\
+		(-abs((y)*dc->wndExtY/dc->vportExtY)):		\
+		(abs((y)*dc->wndExtY/dc->vportExtY)))
+	
+    metrics->tmHeight           = HDPTOLP(metrics->tmHeight);
+    metrics->tmAscent           = HDPTOLP(metrics->tmAscent);
+    metrics->tmDescent          = HDPTOLP(metrics->tmDescent);
+    metrics->tmInternalLeading  = HDPTOLP(metrics->tmInternalLeading);
+    metrics->tmExternalLeading  = HDPTOLP(metrics->tmExternalLeading);
+    metrics->tmAveCharWidth     = WDPTOLP(metrics->tmAveCharWidth);
+    metrics->tmMaxCharWidth     = WDPTOLP(metrics->tmMaxCharWidth);
+    metrics->tmOverhang         = WDPTOLP(metrics->tmOverhang);
 
-    dprintf_font(stdnimp,"text metrics:
+    dprintf_font(stddeb,"text metrics:
     Weight = %03i\t FirstChar = %03i\t AveCharWidth = %i
     Italic = % 3i\t LastChar = %03i\t\t MaxCharWidth = %i
     UnderLined = %01i\t DefaultChar = %03i\t Overhang = %i
@@ -819,7 +852,7 @@
 /***********************************************************************
  *           GetTextMetrics32W    (GDI32.237)
  */
-BOOL32 GetTextMetrics32W( HDC32 hdc, TEXTMETRIC32W *metrics )
+BOOL32 WINAPI GetTextMetrics32W( HDC32 hdc, TEXTMETRIC32W *metrics )
 {
     TEXTMETRIC32A tm;
     if (!GetTextMetrics32A( (HDC16)hdc, &tm )) return FALSE;
@@ -831,8 +864,8 @@
 /***********************************************************************
  *           GetCharWidth16    (GDI.350)
  */
-BOOL16 GetCharWidth16( HDC16 hdc, UINT16 firstChar, UINT16 lastChar,
-                       LPINT16 buffer )
+BOOL16 WINAPI GetCharWidth16( HDC16 hdc, UINT16 firstChar, UINT16 lastChar,
+                              LPINT16 buffer )
 {
     BOOL32	retVal = FALSE;
 
@@ -867,8 +900,8 @@
 /***********************************************************************
  *           GetCharWidth32A    (GDI32.155)
  */
-BOOL32 GetCharWidth32A( HDC32 hdc, UINT32 firstChar, UINT32 lastChar,
-                        LPINT32 buffer )
+BOOL32 WINAPI GetCharWidth32A( HDC32 hdc, UINT32 firstChar, UINT32 lastChar,
+                               LPINT32 buffer )
 {
     UINT32 i, extra;
     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
@@ -895,8 +928,8 @@
 /***********************************************************************
  *           GetCharWidth32W    (GDI32.158)
  */
-BOOL32 GetCharWidth32W( HDC32 hdc, UINT32 firstChar, UINT32 lastChar,
-                        LPINT32 buffer )
+BOOL32 WINAPI GetCharWidth32W( HDC32 hdc, UINT32 firstChar, UINT32 lastChar,
+                               LPINT32 buffer )
 {
     return GetCharWidth32A( hdc, firstChar, lastChar, buffer );
 }
@@ -908,7 +941,7 @@
  *
  *           SetMapperFlags16    (GDI.349)
  */
-DWORD SetMapperFlags16( HDC16 hDC, DWORD dwFlag )
+DWORD WINAPI SetMapperFlags16( HDC16 hDC, DWORD dwFlag )
 {
     return SetMapperFlags32( hDC, dwFlag );
 }
@@ -917,19 +950,30 @@
 /***********************************************************************
  *           SetMapperFlags32    (GDI32.322)
  */
-DWORD SetMapperFlags32( HDC32 hDC, DWORD dwFlag )
+DWORD WINAPI SetMapperFlags32( HDC32 hDC, DWORD dwFlag )
 {
     dprintf_font(stdnimp,"SetmapperFlags(%04x, %08lX) // Empty Stub !\n",
                  hDC, dwFlag);
     return 0L;
 }
 
+/***********************************************************************
+ *          GetAspectRatioFilterEx16  (GDI.486)
+ */
+BOOL16 GetAspectRatioFilterEx16( HDC16 hdc, LPVOID pAspectRatio )
+{
+  dprintf_font(stdnimp, 
+      "GetAspectRatioFilterEx(%04x, %p): // Empty Stub !\n",
+	       hdc, pAspectRatio);
+  return FALSE;
+}
+
 
 /***********************************************************************
  *           GetCharABCWidths16   (GDI.307)
  */
-BOOL16 GetCharABCWidths16( HDC16 hdc, UINT16 firstChar, UINT16 lastChar,
-                           LPABC16 abc )
+BOOL16 WINAPI GetCharABCWidths16( HDC16 hdc, UINT16 firstChar, UINT16 lastChar,
+                                  LPABC16 abc )
 {
     ABC32 abc32;
     if (!GetCharABCWidths32A( hdc, firstChar, lastChar, &abc32 )) return FALSE;
@@ -943,8 +987,8 @@
 /***********************************************************************
  *           GetCharABCWidths32A   (GDI32.149)
  */
-BOOL32 GetCharABCWidths32A( HDC32 hdc, UINT32 firstChar, UINT32 lastChar,
-                            LPABC32 abc )
+BOOL32 WINAPI GetCharABCWidths32A(HDC32 hdc, UINT32 firstChar, UINT32 lastChar,
+                                  LPABC32 abc )
 {
     /* No TrueType fonts in Wine so far */
     fprintf( stdnimp, "STUB: GetCharABCWidths(%04x,%04x,%04x,%p)\n",
@@ -956,8 +1000,8 @@
 /***********************************************************************
  *           GetCharABCWidths32W   (GDI32.152)
  */
-BOOL32 GetCharABCWidths32W( HDC32 hdc, UINT32 firstChar, UINT32 lastChar,
-                            LPABC32 abc )
+BOOL32 WINAPI GetCharABCWidths32W(HDC32 hdc, UINT32 firstChar, UINT32 lastChar,
+                                  LPABC32 abc )
 {
     return GetCharABCWidths32A( hdc, firstChar, lastChar, abc );
 }
@@ -966,9 +1010,9 @@
 /***********************************************************************
  *           GetGlyphOutline16    (GDI.309)
  */
-DWORD GetGlyphOutline16( HDC16 hdc, UINT16 uChar, UINT16 fuFormat,
-                         LPGLYPHMETRICS16 lpgm, DWORD cbBuffer,
-                         LPVOID lpBuffer, const MAT2 *lpmat2 )
+DWORD WINAPI GetGlyphOutline16( HDC16 hdc, UINT16 uChar, UINT16 fuFormat,
+                                LPGLYPHMETRICS16 lpgm, DWORD cbBuffer,
+                                LPVOID lpBuffer, const MAT2 *lpmat2 )
 {
     fprintf( stdnimp,"GetGlyphOutLine16(%04x, '%c', %04x, %p, %ld, %p, %p) // - empty stub!\n",
              hdc, uChar, fuFormat, lpgm, cbBuffer, lpBuffer, lpmat2 );
@@ -979,9 +1023,9 @@
 /***********************************************************************
  *           GetGlyphOutline32A    (GDI32.186)
  */
-DWORD GetGlyphOutline32A( HDC32 hdc, UINT32 uChar, UINT32 fuFormat,
-                         LPGLYPHMETRICS32 lpgm, DWORD cbBuffer,
-                         LPVOID lpBuffer, const MAT2 *lpmat2 )
+DWORD WINAPI GetGlyphOutline32A( HDC32 hdc, UINT32 uChar, UINT32 fuFormat,
+                                 LPGLYPHMETRICS32 lpgm, DWORD cbBuffer,
+                                 LPVOID lpBuffer, const MAT2 *lpmat2 )
 {
     fprintf( stdnimp,"GetGlyphOutLine32A(%04x, '%c', %04x, %p, %ld, %p, %p) // - empty stub!\n",
              hdc, uChar, fuFormat, lpgm, cbBuffer, lpBuffer, lpmat2 );
@@ -991,9 +1035,9 @@
 /***********************************************************************
  *           GetGlyphOutline32W    (GDI32.187)
  */
-DWORD GetGlyphOutline32W( HDC32 hdc, UINT32 uChar, UINT32 fuFormat,
-                         LPGLYPHMETRICS32 lpgm, DWORD cbBuffer,
-                         LPVOID lpBuffer, const MAT2 *lpmat2 )
+DWORD WINAPI GetGlyphOutline32W( HDC32 hdc, UINT32 uChar, UINT32 fuFormat,
+                                 LPGLYPHMETRICS32 lpgm, DWORD cbBuffer,
+                                 LPVOID lpBuffer, const MAT2 *lpmat2 )
 {
     fprintf( stdnimp,"GetGlyphOutLine32W(%04x, '%c', %04x, %p, %ld, %p, %p) // - empty stub!\n",
              hdc, uChar, fuFormat, lpgm, cbBuffer, lpBuffer, lpmat2 );
@@ -1003,8 +1047,9 @@
 /***********************************************************************
  *           CreateScalableFontResource16   (GDI.310)
  */
-BOOL16 CreateScalableFontResource16( UINT16 fHidden, LPCSTR lpszResourceFile,
-                                     LPCSTR fontFile, LPCSTR path )
+BOOL16 WINAPI CreateScalableFontResource16( UINT16 fHidden,
+                                            LPCSTR lpszResourceFile,
+                                            LPCSTR fontFile, LPCSTR path )
 {
     return CreateScalableFontResource32A( fHidden, lpszResourceFile,
                                           fontFile, path );
@@ -1013,9 +1058,10 @@
 /***********************************************************************
  *           CreateScalableFontResource32A   (GDI32.62)
  */
-BOOL32 CreateScalableFontResource32A( DWORD fHidden, LPCSTR lpszResourceFile,
-                                      LPCSTR lpszFontFile,
-                                      LPCSTR lpszCurrentPath )
+BOOL32 WINAPI CreateScalableFontResource32A( DWORD fHidden,
+                                             LPCSTR lpszResourceFile,
+                                             LPCSTR lpszFontFile,
+                                             LPCSTR lpszCurrentPath )
 {
     /* fHidden=1 - only visible for the calling app, read-only, not
      * enumbered with EnumFonts/EnumFontFamilies
@@ -1029,9 +1075,10 @@
 /***********************************************************************
  *           CreateScalableFontResource32W   (GDI32.63)
  */
-BOOL32 CreateScalableFontResource32W( DWORD fHidden, LPCWSTR lpszResourceFile,
-                                      LPCWSTR lpszFontFile,
-                                      LPCWSTR lpszCurrentPath )
+BOOL32 WINAPI CreateScalableFontResource32W( DWORD fHidden,
+                                             LPCWSTR lpszResourceFile,
+                                             LPCWSTR lpszFontFile,
+                                             LPCWSTR lpszCurrentPath )
 {
     fprintf(stdnimp,"CreateScalableFontResource32W(%ld,%p,%p,%p) // empty stub\n",
             fHidden, lpszResourceFile, lpszFontFile, lpszCurrentPath );
@@ -1042,7 +1089,7 @@
 /*************************************************************************
  *             GetRasterizerCaps16   (GDI.313)
  */
-BOOL16 GetRasterizerCaps16( LPRASTERIZER_STATUS lprs, UINT16 cbNumBytes )
+BOOL16 WINAPI GetRasterizerCaps16( LPRASTERIZER_STATUS lprs, UINT16 cbNumBytes)
 {
     return GetRasterizerCaps32( lprs, cbNumBytes );
 }
@@ -1051,7 +1098,7 @@
 /*************************************************************************
  *             GetRasterizerCaps32   (GDI32.216)
  */
-BOOL32 GetRasterizerCaps32( LPRASTERIZER_STATUS lprs, UINT32 cbNumBytes )
+BOOL32 WINAPI GetRasterizerCaps32( LPRASTERIZER_STATUS lprs, UINT32 cbNumBytes)
 {
   RASTERIZER_STATUS rs;
 
@@ -1065,8 +1112,8 @@
 /*************************************************************************
  *             GetKerningPairs16   (GDI.332)
  */
-INT16 GetKerningPairs16( HDC16 hDC, INT16 cPairs,
-                         LPKERNINGPAIR16 lpKerningPairs )
+INT16 WINAPI GetKerningPairs16( HDC16 hDC, INT16 cPairs,
+                                LPKERNINGPAIR16 lpKerningPairs )
 {
     /* At this time kerning is ignored (set to 0) */
     int i;
@@ -1080,8 +1127,8 @@
 /*************************************************************************
  *             GetKerningPairs32A   (GDI32.192)
  */
-DWORD GetKerningPairs32A( HDC32 hDC, DWORD cPairs,
-                          LPKERNINGPAIR32 lpKerningPairs )
+DWORD WINAPI GetKerningPairs32A( HDC32 hDC, DWORD cPairs,
+                                 LPKERNINGPAIR32 lpKerningPairs )
 {
     int i;
     fprintf(stdnimp,"GetKerningPairs32: almost empty stub!\n");
@@ -1093,9 +1140,16 @@
 /*************************************************************************
  *             GetKerningPairs32W   (GDI32.193)
  */
-DWORD GetKerningPairs32W( HDC32 hDC, DWORD cPairs,
-                          LPKERNINGPAIR32 lpKerningPairs )
+DWORD WINAPI GetKerningPairs32W( HDC32 hDC, DWORD cPairs,
+                                 LPKERNINGPAIR32 lpKerningPairs )
 {
     return GetKerningPairs32A( hDC, cPairs, lpKerningPairs );
 }
 
+BOOL32 WINAPI TranslateCharSetInfo(LPDWORD lpSrc,LPCHARSETINFO lpCs,DWORD dwFlags) {
+    fprintf(stderr,"TranslateCharSetInfo(%p,%p,0x%08lx), stub.\n",
+    	lpSrc,lpCs,dwFlags
+    );
+    return TRUE;
+}
+
diff --git a/objects/gdiobj.c b/objects/gdiobj.c
index 3d59435..6c80245 100644
--- a/objects/gdiobj.c
+++ b/objects/gdiobj.c
@@ -231,11 +231,13 @@
     {
 	/* Create default palette */
 
+      /* DR well *this* palette can't be moveable (?) */
+
 	HPALETTE16 hpalette = PALETTE_Init();
 
 	if( hpalette )
 	{
-	    StockObjects[DEFAULT_PALETTE] = (GDIOBJHDR *)GDI_HEAP_LIN_ADDR( hpalette );
+	    StockObjects[DEFAULT_PALETTE] = (GDIOBJHDR *)GDI_HEAP_LOCK( hpalette );
 	    return TRUE;
 	}
     }
@@ -250,12 +252,17 @@
 {
     static DWORD count = 0;
     GDIOBJHDR * obj;
-    HGDIOBJ16 handle = GDI_HEAP_ALLOC( size );
+    HGDIOBJ16 handle;
+    if ( magic == DC_MAGIC || magic == METAFILE_DC_MAGIC )
+      handle = GDI_HEAP_ALLOC( size );
+    else 
+      handle = GDI_HEAP_ALLOC_MOVEABLE( size );
     if (!handle) return 0;
-    obj = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
+    obj = (GDIOBJHDR *) GDI_HEAP_LOCK( handle );
     obj->hNext   = 0;
     obj->wMagic  = magic;
     obj->dwCount = ++count;
+    GDI_HEAP_UNLOCK( handle );
     return handle;
 }
 
@@ -271,10 +278,10 @@
     if ((handle >= FIRST_STOCK_HANDLE) && (handle <= LAST_STOCK_HANDLE))
         return TRUE;
     
-    object = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
+    object = (GDIOBJHDR *) GDI_HEAP_LOCK( handle );
     if (!object) return FALSE;
     object->wMagic = 0;  /* Mark it as invalid */
-
+ 
       /* Free object */
     
     GDI_HEAP_FREE( handle );
@@ -286,6 +293,8 @@
  *
  * Return a pointer to the GDI object associated to the handle.
  * Return NULL if the object has the wrong magic number.
+ * Movable GDI objects are locked in memory: it is up to the caller to unlock
+ * it after the caller is done with the pointer.
  */
 GDIOBJHDR * GDI_GetObjPtr( HGDIOBJ16 handle, WORD magic )
 {
@@ -294,9 +303,13 @@
     if ((handle >= FIRST_STOCK_HANDLE) && (handle <= LAST_STOCK_HANDLE))
       ptr = StockObjects[handle - FIRST_STOCK_HANDLE];
     else 
-      ptr = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
+      ptr = (GDIOBJHDR *) GDI_HEAP_LOCK( handle );
     if (!ptr) return NULL;
-    if ((magic != MAGIC_DONTCARE) && (ptr->wMagic != magic)) return NULL;
+    if ((magic != MAGIC_DONTCARE) && (ptr->wMagic != magic)) 
+    {
+      GDI_HEAP_UNLOCK( handle );
+      return NULL;
+    }
     return ptr;
 }
 
@@ -304,7 +317,7 @@
 /***********************************************************************
  *           DeleteObject16    (GDI.69)
  */
-BOOL16 DeleteObject16( HGDIOBJ16 obj )
+BOOL16 WINAPI DeleteObject16( HGDIOBJ16 obj )
 {
     return DeleteObject32( obj );
 }
@@ -313,12 +326,15 @@
 /***********************************************************************
  *           DeleteObject32    (GDI32.70)
  */
-BOOL32 DeleteObject32( HGDIOBJ32 obj )
+BOOL32 WINAPI DeleteObject32( HGDIOBJ32 obj )
 {
       /* Check if object is valid */
 
-    GDIOBJHDR * header = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( obj );
-    if (!header || HIWORD(obj)) return FALSE;
+    GDIOBJHDR * header;
+    if (HIWORD(obj)) return FALSE;
+    if ((obj >= FIRST_STOCK_HANDLE) && (obj <= LAST_STOCK_HANDLE))
+        return TRUE;
+    if (!(header = (GDIOBJHDR *) GDI_HEAP_LOCK( obj ))) return FALSE;
 
     dprintf_gdi(stddeb, "DeleteObject: %04x\n", obj );
 
@@ -340,7 +356,7 @@
 /***********************************************************************
  *           GetStockObject16    (GDI.87)
  */
-HGDIOBJ16 GetStockObject16( INT16 obj )
+HGDIOBJ16 WINAPI GetStockObject16( INT16 obj )
 {
     return (HGDIOBJ16)GetStockObject32( obj );
 }
@@ -349,7 +365,7 @@
 /***********************************************************************
  *           GetStockObject32    (GDI32.220)
  */
-HGDIOBJ32 GetStockObject32( INT32 obj )
+HGDIOBJ32 WINAPI GetStockObject32( INT32 obj )
 {
     if ((obj < 0) || (obj >= NB_STOCK_OBJECTS)) return 0;
     if (!StockObjects[obj]) return 0;
@@ -362,73 +378,86 @@
 /***********************************************************************
  *           GetObject16    (GDI.82)
  */
-INT16 GetObject16( HANDLE16 handle, INT16 count, LPVOID buffer )
+INT16 WINAPI GetObject16( HANDLE16 handle, INT16 count, LPVOID buffer )
 {
     GDIOBJHDR * ptr = NULL;
+    INT16 result = 0;
     dprintf_gdi(stddeb, "GetObject16: %04x %d %p\n", handle, count, buffer );
     if (!count) return 0;
 
     if ((handle >= FIRST_STOCK_HANDLE) && (handle <= LAST_STOCK_HANDLE))
       ptr = StockObjects[handle - FIRST_STOCK_HANDLE];
     else
-      ptr = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
+      ptr = (GDIOBJHDR *) GDI_HEAP_LOCK( handle );
     if (!ptr) return 0;
     
     switch(ptr->wMagic)
-    {
+      {
       case PEN_MAGIC:
-	  return PEN_GetObject16( (PENOBJ *)ptr, count, buffer );
+	result = PEN_GetObject16( (PENOBJ *)ptr, count, buffer );
+	break;
       case BRUSH_MAGIC: 
-	  return BRUSH_GetObject16( (BRUSHOBJ *)ptr, count, buffer );
+	result = BRUSH_GetObject16( (BRUSHOBJ *)ptr, count, buffer );
+	break;
       case BITMAP_MAGIC: 
-	  return BITMAP_GetObject16( (BITMAPOBJ *)ptr, count, buffer );
+	result = BITMAP_GetObject16( (BITMAPOBJ *)ptr, count, buffer );
+	break;
       case FONT_MAGIC:
-	  return FONT_GetObject16( (FONTOBJ *)ptr, count, buffer );
+	result = FONT_GetObject16( (FONTOBJ *)ptr, count, buffer );
+	break;
       case PALETTE_MAGIC:
-	  return PALETTE_GetObject( (PALETTEOBJ *)ptr, count, buffer );
-    }
-    return 0;
+	result = PALETTE_GetObject( (PALETTEOBJ *)ptr, count, buffer );
+	break;
+      }
+    GDI_HEAP_UNLOCK( handle );
+    return result;
 }
 
 
 /***********************************************************************
  *           GetObject32A    (GDI32.204)
  */
-INT32 GetObject32A( HANDLE32 handle, INT32 count, LPVOID buffer )
+INT32 WINAPI GetObject32A( HANDLE32 handle, INT32 count, LPVOID buffer )
 {
     GDIOBJHDR * ptr = NULL;
+    INT32 result = 0;
     dprintf_gdi(stddeb, "GetObject32A: %08x %d %p\n", handle, count, buffer );
     if (!count) return 0;
 
     if ((handle >= FIRST_STOCK_HANDLE) && (handle <= LAST_STOCK_HANDLE))
       ptr = StockObjects[handle - FIRST_STOCK_HANDLE];
     else
-      ptr = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
+      ptr = (GDIOBJHDR *) GDI_HEAP_LOCK( handle );
     if (!ptr) return 0;
     
     switch(ptr->wMagic)
     {
       case PEN_MAGIC:
-	  return PEN_GetObject32( (PENOBJ *)ptr, count, buffer );
+	  result = PEN_GetObject32( (PENOBJ *)ptr, count, buffer );
+	  break;
       case BRUSH_MAGIC: 
-	  return BRUSH_GetObject32( (BRUSHOBJ *)ptr, count, buffer );
+	  result = BRUSH_GetObject32( (BRUSHOBJ *)ptr, count, buffer );
+	  break;
       case BITMAP_MAGIC: 
-	  return BITMAP_GetObject32( (BITMAPOBJ *)ptr, count, buffer );
+	  result = BITMAP_GetObject32( (BITMAPOBJ *)ptr, count, buffer );
+	  break;
       case FONT_MAGIC:
-	  return FONT_GetObject32A( (FONTOBJ *)ptr, count, buffer );
+	  result = FONT_GetObject32A( (FONTOBJ *)ptr, count, buffer );
+	  break;
       case PALETTE_MAGIC:
           fprintf( stderr, "GetObject32: magic %04x not implemented\n",
                    ptr->wMagic );
           break;
     }
-    return 0;
+    GDI_HEAP_UNLOCK( handle );
+    return result;
 }
 
 
 /***********************************************************************
  *           GetObject32W    (GDI32.206)
  */
-INT32 GetObject32W( HANDLE32 handle, INT32 count, LPVOID buffer )
+INT32 WINAPI GetObject32W( HANDLE32 handle, INT32 count, LPVOID buffer )
 {
     return GetObject32A( handle, count, buffer );
 }
@@ -437,7 +466,7 @@
 /***********************************************************************
  *           SelectObject16    (GDI.45)
  */
-HGDIOBJ16 SelectObject16( HDC16 hdc, HGDIOBJ16 handle )
+HGDIOBJ16 WINAPI SelectObject16( HDC16 hdc, HGDIOBJ16 handle )
 {
     return (HGDIOBJ16)SelectObject32( hdc, handle );
 }
@@ -446,7 +475,7 @@
 /***********************************************************************
  *           SelectObject32    (GDI32.299)
  */
-HGDIOBJ32 SelectObject32( HDC32 hdc, HGDIOBJ32 handle )
+HGDIOBJ32 WINAPI SelectObject32( HDC32 hdc, HGDIOBJ32 handle )
 {
     DC * dc = DC_GetDCPtr( hdc );
     if (!dc || !dc->funcs->pSelectObject) return 0;
@@ -458,7 +487,7 @@
 /***********************************************************************
  *           UnrealizeObject16    (GDI.150)
  */
-BOOL16 UnrealizeObject16( HGDIOBJ16 obj )
+BOOL16 WINAPI UnrealizeObject16( HGDIOBJ16 obj )
 {
     return UnrealizeObject32( obj );
 }
@@ -467,11 +496,12 @@
 /***********************************************************************
  *           UnrealizeObject    (GDI32.358)
  */
-BOOL32 UnrealizeObject32( HGDIOBJ32 obj )
+BOOL32 WINAPI UnrealizeObject32( HGDIOBJ32 obj )
 {
-      /* Check if object is valid */
+    BOOL32 result = TRUE;
+  /* Check if object is valid */
 
-    GDIOBJHDR * header = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( obj );
+    GDIOBJHDR * header = (GDIOBJHDR *) GDI_HEAP_LOCK( obj );
     if (!header) return FALSE;
 
     dprintf_gdi( stddeb, "UnrealizeObject: %04x\n", obj );
@@ -481,21 +511,23 @@
     switch(header->wMagic)
     {
     case PALETTE_MAGIC: 
-        return PALETTE_UnrealizeObject( obj, (PALETTEOBJ *)header );
+        result = PALETTE_UnrealizeObject( obj, (PALETTEOBJ *)header );
+	break;
 
     case BRUSH_MAGIC:
         /* Windows resets the brush origin. We don't need to. */
         break;
     }
-    return TRUE;
+    GDI_HEAP_UNLOCK( obj );
+    return result;
 }
 
 
 /***********************************************************************
  *           EnumObjects16    (GDI.71)
  */
-INT16 EnumObjects16( HDC16 hdc, INT16 nObjType, GOBJENUMPROC16 lpEnumFunc,
-                     LPARAM lParam )
+INT16 WINAPI EnumObjects16( HDC16 hdc, INT16 nObjType,
+                            GOBJENUMPROC16 lpEnumFunc, LPARAM lParam )
 {
     /* Solid colors to enumerate */
     static const COLORREF solid_colors[] =
@@ -573,8 +605,8 @@
 /***********************************************************************
  *           EnumObjects32    (GDI32.89)
  */
-INT32 EnumObjects32( HDC32 hdc, INT32 nObjType, GOBJENUMPROC32 lpEnumFunc,
-                     LPARAM lParam )
+INT32 WINAPI EnumObjects32( HDC32 hdc, INT32 nObjType,
+                            GOBJENUMPROC32 lpEnumFunc, LPARAM lParam )
 {
     /* Solid colors to enumerate */
     static const COLORREF solid_colors[] =
@@ -648,12 +680,19 @@
 
 /***********************************************************************
  *           IsGDIObject    (GDI.462)
+ * 
+ * returns type of object if valid (W95 system programming secrets p. 264-5)
  */
-BOOL16 IsGDIObject( HGDIOBJ16 handle )
+BOOL16 WINAPI IsGDIObject( HGDIOBJ16 handle )
 {
-    GDIOBJHDR *object = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
+    GDIOBJHDR *object = (GDIOBJHDR *) GDI_HEAP_LOCK( handle );
     if (object)
-      return (object->wMagic>=PEN_MAGIC && object->wMagic<= METAFILE_DC_MAGIC);
+    {
+        UINT16 magic = object->wMagic;
+        GDI_HEAP_UNLOCK( handle );
+        if (magic >= PEN_MAGIC && magic <= METAFILE_DC_MAGIC)
+            return magic - PEN_MAGIC + 1;
+    }
     return FALSE;
 }
 
@@ -661,7 +700,7 @@
 /***********************************************************************
  *           MulDiv16   (GDI.128)
  */
-INT16 MulDiv16( INT16 foo, INT16 bar, INT16 baz )
+INT16 WINAPI MulDiv16( INT16 foo, INT16 bar, INT16 baz )
 {
     INT32 ret;
     if (!baz) return -32768;
@@ -674,7 +713,7 @@
 /***********************************************************************
  *           MulDiv32   (KERNEL32.391)
  */
-INT32 MulDiv32( INT32 foo, INT32 bar, INT32 baz )
+INT32 WINAPI MulDiv32( INT32 foo, INT32 bar, INT32 baz )
 {
 #ifdef __GNUC__
     long long ret;
diff --git a/objects/linedda.c b/objects/linedda.c
index d0cf871..7297860 100644
--- a/objects/linedda.c
+++ b/objects/linedda.c
@@ -11,8 +11,8 @@
 /**********************************************************************
  *           LineDDA16   (GDI.100)
  */
-void LineDDA16( INT16 nXStart, INT16 nYStart, INT16 nXEnd, INT16 nYEnd,
-                LINEDDAPROC16 callback, LPARAM lParam )
+void WINAPI LineDDA16( INT16 nXStart, INT16 nYStart, INT16 nXEnd, INT16 nYEnd,
+                       LINEDDAPROC16 callback, LPARAM lParam )
 {
     LineDDA32( nXStart, nYStart, nXEnd, nYEnd,
                (LINEDDAPROC32)callback, lParam );
@@ -22,8 +22,8 @@
 /**********************************************************************
  *           LineDDA32   (GDI32.248)
  */
-BOOL32 LineDDA32( INT32 nXStart, INT32 nYStart, INT32 nXEnd, INT32 nYEnd,
-                  LINEDDAPROC32 callback, LPARAM lParam )
+BOOL32 WINAPI LineDDA32(INT32 nXStart, INT32 nYStart, INT32 nXEnd, INT32 nYEnd,
+                        LINEDDAPROC32 callback, LPARAM lParam )
 {
     INT32 xadd = 1, yadd = 1;
     INT32 err,erradd;
diff --git a/objects/metafile.c b/objects/metafile.c
index 87e0b34..ad8622a 100644
--- a/objects/metafile.c
+++ b/objects/metafile.c
@@ -57,7 +57,7 @@
 /******************************************************************
  *         GetMetafile16   (GDI.124)
  */
-HMETAFILE16 GetMetaFile16( LPCSTR lpFilename )
+HMETAFILE16 WINAPI GetMetaFile16( LPCSTR lpFilename )
 {
     return GetMetaFile32A( lpFilename );
 }
@@ -66,7 +66,7 @@
 /******************************************************************
  *         GetMetafile32A   (GDI32.197)
  */
-HMETAFILE32 GetMetaFile32A( LPCSTR lpFilename )
+HMETAFILE32 WINAPI GetMetaFile32A( LPCSTR lpFilename )
 {
   HMETAFILE16 hmf;
   METAHEADER *mh;
@@ -137,7 +137,7 @@
 /******************************************************************
  *         GetMetafile32W   (GDI32.199)
  */
-HMETAFILE32 GetMetaFile32W( LPCWSTR lpFilename )
+HMETAFILE32 WINAPI GetMetaFile32W( LPCWSTR lpFilename )
 {
     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFilename );
     HMETAFILE32 ret = GetMetaFile32A( p );
@@ -150,7 +150,7 @@
  *         CopyMetaFile16   (GDI.151)
  */
 
-HMETAFILE16 CopyMetaFile16( HMETAFILE16 hSrcMetaFile, LPCSTR lpFilename )
+HMETAFILE16 WINAPI CopyMetaFile16( HMETAFILE16 hSrcMetaFile, LPCSTR lpFilename )
 {
     return CopyMetaFile32A( hSrcMetaFile, lpFilename );
 }
@@ -159,7 +159,7 @@
 /******************************************************************
  *         CopyMetaFile32A   (GDI32.23)
  */
-HMETAFILE32 CopyMetaFile32A( HMETAFILE32 hSrcMetaFile, LPCSTR lpFilename )
+HMETAFILE32 WINAPI CopyMetaFile32A(HMETAFILE32 hSrcMetaFile, LPCSTR lpFilename)
 {
     HMETAFILE16 handle = 0;
     METAHEADER *mh;
@@ -201,7 +201,8 @@
 /******************************************************************
  *         CopyMetaFile32W   (GDI32.24)
  */
-HMETAFILE32 CopyMetaFile32W( HMETAFILE32 hSrcMetaFile, LPCWSTR lpFilename )
+HMETAFILE32 WINAPI CopyMetaFile32W( HMETAFILE32 hSrcMetaFile,
+                                    LPCWSTR lpFilename )
 {
     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, lpFilename );
     HMETAFILE32 ret = CopyMetaFile32A( hSrcMetaFile, p );
@@ -215,7 +216,7 @@
  *         (This is not exactly what windows does, see "Undoc Win")
  */
 
-BOOL16 IsValidMetaFile(HMETAFILE16 hmf)
+BOOL16 WINAPI IsValidMetaFile(HMETAFILE16 hmf)
 {
     BOOL16 resu=FALSE;
     METAHEADER *mh = (METAHEADER *)GlobalLock16(hmf);
@@ -234,7 +235,7 @@
 /******************************************************************
  *         PlayMetafile16   (GDI.123)
  */
-BOOL16 PlayMetaFile16( HDC16 hdc, HMETAFILE16 hmf )
+BOOL16 WINAPI PlayMetaFile16( HDC16 hdc, HMETAFILE16 hmf )
 {
     return PlayMetaFile32( hdc, hmf );
 }
@@ -243,7 +244,7 @@
 /******************************************************************
  *         PlayMetafile32   (GDI32.265)
  */
-BOOL32 PlayMetaFile32( HDC32 hdc, HMETAFILE32 hmf )
+BOOL32 WINAPI PlayMetaFile32( HDC32 hdc, HMETAFILE32 hmf )
 {
     METAHEADER *mh = (METAHEADER *)GlobalLock16(hmf);
     METARECORD *mr;
@@ -262,7 +263,7 @@
     hPen = dc->w.hPen;
     hBrush = dc->w.hBrush;
     hFont = dc->w.hFont;
-
+    GDI_HEAP_UNLOCK(hdc);
     /* create the handle table */
     hHT = GlobalAlloc16(GMEM_MOVEABLE|GMEM_ZEROINIT,
 		      sizeof(HANDLETABLE16) * mh->mtNoObjects);
@@ -299,8 +300,8 @@
  *            EnumMetaFile16   (GDI.175)
  *                                    Niels de carpentier, april 1996
  */
-BOOL16 EnumMetaFile16( HDC16 hdc, HMETAFILE16 hmf, MFENUMPROC16 lpEnumFunc,
-                       LPARAM lpData )
+BOOL16 WINAPI EnumMetaFile16( HDC16 hdc, HMETAFILE16 hmf,
+                              MFENUMPROC16 lpEnumFunc, LPARAM lpData )
 {
     METAHEADER *mh = (METAHEADER *)GlobalLock16(hmf);
     METARECORD *mr;
@@ -321,7 +322,8 @@
     hPen = dc->w.hPen;
     hBrush = dc->w.hBrush;
     hFont = dc->w.hFont;
-   
+    GDI_HEAP_UNLOCK(hdc);
+
     /* create the handle table */
     
     hHT = GlobalAlloc16(GMEM_MOVEABLE | GMEM_ZEROINIT,
@@ -365,8 +367,8 @@
 /******************************************************************
  *             PlayMetaFileRecord16   (GDI.176)
  */
-void PlayMetaFileRecord16( HDC16 hdc, HANDLETABLE16 *ht, METARECORD *mr,
-                           UINT16 nHandles )
+void WINAPI PlayMetaFileRecord16( HDC16 hdc, HANDLETABLE16 *ht, METARECORD *mr,
+                                  UINT16 nHandles )
 {
     short s1;
     HANDLE16 hndl;
@@ -818,7 +820,7 @@
  * Trade in a meta file object handle for a handle to the meta file memory
  */
 
-HGLOBAL16 GetMetaFileBits(HMETAFILE16 hmf)
+HGLOBAL16 WINAPI GetMetaFileBits(HMETAFILE16 hmf)
 {
     dprintf_metafile(stddeb,"GetMetaFileBits: hMem out: %04x\n", hmf);
 
@@ -830,7 +832,7 @@
  *
  * Trade in a meta file memory handle for a handle to a meta file object
  */
-HMETAFILE16 SetMetaFileBits( HGLOBAL16 hMem )
+HMETAFILE16 WINAPI SetMetaFileBits( HGLOBAL16 hMem )
 {
     dprintf_metafile(stddeb,"SetMetaFileBits: hmf out: %04x\n", hMem);
 
@@ -840,7 +842,7 @@
 /******************************************************************
  *         SetMetaFileBitsBetter   (GDI.196)
  */
-HMETAFILE16 SetMetaFileBitsBetter( HMETAFILE16 hMeta )
+HMETAFILE16 WINAPI SetMetaFileBitsBetter( HMETAFILE16 hMeta )
 {
    if( IsValidMetaFile( hMeta ) )
        return (HMETAFILE16)GlobalReAlloc16( hMeta, 0, 
@@ -1044,7 +1046,10 @@
 	len = sizeof(METARECORD) + sizeof(BITMAPINFOHEADER) + 
 	      (bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes) + 6;
 	if (!(hmr = GlobalAlloc16(GMEM_MOVEABLE, len)))
+	  {
+	    GDI_HEAP_UNLOCK((HGDIOBJ16)logbrush->lbHatch);
 	    return FALSE;
+	  }
 	mr = (METARECORD *)GlobalLock16(hmr);
 	memset(mr, 0, len);
 	mr->rdFunction = META_DIBCREATEPATTERNBRUSH;
@@ -1060,6 +1065,7 @@
 	memcpy(mr->rdParam + (sizeof(BITMAPINFOHEADER) / 2) + 4,
 	       PTR_SEG_TO_LIN(bmp->bitmap.bmBits),
 	       bmp->bitmap.bmHeight * bmp->bitmap.bmWidthBytes);
+	GDI_HEAP_UNLOCK(logbrush->lbHatch);
 	break;
 
     case BS_DIBPATTERN:
diff --git a/objects/oembitmap.c b/objects/oembitmap.c
index 514cb78..2a3cb2d 100644
--- a/objects/oembitmap.c
+++ b/objects/oembitmap.c
@@ -249,6 +249,17 @@
 
 static XpmColorSymbol *OBM_Colors = NULL;
 
+/* This structure holds the arguments for OBM_CreateBitmaps() */
+typedef struct
+{
+    char     **data;      /* In: bitmap data */
+    BOOL32     color;     /* In: color or monochrome */
+    BOOL32     need_mask; /* In: do we need a mask? */
+    HBITMAP16  bitmap;    /* Out: resulting bitmap */
+    HBITMAP16  mask;      /* Out: resulting mask (if needed) */
+    POINT32    hotspot;   /* Out: bitmap hotspot */
+} OBM_BITMAP_DESCR;
+
 
 /***********************************************************************
  *           OBM_InitColorSymbols
@@ -293,7 +304,7 @@
     hbitmap = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC );
     if (!hbitmap) return 0;
 
-    bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LIN_ADDR( hbitmap );
+    bmpObjPtr = (BITMAPOBJ *) GDI_HEAP_LOCK( hbitmap );
     bmpObjPtr->size.cx = width;
     bmpObjPtr->size.cy = height;
     bmpObjPtr->pixmap  = pixmap;
@@ -304,6 +315,7 @@
     bmpObjPtr->bitmap.bmPlanes     = 1;
     bmpObjPtr->bitmap.bmBitsPixel  = bpp;
     bmpObjPtr->bitmap.bmBits       = NULL;
+    GDI_HEAP_UNLOCK( hbitmap );
     return hbitmap;
 }
 
@@ -313,8 +325,7 @@
  *
  * Create the 2 bitmaps from XPM data.
  */
-static BOOL32 OBM_CreateBitmaps( char **data, BOOL32 color, HBITMAP16 *hBitmap,
-                                 HBITMAP16 *hBitmapMask, POINT32 *hotspot )
+static BOOL32 OBM_CreateBitmaps( OBM_BITMAP_DESCR *descr )
 {
     Pixmap pixmap, pixmask;
     XpmAttributes *attrs;
@@ -323,11 +334,11 @@
     attrs = (XpmAttributes *)xmalloc( XpmAttributesSize() );
     attrs->valuemask    = XpmColormap | XpmDepth | XpmColorSymbols |XpmHotspot;
     attrs->colormap     = COLOR_GetColormap();
-    attrs->depth        = color ? screenDepth : 1;
+    attrs->depth        = descr->color ? screenDepth : 1;
     attrs->colorsymbols = (attrs->depth > 1) ? OBM_Colors : OBM_BlackAndWhite;
     attrs->numsymbols   = (attrs->depth > 1) ? NB_COLOR_SYMBOLS : 2;
         
-    err = XpmCreatePixmapFromData( display, rootWindow, data,
+    err = XpmCreatePixmapFromData( display, rootWindow, descr->data,
                                    &pixmap, &pixmask, attrs );
 
     if (err != XpmSuccess)
@@ -335,22 +346,20 @@
         free( attrs );
         return FALSE;
     }
-    if (hotspot)
-    {
-        hotspot->x = attrs->x_hotspot;
-        hotspot->y = attrs->y_hotspot;
-    }
-    *hBitmap = OBM_MakeBitmap( attrs->width, attrs->height,
-                               attrs->depth, pixmap );
-    if (hBitmapMask) *hBitmapMask = OBM_MakeBitmap(attrs->width, attrs->height,
-                                                   1, pixmask );
+    descr->hotspot.x = attrs->x_hotspot;
+    descr->hotspot.y = attrs->y_hotspot;
+    descr->bitmap = OBM_MakeBitmap( attrs->width, attrs->height,
+                                    attrs->depth, pixmap );
+    if (descr->need_mask)
+        descr->mask = OBM_MakeBitmap( attrs->width, attrs->height,
+                                      1, pixmask );
     free( attrs );
-    if (!*hBitmap)
+    if (!descr->bitmap)
     {
         if (pixmap) XFreePixmap( display, pixmap );
         if (pixmask) XFreePixmap( display, pixmask );
-        if (*hBitmap) GDI_FreeObject( *hBitmap );
-        if (hBitmapMask && *hBitmapMask) GDI_FreeObject( *hBitmapMask );
+        if (descr->bitmap) GDI_FreeObject( descr->bitmap );
+        if (descr->need_mask && descr->mask) GDI_FreeObject( descr->mask );
         return FALSE;
     }
     else return TRUE;
@@ -362,22 +371,23 @@
  */
 HBITMAP16 OBM_LoadBitmap( WORD id )
 {
-    HBITMAP16 hbitmap;
+    OBM_BITMAP_DESCR descr;
 
     if ((id < OBM_FIRST) || (id > OBM_LAST)) return 0;
     id -= OBM_FIRST;
 
     if (!OBM_InitColorSymbols()) return 0;
-    
-    if (!CallTo32_LargeStack( (int(*)())OBM_CreateBitmaps, 5,
-                              OBM_Pixmaps_Data[id].data,
-                              OBM_Pixmaps_Data[id].color,
-                              &hbitmap, NULL, NULL ))
+
+    descr.data      = OBM_Pixmaps_Data[id].data;
+    descr.color     = OBM_Pixmaps_Data[id].color;
+    descr.need_mask = FALSE;
+
+    if (!CALL_LARGE_STACK( OBM_CreateBitmaps, &descr ))
     {
         fprintf( stderr, "Error creating OEM bitmap %d\n", OBM_FIRST+id );
         return 0;
     }
-    return hbitmap;
+    return descr.bitmap;
 }
 
 
@@ -386,11 +396,10 @@
  */
 HGLOBAL16 OBM_LoadCursorIcon( WORD id, BOOL32 fCursor )
 {
+    OBM_BITMAP_DESCR descr;
     HGLOBAL16 handle;
     CURSORICONINFO *pInfo;
     BITMAPOBJ *bmpXor, *bmpAnd;
-    HBITMAP16 hXorBits, hAndBits;
-    POINT32 hotspot;
     int sizeXor, sizeAnd;
 
     if (fCursor)
@@ -412,37 +421,39 @@
 
     if (!OBM_InitColorSymbols()) return 0;
     
-    if (!CallTo32_LargeStack( (int(*)())OBM_CreateBitmaps, 5,
-                           fCursor ? OBM_Cursors_Data[id] : OBM_Icons_Data[id],
-                           !fCursor, &hXorBits, &hAndBits, &hotspot ))
+    descr.data      = fCursor ? OBM_Cursors_Data[id] : OBM_Icons_Data[id];
+    descr.color     = !fCursor;
+    descr.need_mask = TRUE;
+
+    if (!CALL_LARGE_STACK( OBM_CreateBitmaps, &descr ))
     {
         fprintf( stderr, "Error creating OEM cursor/icon %d\n", id );
         return 0;
     }
 
-    bmpXor = (BITMAPOBJ *) GDI_GetObjPtr( hXorBits, BITMAP_MAGIC );
-    bmpAnd = (BITMAPOBJ *) GDI_GetObjPtr( hAndBits, BITMAP_MAGIC );
+    bmpXor = (BITMAPOBJ *) GDI_GetObjPtr( descr.bitmap, BITMAP_MAGIC );
+    bmpAnd = (BITMAPOBJ *) GDI_GetObjPtr( descr.mask, BITMAP_MAGIC );
     sizeXor = bmpXor->bitmap.bmHeight * bmpXor->bitmap.bmWidthBytes;
     sizeAnd = bmpXor->bitmap.bmHeight * BITMAP_WIDTH_BYTES( bmpXor->bitmap.bmWidth, 1 );
 
     if (!(handle = GlobalAlloc16( GMEM_MOVEABLE,
                                   sizeof(CURSORICONINFO) + sizeXor + sizeAnd)))
     {
-        DeleteObject32( hXorBits );
-        DeleteObject32( hAndBits );
+        DeleteObject32( descr.bitmap );
+        DeleteObject32( descr.mask );
         return 0;
     }
 
     pInfo = (CURSORICONINFO *)GlobalLock16( handle );
-    pInfo->ptHotSpot.x   = hotspot.x;
-    pInfo->ptHotSpot.y   = hotspot.y;
+    pInfo->ptHotSpot.x   = descr.hotspot.x;
+    pInfo->ptHotSpot.y   = descr.hotspot.y;
     pInfo->nWidth        = bmpXor->bitmap.bmWidth;
     pInfo->nHeight       = bmpXor->bitmap.bmHeight;
     pInfo->nWidthBytes   = bmpXor->bitmap.bmWidthBytes;
     pInfo->bPlanes       = bmpXor->bitmap.bmPlanes;
     pInfo->bBitsPerPixel = bmpXor->bitmap.bmBitsPixel;
 
-    if (hAndBits)
+    if (descr.mask)
     {
           /* Invert the mask */
 
@@ -466,12 +477,12 @@
         }
     }
 
-    if (hAndBits) GetBitmapBits32( hAndBits, sizeAnd, (char *)(pInfo + 1) );
+    if (descr.mask) GetBitmapBits32( descr.mask, sizeAnd, (char *)(pInfo + 1));
     else memset( (char *)(pInfo + 1), 0xff, sizeAnd );
-    GetBitmapBits32( hXorBits, sizeXor, (char *)(pInfo + 1) + sizeAnd );
+    GetBitmapBits32( descr.bitmap, sizeXor, (char *)(pInfo + 1) + sizeAnd );
 
-    DeleteObject32( hXorBits );
-    DeleteObject32( hAndBits );
+    DeleteObject32( descr.bitmap );
+    DeleteObject32( descr.mask );
 
     if (fCursor) OBM_Cursors[id] = handle;
     return handle;
diff --git a/objects/palette.c b/objects/palette.c
index 74207b8..0049425 100644
--- a/objects/palette.c
+++ b/objects/palette.c
@@ -64,6 +64,8 @@
 
     palObj->mapping = xmalloc( sizeof(int) * 20 );
 
+    GDI_HEAP_UNLOCK( hpalette );
+
     free( palPtr );
     return hpalette;
 }
@@ -82,7 +84,7 @@
 /***********************************************************************
  *           CreatePalette16    (GDI.360)
  */
-HPALETTE16 CreatePalette16( const LOGPALETTE* palette )
+HPALETTE16 WINAPI CreatePalette16( const LOGPALETTE* palette )
 {
     return CreatePalette32( palette );
 }
@@ -91,7 +93,7 @@
 /***********************************************************************
  *           CreatePalette32    (GDI32.53)
  */
-HPALETTE32 CreatePalette32( const LOGPALETTE* palette )
+HPALETTE32 WINAPI CreatePalette32( const LOGPALETTE* palette )
 {
     PALETTEOBJ * palettePtr;
     HPALETTE32 hpalette;
@@ -103,11 +105,12 @@
     hpalette = GDI_AllocObject( size + sizeof(int*) +sizeof(GDIOBJHDR) , PALETTE_MAGIC );
     if (!hpalette) return 0;
 
-    palettePtr = (PALETTEOBJ *) GDI_HEAP_LIN_ADDR( hpalette );
+    palettePtr = (PALETTEOBJ *) GDI_HEAP_LOCK( hpalette );
     memcpy( &palettePtr->logpalette, palette, size );
     PALETTE_ValidateFlags(palettePtr->logpalette.palPalEntry, 
 			  palettePtr->logpalette.palNumEntries);
     palettePtr->mapping = NULL;
+    GDI_HEAP_UNLOCK( hpalette );
 
     dprintf_palette(stddeb,"returning %04x\n", hpalette);
     return hpalette;
@@ -117,8 +120,8 @@
 /***********************************************************************
  *           GetPaletteEntries16    (GDI.363)
  */
-UINT16 GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start, UINT16 count,
-                            LPPALETTEENTRY entries )
+UINT16 WINAPI GetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
+                                   UINT16 count, LPPALETTEENTRY entries )
 {
     return GetPaletteEntries32( hpalette, start, count, entries );
 }
@@ -127,8 +130,8 @@
 /***********************************************************************
  *           GetPaletteEntries32    (GDI32.209)
  */
-UINT32 GetPaletteEntries32( HPALETTE32 hpalette, UINT32 start, UINT32 count,
-                            LPPALETTEENTRY entries )
+UINT32 WINAPI GetPaletteEntries32( HPALETTE32 hpalette, UINT32 start,
+                                   UINT32 count, LPPALETTEENTRY entries )
 {
     PALETTEOBJ * palPtr;
     INT32 numEntries;
@@ -140,13 +143,18 @@
     if (!palPtr) return 0;
 
     numEntries = palPtr->logpalette.palNumEntries;
-    if (start >= numEntries) return 0;
+    if (start >= numEntries) 
+    {
+      GDI_HEAP_UNLOCK( hpalette );
+      return 0;
+    }
     if (start+count > numEntries) count = numEntries - start;
     memcpy( entries, &palPtr->logpalette.palPalEntry[start],
 	    count * sizeof(PALETTEENTRY) );
     for( numEntries = 0; numEntries < count ; numEntries++ )
          if (entries[numEntries].peFlags & 0xF0)
              entries[numEntries].peFlags = 0;
+    GDI_HEAP_UNLOCK( hpalette );
     return count;
 }
 
@@ -154,8 +162,8 @@
 /***********************************************************************
  *           SetPaletteEntries16    (GDI.364)
  */
-UINT16 SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start, UINT16 count,
-                            LPPALETTEENTRY entries )
+UINT16 WINAPI SetPaletteEntries16( HPALETTE16 hpalette, UINT16 start,
+                                   UINT16 count, LPPALETTEENTRY entries )
 {
     return SetPaletteEntries32( hpalette, start, count, entries );
 }
@@ -164,8 +172,8 @@
 /***********************************************************************
  *           SetPaletteEntries32    (GDI32.326)
  */
-UINT32 SetPaletteEntries32( HPALETTE32 hpalette, UINT32 start, UINT32 count,
-                            LPPALETTEENTRY entries )
+UINT32 WINAPI SetPaletteEntries32( HPALETTE32 hpalette, UINT32 start,
+                                   UINT32 count, LPPALETTEENTRY entries )
 {
     PALETTEOBJ * palPtr;
     INT32 numEntries;
@@ -177,7 +185,11 @@
     if (!palPtr) return 0;
 
     numEntries = palPtr->logpalette.palNumEntries;
-    if (start >= numEntries) return 0;
+    if (start >= numEntries) 
+    {
+      GDI_HEAP_UNLOCK( hpalette );
+      return 0;
+    }
     if (start+count > numEntries) count = numEntries - start;
     memcpy( &palPtr->logpalette.palPalEntry[start], entries,
 	    count * sizeof(PALETTEENTRY) );
@@ -185,7 +197,7 @@
 			  palPtr->logpalette.palNumEntries);
     free(palPtr->mapping);
     palPtr->mapping = NULL;
-
+    GDI_HEAP_UNLOCK( hpalette );
     return count;
 }
 
@@ -193,7 +205,7 @@
 /***********************************************************************
  *           ResizePalette16   (GDI.368)
  */
-BOOL16 ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
+BOOL16 WINAPI ResizePalette16( HPALETTE16 hPal, UINT16 cEntries )
 {
     return ResizePalette32( hPal, cEntries );
 }
@@ -202,7 +214,7 @@
 /***********************************************************************
  *           ResizePalette32   (GDI32.289)
  */
-BOOL32 ResizePalette32( HPALETTE32 hPal, UINT32 cEntries )
+BOOL32 WINAPI ResizePalette32( HPALETTE32 hPal, UINT32 cEntries )
 {
     PALETTEOBJ * palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
     UINT32	 cPrevEnt, prevVer;
@@ -219,7 +231,9 @@
 	      				sizeof(int*) + sizeof(GDIOBJHDR);
     size += sizeof(int*) + sizeof(GDIOBJHDR);
     mapping = palPtr->mapping;
-
+    
+    GDI_HEAP_UNLOCK( hPal );
+    
     hPal = GDI_HEAP_REALLOC( hPal, size );
     palPtr = (PALETTEOBJ *) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
     if( !palPtr ) return FALSE;
@@ -236,6 +250,7 @@
     }
     palPtr->logpalette.palNumEntries = cEntries;
     palPtr->logpalette.palVersion = prevVer;
+    GDI_HEAP_UNLOCK( hPal );
     return TRUE;
 }
 
@@ -243,28 +258,47 @@
 /***********************************************************************
  *           AnimatePalette16   (GDI.367)
  */
-BOOL16 AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex, UINT16 NumEntries,
-                         LPPALETTEENTRY PaletteColors )
+void WINAPI AnimatePalette16( HPALETTE16 hPal, UINT16 StartIndex,
+                              UINT16 NumEntries, LPPALETTEENTRY PaletteColors)
 {
-    return AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
+    AnimatePalette32( hPal, StartIndex, NumEntries, PaletteColors );
 }
 
 
 /***********************************************************************
  *           AnimatePalette32   (GDI32.6)
+ *
+ * FIXME: should use existing mapping when animating a primary palette
  */
-BOOL32 AnimatePalette32( HPALETTE32 hPal, UINT32 StartIndex, UINT32 NumEntries,
-                         LPPALETTEENTRY PaletteColors )
+BOOL32 WINAPI AnimatePalette32( HPALETTE32 hPal, UINT32 StartIndex,
+                               UINT32 NumEntries, LPPALETTEENTRY PaletteColors)
 {
-    fprintf(stdnimp,"AnimatePalette: empty stub! \n");
-    return TRUE;
+    dprintf_palette(stddeb, "AnimatePalette: %04x (%i - %i)", hPal, 
+                    StartIndex, StartIndex + NumEntries );
+
+    if( hPal != STOCK_DEFAULT_PALETTE ) 
+    {
+        PALETTEOBJ* palPtr = (PALETTEOBJ *)GDI_GetObjPtr(hPal, PALETTE_MAGIC);
+
+	if( (StartIndex + NumEntries) < palPtr->logpalette.palNumEntries )
+	{
+	    UINT32 u;
+	    for( u = 0; u < NumEntries; u++ )
+		palPtr->logpalette.palPalEntry[u + StartIndex] = PaletteColors[u];
+	    COLOR_SetMapping(palPtr, StartIndex, NumEntries,
+                             hPal != hPrimaryPalette );
+            GDI_HEAP_UNLOCK( hPal );
+	    return TRUE;
+	}
+    }
+    return FALSE;
 }
 
 
 /***********************************************************************
  *           SetSystemPaletteUse16   (GDI.373)
  */
-UINT16 SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
+UINT16 WINAPI SetSystemPaletteUse16( HDC16 hdc, UINT16 use )
 {
     return SetSystemPaletteUse32( hdc, use );
 }
@@ -273,7 +307,7 @@
 /***********************************************************************
  *           SetSystemPaletteUse32   (GDI32.335)
  */
-UINT32 SetSystemPaletteUse32( HDC32 hdc, UINT32 use )
+UINT32 WINAPI SetSystemPaletteUse32( HDC32 hdc, UINT32 use )
 {
     UINT32 old = SystemPaletteUse;
     fprintf( stdnimp,"SetSystemPaletteUse(%04x,%04x) // empty stub !!!\n",
@@ -286,7 +320,7 @@
 /***********************************************************************
  *           GetSystemPaletteUse16   (GDI.374)
  */
-UINT16 GetSystemPaletteUse16( HDC16 hdc )
+UINT16 WINAPI GetSystemPaletteUse16( HDC16 hdc )
 {
     return SystemPaletteUse;
 }
@@ -295,7 +329,7 @@
 /***********************************************************************
  *           GetSystemPaletteUse32   (GDI32.223)
  */
-UINT32 GetSystemPaletteUse32( HDC32 hdc )
+UINT32 WINAPI GetSystemPaletteUse32( HDC32 hdc )
 {
     return SystemPaletteUse;
 }
@@ -304,8 +338,8 @@
 /***********************************************************************
  *           GetSystemPaletteEntries16   (GDI.375)
  */
-UINT16 GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
-                                  LPPALETTEENTRY entries )
+UINT16 WINAPI GetSystemPaletteEntries16( HDC16 hdc, UINT16 start, UINT16 count,
+                                         LPPALETTEENTRY entries )
 {
     return GetSystemPaletteEntries32( hdc, start, count, entries );
 }
@@ -314,8 +348,8 @@
 /***********************************************************************
  *           GetSystemPaletteEntries32   (GDI32.222)
  */
-UINT32 GetSystemPaletteEntries32( HDC32 hdc, UINT32 start, UINT32 count,
-                                  LPPALETTEENTRY entries )
+UINT32 WINAPI GetSystemPaletteEntries32( HDC32 hdc, UINT32 start, UINT32 count,
+                                         LPPALETTEENTRY entries )
 {
     UINT32 i;
     DC *dc;
@@ -323,7 +357,11 @@
     dprintf_palette(stddeb,"GetSystemPaletteEntries: hdc = %04x, cound = %i", hdc, count );
 
     if (!(dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC ))) return 0;
-    if (start >= dc->w.devCaps->sizePalette) return 0;
+    if (start >= dc->w.devCaps->sizePalette)
+      {
+	GDI_HEAP_UNLOCK( hdc );
+	return 0;
+      }
     if (start+count >= dc->w.devCaps->sizePalette)
 	count = dc->w.devCaps->sizePalette - start;
     for (i = 0; i < count; i++)
@@ -333,6 +371,7 @@
         dprintf_palette( stddeb,"\tidx(%02x) -> RGB(%08lx)\n",
                          start + i, *(COLORREF*)(entries + i) );
     }
+    GDI_HEAP_UNLOCK( hdc );
     return count;
 }
 
@@ -340,7 +379,7 @@
 /***********************************************************************
  *           GetNearestPaletteIndex16   (GDI.370)
  */
-UINT16 GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
+UINT16 WINAPI GetNearestPaletteIndex16( HPALETTE16 hpalette, COLORREF color )
 {
     return GetNearestPaletteIndex32( hpalette, color );
 }
@@ -349,7 +388,7 @@
 /***********************************************************************
  *           GetNearestPaletteIndex32   (GDI32.203)
  */
-UINT32 GetNearestPaletteIndex32( HPALETTE32 hpalette, COLORREF color )
+UINT32 WINAPI GetNearestPaletteIndex32( HPALETTE32 hpalette, COLORREF color )
 {
     PALETTEOBJ*	palObj = (PALETTEOBJ*)GDI_GetObjPtr( hpalette, PALETTE_MAGIC );
     UINT32 index  = 0;
@@ -361,6 +400,7 @@
 
     dprintf_palette(stddeb,"GetNearestPaletteIndex(%04x,%06lx): returning %d\n", 
                     hpalette, color, index );
+    GDI_HEAP_UNLOCK( hpalette );
     return index;
 }
 
@@ -368,7 +408,7 @@
 /***********************************************************************
  *           GetNearestColor16   (GDI.154)
  */
-COLORREF GetNearestColor16( HDC16 hdc, COLORREF color )
+COLORREF WINAPI GetNearestColor16( HDC16 hdc, COLORREF color )
 {
     return GetNearestColor32( hdc, color );
 }
@@ -377,7 +417,7 @@
 /***********************************************************************
  *           GetNearestColor32   (GDI32.202)
  */
-COLORREF GetNearestColor32( HDC32 hdc, COLORREF color )
+COLORREF WINAPI GetNearestColor32( HDC32 hdc, COLORREF color )
 {
     COLORREF 	 nearest = 0xFADECAFE;
     DC 		*dc;
@@ -391,10 +431,12 @@
 
       nearest = COLOR_LookupNearestColor( palObj->logpalette.palPalEntry,
 					  palObj->logpalette.palNumEntries, color );
+      GDI_HEAP_UNLOCK( dc->w.hPalette );
     }
 
     dprintf_palette(stddeb,"GetNearestColor(%06lx): returning %06lx\n", 
                     color, nearest );
+    GDI_HEAP_UNLOCK( hdc );    
     return nearest;
 }
 
@@ -431,6 +473,7 @@
 BOOL32 PALETTE_DeleteObject( HPALETTE16 hpalette, PALETTEOBJ *palette )
 {
     free( palette->mapping );
+    if (hLastRealizedPalette == hpalette) hLastRealizedPalette = 0;
     return GDI_FreeObject( hpalette );
 }
 
@@ -438,7 +481,7 @@
 /***********************************************************************
  *           GDISelectPalette    (GDI.361)
  */
-HPALETTE16 GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
+HPALETTE16 WINAPI GDISelectPalette( HDC16 hdc, HPALETTE16 hpal, WORD wBkg)
 {
     HPALETTE16 prev;
     DC *dc;
@@ -453,6 +496,7 @@
     }
     prev = dc->w.hPalette;
     dc->w.hPalette = hpal;
+    GDI_HEAP_UNLOCK( hdc );
     if (!wBkg) hPrimaryPalette = hpal; 
     return prev;
 }
@@ -461,7 +505,7 @@
 /***********************************************************************
  *           GDIRealizePalette    (GDI.362)
  */
-UINT16 GDIRealizePalette( HDC16 hdc )
+UINT16 WINAPI GDIRealizePalette( HDC16 hdc )
 {
     PALETTEOBJ* palPtr;
     int realized = 0;
@@ -481,12 +525,16 @@
 
         palPtr = (PALETTEOBJ *) GDI_GetObjPtr( dc->w.hPalette, PALETTE_MAGIC );
         
-	realized = COLOR_SetMapping(palPtr, dc->w.hPalette != hPrimaryPalette 
-                                  || dc->w.hPalette == STOCK_DEFAULT_PALETTE );
+        realized = COLOR_SetMapping(palPtr,0,palPtr->logpalette.palNumEntries,
+                                    (dc->w.hPalette != hPrimaryPalette) ||
+                                    (dc->w.hPalette == STOCK_DEFAULT_PALETTE));
+	GDI_HEAP_UNLOCK( dc->w.hPalette );
 	hLastRealizedPalette = dc->w.hPalette;
     }
-    else dprintf_palette(stddeb, " skipping ");
+    else dprintf_palette(stddeb, " skipping (hLastRealizedPalette = %04x) ",
+			 hLastRealizedPalette);
     
+    GDI_HEAP_UNLOCK( hdc );
     dprintf_palette(stdnimp, " realized %i colors\n", realized );
     return (UINT16)realized;
 }
@@ -495,7 +543,7 @@
 /***********************************************************************
  *           RealizeDefaultPalette    (GDI.365)
  */
-UINT16 RealizeDefaultPalette( HDC16 hdc )
+UINT16 WINAPI RealizeDefaultPalette( HDC16 hdc )
 {
     DC          *dc;
     PALETTEOBJ*  palPtr;
@@ -510,7 +558,11 @@
 	if (!dc) return 0;
     }
 
-    if ( dc->w.flags & DC_MEMORY ) return 0;
+    if ( dc->w.flags & DC_MEMORY ) 
+      {
+	GDI_HEAP_UNLOCK( hdc );
+	return 0;
+      }
 
     hPrimaryPalette = STOCK_DEFAULT_PALETTE;
     hLastRealizedPalette = STOCK_DEFAULT_PALETTE;
@@ -533,18 +585,23 @@
 /***********************************************************************
  *           IsDCCurrentPalette   (GDI.412)
  */
-BOOL16 IsDCCurrentPalette(HDC16 hDC)
+BOOL16 WINAPI IsDCCurrentPalette(HDC16 hDC)
 {
     DC* dc = (DC *)GDI_GetObjPtr( hDC, DC_MAGIC );
-    return (dc)?(dc->w.hPalette == hPrimaryPalette):FALSE;
+    if (dc) 
+    {
+      GDI_HEAP_UNLOCK( hDC );
+      return dc->w.hPalette == hPrimaryPalette;
+    }
+    return FALSE;
 }
 
 
 /***********************************************************************
  *           SelectPalette16    (USER.282)
  */
-HPALETTE16 SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
-                            BOOL16 bForceBackground )
+HPALETTE16 WINAPI SelectPalette16( HDC16 hDC, HPALETTE16 hPal,
+                                   BOOL16 bForceBackground )
 {
     return SelectPalette32( hDC, hPal, bForceBackground );
 }
@@ -553,8 +610,8 @@
 /***********************************************************************
  *           SelectPalette32    (GDI32.300)
  */
-HPALETTE32 SelectPalette32( HDC32 hDC, HPALETTE32 hPal,
-                            BOOL32 bForceBackground )
+HPALETTE32 WINAPI SelectPalette32( HDC32 hDC, HPALETTE32 hPal,
+                                   BOOL32 bForceBackground )
 {
     WORD	wBkgPalette = 1;
     PALETTEOBJ* lpt = (PALETTEOBJ*) GDI_GetObjPtr( hPal, PALETTE_MAGIC );
@@ -565,6 +622,7 @@
 
     dprintf_palette(stddeb," entries = %d\n", 
 			    lpt->logpalette.palNumEntries);
+    GDI_HEAP_UNLOCK( hPal );
 
     if( hPal != STOCK_DEFAULT_PALETTE )
     {
@@ -584,7 +642,7 @@
 /***********************************************************************
  *           RealizePalette16    (USER.283)
  */
-UINT16 RealizePalette16( HDC16 hDC )
+UINT16 WINAPI RealizePalette16( HDC16 hDC )
 {
     return RealizePalette32( hDC );
 }
@@ -593,7 +651,7 @@
 /***********************************************************************
  *           RealizePalette32    (GDI32.280)
  */
-UINT32 RealizePalette32( HDC32 hDC )
+UINT32 WINAPI RealizePalette32( HDC32 hDC )
 {
     UINT32 realized = GDIRealizePalette( hDC );
 
@@ -615,7 +673,7 @@
 /**********************************************************************
  *            UpdateColors16   (GDI.366)
  */
-INT16 UpdateColors16( HDC16 hDC )
+INT16 WINAPI UpdateColors16( HDC16 hDC )
 {
     HWND32 hWnd = WindowFromDC32( hDC );
 
@@ -631,7 +689,7 @@
 /**********************************************************************
  *            UpdateColors32   (GDI32.359)
  */
-BOOL32 UpdateColors32( HDC32 hDC )
+BOOL32 WINAPI UpdateColors32( HDC32 hDC )
 {
     UpdateColors16( hDC );
     return TRUE;
diff --git a/objects/pen.c b/objects/pen.c
index f33b791..c2c63ad 100644
--- a/objects/pen.c
+++ b/objects/pen.c
@@ -15,7 +15,7 @@
 /***********************************************************************
  *           CreatePen16    (GDI.61)
  */
-HPEN16 CreatePen16( INT16 style, INT16 width, COLORREF color )
+HPEN16 WINAPI CreatePen16( INT16 style, INT16 width, COLORREF color )
 {
     LOGPEN32 logpen = { style, { width, 0 }, color };
     dprintf_gdi(stddeb, "CreatePen16: %d %d %06lx\n", style, width, color );
@@ -26,7 +26,7 @@
 /***********************************************************************
  *           CreatePen32    (GDI32.55)
  */
-HPEN32 CreatePen32( INT32 style, INT32 width, COLORREF color )
+HPEN32 WINAPI CreatePen32( INT32 style, INT32 width, COLORREF color )
 {
     LOGPEN32 logpen = { style, { width, 0 }, color };
     dprintf_gdi(stddeb, "CreatePen32: %d %d %06lx\n", style, width, color );
@@ -37,7 +37,7 @@
 /***********************************************************************
  *           CreatePenIndirect16    (GDI.62)
  */
-HPEN16 CreatePenIndirect16( const LOGPEN16 * pen )
+HPEN16 WINAPI CreatePenIndirect16( const LOGPEN16 * pen )
 {
     PENOBJ * penPtr;
     HPEN16 hpen;
@@ -45,10 +45,11 @@
     if (pen->lopnStyle > PS_INSIDEFRAME) return 0;
     hpen = GDI_AllocObject( sizeof(PENOBJ), PEN_MAGIC );
     if (!hpen) return 0;
-    penPtr = (PENOBJ *)GDI_HEAP_LIN_ADDR( hpen );
+    penPtr = (PENOBJ *)GDI_HEAP_LOCK( hpen );
     penPtr->logpen.lopnStyle = pen->lopnStyle;
     penPtr->logpen.lopnColor = pen->lopnColor;
     CONV_POINT16TO32( &pen->lopnWidth, &penPtr->logpen.lopnWidth );
+    GDI_HEAP_UNLOCK( hpen );
     return hpen;
 }
 
@@ -56,7 +57,7 @@
 /***********************************************************************
  *           CreatePenIndirect32    (GDI32.56)
  */
-HPEN32 CreatePenIndirect32( const LOGPEN32 * pen )
+HPEN32 WINAPI CreatePenIndirect32( const LOGPEN32 * pen )
 {
     PENOBJ * penPtr;
     HPEN32 hpen;
@@ -64,10 +65,11 @@
     if (pen->lopnStyle > PS_INSIDEFRAME) return 0;
     hpen = GDI_AllocObject( sizeof(PENOBJ), PEN_MAGIC );
     if (!hpen) return 0;
-    penPtr = (PENOBJ *)GDI_HEAP_LIN_ADDR( hpen );
+    penPtr = (PENOBJ *)GDI_HEAP_LOCK( hpen );
     penPtr->logpen.lopnStyle = pen->lopnStyle;
     penPtr->logpen.lopnWidth = pen->lopnWidth;
     penPtr->logpen.lopnColor = pen->lopnColor;
+    GDI_HEAP_UNLOCK( hpen );
     return hpen;
 }
 
diff --git a/objects/region.c b/objects/region.c
index 9148e6d..a8e86e8 100644
--- a/objects/region.c
+++ b/objects/region.c
@@ -27,7 +27,7 @@
 /***********************************************************************
  *           OffsetRgn16    (GDI.101)
  */
-INT16 OffsetRgn16( HRGN16 hrgn, INT16 x, INT16 y )
+INT16 WINAPI OffsetRgn16( HRGN16 hrgn, INT16 x, INT16 y )
 {
     return OffsetRgn32( hrgn, x, y );
 }
@@ -36,13 +36,18 @@
 /***********************************************************************
  *           OffsetRgn32   (GDI32.256)
  */
-INT32 OffsetRgn32( HRGN32 hrgn, INT32 x, INT32 y )
+INT32 WINAPI OffsetRgn32( HRGN32 hrgn, INT32 x, INT32 y )
 {
     RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
     if (!obj) return ERROR;
     dprintf_region(stddeb, "OffsetRgn: %04x %d,%d\n", hrgn, x, y );
-    if (!obj->xrgn) return NULLREGION;
+    if (!obj->xrgn) 
+    {
+      GDI_HEAP_UNLOCK( hrgn );
+      return NULLREGION;
+    }
     XOffsetRegion( obj->xrgn, x, y );
+    GDI_HEAP_UNLOCK( hrgn );
     return COMPLEXREGION;
 }
 
@@ -50,7 +55,7 @@
 /***********************************************************************
  *           GetRgnBox16    (GDI.134)
  */
-INT16 GetRgnBox16( HRGN16 hrgn, LPRECT16 rect )
+INT16 WINAPI GetRgnBox16( HRGN16 hrgn, LPRECT16 rect )
 {
     RGNOBJ * obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC );
     if (!obj) return ERROR;
@@ -58,12 +63,14 @@
     if (!obj->xrgn)
     {
         SetRectEmpty16( rect );
+	GDI_HEAP_UNLOCK( hrgn );
         return NULLREGION;
     }
     else
     {
         XRectangle xrect;
         XClipBox( obj->xrgn, &xrect );
+	GDI_HEAP_UNLOCK(hrgn);
         SetRect16( rect, xrect.x, xrect.y,
                    xrect.x + xrect.width, xrect.y + xrect.height);
         return COMPLEXREGION;
@@ -74,7 +81,7 @@
 /***********************************************************************
  *           GetRgnBox32    (GDI32.219)
  */
-INT32 GetRgnBox32( HRGN32 hrgn, LPRECT32 rect )
+INT32 WINAPI GetRgnBox32( HRGN32 hrgn, LPRECT32 rect )
 {
     RECT16 r;
     INT16 ret = GetRgnBox16( hrgn, &r );
@@ -86,7 +93,7 @@
 /***********************************************************************
  *           CreateRectRgn16    (GDI.64)
  */
-HRGN16 CreateRectRgn16( INT16 left, INT16 top, INT16 right, INT16 bottom )
+HRGN16 WINAPI CreateRectRgn16(INT16 left, INT16 top, INT16 right, INT16 bottom)
 {
     return (HRGN16)CreateRectRgn32( left, top, right, bottom );
 }
@@ -95,13 +102,13 @@
 /***********************************************************************
  *           CreateRectRgn32   (GDI32.59)
  */
-HRGN32 CreateRectRgn32( INT32 left, INT32 top, INT32 right, INT32 bottom )
+HRGN32 WINAPI CreateRectRgn32(INT32 left, INT32 top, INT32 right, INT32 bottom)
 {
     HRGN32 hrgn;
     RGNOBJ *obj;
 
     if (!(hrgn = GDI_AllocObject( sizeof(RGNOBJ), REGION_MAGIC ))) return 0;
-    obj = (RGNOBJ *) GDI_HEAP_LIN_ADDR( hrgn );
+    obj = (RGNOBJ *) GDI_HEAP_LOCK( hrgn );
     if ((right > left) && (bottom > top))
     {
 	XRectangle rect = { left, top, right - left, bottom - top };
@@ -115,6 +122,7 @@
     else obj->xrgn = 0;
     dprintf_region( stddeb, "CreateRectRgn(%d,%d-%d,%d): returning %04x\n",
                     left, top, right, bottom, hrgn );
+    GDI_HEAP_UNLOCK( hrgn );
     return hrgn;
 }
 
@@ -122,7 +130,7 @@
 /***********************************************************************
  *           CreateRectRgnIndirect16    (GDI.65)
  */
-HRGN16 CreateRectRgnIndirect16( const RECT16* rect )
+HRGN16 WINAPI CreateRectRgnIndirect16( const RECT16* rect )
 {
     return CreateRectRgn32( rect->left, rect->top, rect->right, rect->bottom );
 }
@@ -131,7 +139,7 @@
 /***********************************************************************
  *           CreateRectRgnIndirect32    (GDI32.60)
  */
-HRGN32 CreateRectRgnIndirect32( const RECT32* rect )
+HRGN32 WINAPI CreateRectRgnIndirect32( const RECT32* rect )
 {
     return CreateRectRgn32( rect->left, rect->top, rect->right, rect->bottom );
 }
@@ -140,8 +148,8 @@
 /***********************************************************************
  *           SetRectRgn16    (GDI.172)
  */
-VOID SetRectRgn16( HRGN16 hrgn, INT16 left, INT16 top,
-                   INT16 right, INT16 bottom )
+VOID WINAPI SetRectRgn16( HRGN16 hrgn, INT16 left, INT16 top,
+                          INT16 right, INT16 bottom )
 {
     SetRectRgn32( hrgn, left, top, right, bottom );
 }
@@ -150,8 +158,8 @@
 /***********************************************************************
  *           SetRectRgn32    (GDI32.332)
  */
-VOID SetRectRgn32( HRGN32 hrgn, INT32 left, INT32 top,
-                   INT32 right, INT32 bottom )
+VOID WINAPI SetRectRgn32( HRGN32 hrgn, INT32 left, INT32 top,
+                          INT32 right, INT32 bottom )
 {
     RGNOBJ * obj;
 
@@ -167,14 +175,16 @@
             XUnionRectWithRegion( &rect, obj->xrgn, obj->xrgn );
     }
     else obj->xrgn = 0;
+    GDI_HEAP_UNLOCK( hrgn );
 }
 
 
 /***********************************************************************
  *           CreateRoundRectRgn16    (GDI.444)
  */
-HRGN16 CreateRoundRectRgn16( INT16 left, INT16 top, INT16 right, INT16 bottom,
-                             INT16 ellipse_width, INT16 ellipse_height )
+HRGN16 WINAPI CreateRoundRectRgn16( INT16 left, INT16 top,
+                                    INT16 right, INT16 bottom,
+                                    INT16 ellipse_width, INT16 ellipse_height )
 {
     return (HRGN16)CreateRoundRectRgn32( left, top, right, bottom,
                                          ellipse_width, ellipse_height );
@@ -184,8 +194,9 @@
 /***********************************************************************
  *           CreateRoundRectRgn32    (GDI32.61)
  */
-HRGN32 CreateRoundRectRgn32( INT32 left, INT32 top, INT32 right, INT32 bottom,
-                             INT32 ellipse_width, INT32 ellipse_height )
+HRGN32 WINAPI CreateRoundRectRgn32( INT32 left, INT32 top,
+                                    INT32 right, INT32 bottom,
+                                    INT32 ellipse_width, INT32 ellipse_height )
 {
     RGNOBJ * obj;
     HRGN32 hrgn;
@@ -201,7 +212,7 @@
       /* Create region */
 
     if (!(hrgn = GDI_AllocObject( sizeof(RGNOBJ), REGION_MAGIC ))) return 0;
-    obj = (RGNOBJ *) GDI_HEAP_LIN_ADDR( hrgn );
+    obj = (RGNOBJ *) GDI_HEAP_LOCK( hrgn );
     obj->xrgn = XCreateRegion();
     dprintf_region(stddeb,"CreateRoundRectRgn(%d,%d-%d,%d %dx%d): return=%04x\n",
                left, top, right, bottom, ellipse_width, ellipse_height, hrgn );
@@ -273,6 +284,7 @@
         rect.height = bottom - top + 1;
         XUnionRectWithRegion( &rect, obj->xrgn, obj->xrgn );
     }
+    GDI_HEAP_UNLOCK( hrgn );
     return hrgn;
 }
 
@@ -280,7 +292,8 @@
 /***********************************************************************
  *           CreateEllipticRgn16    (GDI.54)
  */
-HRGN16 CreateEllipticRgn16( INT16 left, INT16 top, INT16 right, INT16 bottom )
+HRGN16 WINAPI CreateEllipticRgn16( INT16 left, INT16 top,
+                                   INT16 right, INT16 bottom )
 {
     return (HRGN16)CreateRoundRectRgn32( left, top, right, bottom,
                                          right-left, bottom-top );
@@ -290,7 +303,8 @@
 /***********************************************************************
  *           CreateEllipticRgn32    (GDI32.39)
  */
-HRGN32 CreateEllipticRgn32( INT32 left, INT32 top, INT32 right, INT32 bottom )
+HRGN32 WINAPI CreateEllipticRgn32( INT32 left, INT32 top,
+                                   INT32 right, INT32 bottom )
 {
     return CreateRoundRectRgn32( left, top, right, bottom,
                                  right-left, bottom-top );
@@ -300,7 +314,7 @@
 /***********************************************************************
  *           CreateEllipticRgnIndirect16    (GDI.55)
  */
-HRGN16 CreateEllipticRgnIndirect16( const RECT16 *rect )
+HRGN16 WINAPI CreateEllipticRgnIndirect16( const RECT16 *rect )
 {
     return CreateRoundRectRgn32( rect->left, rect->top, rect->right,
                                  rect->bottom, rect->right - rect->left,
@@ -311,7 +325,7 @@
 /***********************************************************************
  *           CreateEllipticRgnIndirect32    (GDI32.40)
  */
-HRGN32 CreateEllipticRgnIndirect32( const RECT32 *rect )
+HRGN32 WINAPI CreateEllipticRgnIndirect32( const RECT32 *rect )
 {
     return CreateRoundRectRgn32( rect->left, rect->top, rect->right,
                                  rect->bottom, rect->right - rect->left,
@@ -322,7 +336,8 @@
 /***********************************************************************
  *           CreatePolygonRgn16    (GDI.63)
  */
-HRGN16 CreatePolygonRgn16( const POINT16 * points, INT16 count, INT16 mode )
+HRGN16 WINAPI CreatePolygonRgn16( const POINT16 * points, INT16 count,
+                                  INT16 mode )
 {
     return CreatePolyPolygonRgn16( points, &count, 1, mode );
 }
@@ -331,8 +346,9 @@
 /***********************************************************************
  *           CreatePolyPolygonRgn16    (GDI.451)
  */
-HRGN16 CreatePolyPolygonRgn16( const POINT16 * points, const INT16 * count,
-                               INT16 nbpolygons, INT16 mode )
+HRGN16 WINAPI CreatePolyPolygonRgn16( const POINT16 * points,
+                                      const INT16 * count,
+                                      INT16 nbpolygons, INT16 mode )
 {
     int		i,nrofpts;
     LPINT32	count32;
@@ -360,7 +376,8 @@
 /***********************************************************************
  *           CreatePolygonRgn32    (GDI32.58)
  */
-HRGN32 CreatePolygonRgn32( const POINT32 *points, INT32 count, INT32 mode )
+HRGN32 WINAPI CreatePolygonRgn32( const POINT32 *points, INT32 count,
+                                  INT32 mode )
 {
     return CreatePolyPolygonRgn32( points, &count, 1, mode );
 }
@@ -369,8 +386,9 @@
 /***********************************************************************
  *           CreatePolyPolygonRgn32    (GDI32.57)
  */
-HRGN32 CreatePolyPolygonRgn32( const POINT32 * points, const INT32 * count,
-                               INT32 nbpolygons, INT32 mode )
+HRGN32 WINAPI CreatePolyPolygonRgn32( const POINT32 * points,
+                                      const INT32 * count,
+                                      INT32 nbpolygons, INT32 mode )
 {
     RGNOBJ * obj;
     HRGN32 hrgn;
@@ -395,7 +413,7 @@
 	HeapFree( GetProcessHeap(), 0, xpoints );
 	return 0;
     }
-    obj = (RGNOBJ *) GDI_HEAP_LIN_ADDR( hrgn );
+    obj = (RGNOBJ *) GDI_HEAP_LOCK( hrgn );
     obj->xrgn = 0;
     dprintf_region(stddeb, "CreatePolyPolygonRgn: %d polygons, returning %04x\n",
                    nbpolygons, hrgn );
@@ -430,6 +448,7 @@
     }
 
     HeapFree( GetProcessHeap(), 0, xpoints );
+    GDI_HEAP_UNLOCK( hrgn );
     return hrgn;
 }
 
@@ -437,7 +456,7 @@
 /***********************************************************************
  *           PtInRegion16    (GDI.161)
  */
-BOOL16 PtInRegion16( HRGN16 hrgn, INT16 x, INT16 y )
+BOOL16 WINAPI PtInRegion16( HRGN16 hrgn, INT16 x, INT16 y )
 {
     return PtInRegion32( hrgn, x, y );
 }
@@ -446,50 +465,59 @@
 /***********************************************************************
  *           PtInRegion32    (GDI32.278)
  */
-BOOL32 PtInRegion32( HRGN32 hrgn, INT32 x, INT32 y )
+BOOL32 WINAPI PtInRegion32( HRGN32 hrgn, INT32 x, INT32 y )
 {
     RGNOBJ * obj;
+    BOOL32 result;
     
     if (!(obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC ))) return FALSE;
-    if (!obj->xrgn) return FALSE;
-    return XPointInRegion( obj->xrgn, x, y );
+    if (!obj->xrgn) result = FALSE;
+    else result = XPointInRegion( obj->xrgn, x, y );
+    GDI_HEAP_UNLOCK( hrgn );
+    return result;
 }
 
 
 /***********************************************************************
  *           RectInRegion16    (GDI.181)
  */
-BOOL16 RectInRegion16( HRGN16 hrgn, const RECT16 *rect )
+BOOL16 WINAPI RectInRegion16( HRGN16 hrgn, const RECT16 *rect )
 {
     RGNOBJ * obj;
-    
+    BOOL16 result;
+
     if (!(obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC ))) return FALSE;
-    if (!obj->xrgn) return FALSE;
-    return (XRectInRegion( obj->xrgn, rect->left, rect->top,
+    if (!obj->xrgn) result = FALSE;
+    else result = (XRectInRegion( obj->xrgn, rect->left, rect->top,
                            rect->right-rect->left,
                            rect->bottom-rect->top ) != RectangleOut);
+    GDI_HEAP_UNLOCK( hrgn );
+    return result;
 }
 
 
 /***********************************************************************
  *           RectInRegion32    (GDI32.281)
  */
-BOOL32 RectInRegion32( HRGN32 hrgn, const RECT32 *rect )
+BOOL32 WINAPI RectInRegion32( HRGN32 hrgn, const RECT32 *rect )
 {
     RGNOBJ * obj;
+    BOOL32 result;
     
     if (!(obj = (RGNOBJ *) GDI_GetObjPtr( hrgn, REGION_MAGIC ))) return FALSE;
-    if (!obj->xrgn) return FALSE;
-    return (XRectInRegion( obj->xrgn, rect->left, rect->top,
+    if (!obj->xrgn) result = FALSE;
+    else result = (XRectInRegion( obj->xrgn, rect->left, rect->top,
                            rect->right-rect->left,
                            rect->bottom-rect->top ) != RectangleOut);
+    GDI_HEAP_UNLOCK( hrgn );
+    return result;
 }
 
 
 /***********************************************************************
  *           EqualRgn16    (GDI.72)
  */
-BOOL16 EqualRgn16( HRGN16 rgn1, HRGN16 rgn2 )
+BOOL16 WINAPI EqualRgn16( HRGN16 rgn1, HRGN16 rgn2 )
 {
     return EqualRgn32( rgn1, rgn2 );
 }
@@ -498,13 +526,22 @@
 /***********************************************************************
  *           EqualRgn32    (GDI32.90)
  */
-BOOL32 EqualRgn32( HRGN32 rgn1, HRGN32 rgn2 )
+BOOL32 WINAPI EqualRgn32( HRGN32 rgn1, HRGN32 rgn2 )
 {
     RGNOBJ *obj1, *obj2;
+    BOOL32 result;
+
     if (!(obj1 = (RGNOBJ *) GDI_GetObjPtr( rgn1, REGION_MAGIC ))) return FALSE;
-    if (!(obj2 = (RGNOBJ *) GDI_GetObjPtr( rgn2, REGION_MAGIC ))) return FALSE;
-    if (!obj1->xrgn || !obj2->xrgn) return (!obj1->xrgn && !obj2->xrgn);
-    return XEqualRegion( obj1->xrgn, obj2->xrgn );
+    if (!(obj2 = (RGNOBJ *) GDI_GetObjPtr( rgn2, REGION_MAGIC ))) 
+    {
+      GDI_HEAP_UNLOCK( rgn1 );
+      return FALSE;
+    }
+    if (!obj1->xrgn || !obj2->xrgn) result = (!obj1->xrgn && !obj2->xrgn);
+    else result = XEqualRegion( obj1->xrgn, obj2->xrgn );
+    GDI_HEAP_UNLOCK( rgn1 );
+    GDI_HEAP_UNLOCK( rgn2 );
+    return result;
 }
 
 
@@ -558,6 +595,7 @@
     else
       ret = COMPLEXREGION;
     XUnionRectWithRegion( &rect, rgnObj->xrgn, rgnObj->xrgn );
+    GDI_HEAP_UNLOCK( hRgn );
   }
   return ret;
 }
@@ -575,13 +613,20 @@
 
     destObj = (RGNOBJ*) GDI_GetObjPtr( hDest, REGION_MAGIC );
     srcObj  = (RGNOBJ*) GDI_GetObjPtr( hSrc, REGION_MAGIC );
-    if (!srcObj->xrgn) return FALSE;
+    if (!srcObj->xrgn) 
+    {
+      GDI_HEAP_UNLOCK( hDest );
+      GDI_HEAP_UNLOCK( hSrc );
+      return FALSE;
+    }
     REGION_CopyRegion( srcObj, destObj );
     XShrinkRegion( destObj->xrgn, -x, -y );
     result = XCreateRegion();
     XSubtractRegion( destObj->xrgn, srcObj->xrgn, result );
     XDestroyRegion( destObj->xrgn );
     destObj->xrgn = result;
+    GDI_HEAP_UNLOCK( hDest );
+    GDI_HEAP_UNLOCK( hSrc );
     return TRUE;
 }
 
@@ -589,7 +634,7 @@
 /***********************************************************************
  *           CombineRgn16    (GDI.451)
  */
-INT16 CombineRgn16( HRGN16 hDest, HRGN16 hSrc1, HRGN16 hSrc2, INT16 mode )
+INT16 WINAPI CombineRgn16(HRGN16 hDest, HRGN16 hSrc1, HRGN16 hSrc2, INT16 mode)
 {
     return (INT16)CombineRgn32( hDest, hSrc1, hSrc2, mode );
 }
@@ -600,10 +645,11 @@
  *
  * Note: The behavior is correct even if src and dest regions are the same.
  */
-INT32 CombineRgn32( HRGN32 hDest, HRGN32 hSrc1, HRGN32 hSrc2, INT32 mode )
+INT32 WINAPI CombineRgn32(HRGN32 hDest, HRGN32 hSrc1, HRGN32 hSrc2, INT32 mode)
 {
     RGNOBJ *destObj, *src1Obj, *src2Obj;
     Region destrgn;
+    INT32 result;
 
     dprintf_region(stddeb, "CombineRgn: %04x,%04x -> %04x mode=%x\n", 
 		   hSrc1, hSrc2, hDest, mode );
@@ -611,12 +657,24 @@
     if (!(destObj = (RGNOBJ *) GDI_GetObjPtr( hDest, REGION_MAGIC )))
 	return ERROR;
     if (!(src1Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc1, REGION_MAGIC )))
+    {
+        GDI_HEAP_UNLOCK( hDest );
 	return ERROR;
-    if (mode == RGN_COPY) return REGION_CopyRegion( src1Obj, destObj );
+    }
+    if (mode == RGN_COPY) 
+    {
+       result = REGION_CopyRegion( src1Obj, destObj );
+       GDI_HEAP_UNLOCK( hDest );
+       GDI_HEAP_UNLOCK( hSrc1 );
+       return result;
+    }
 
     if (!(src2Obj = (RGNOBJ *) GDI_GetObjPtr( hSrc2, REGION_MAGIC )))
-        return ERROR;
-
+    {
+       GDI_HEAP_UNLOCK( hDest );
+       GDI_HEAP_UNLOCK( hSrc1 );
+       return ERROR;
+    }
       /* Some optimizations for null regions */
 
     if (!src1Obj->xrgn || !src2Obj->xrgn)
@@ -625,26 +683,48 @@
         {
         case RGN_DIFF:
             if (src1Obj->xrgn)
-                return REGION_CopyRegion( src1Obj, destObj );
+	    {
+                result = REGION_CopyRegion( src1Obj, destObj );
+		GDI_HEAP_UNLOCK( hDest );
+		GDI_HEAP_UNLOCK( hSrc1 );
+		GDI_HEAP_UNLOCK( hSrc2 );
+		return result;
+	    }
             /* else fall through */
         case RGN_AND:
             if (destObj->xrgn) XDestroyRegion( destObj->xrgn );
 	    destObj->xrgn = 0;
+	    GDI_HEAP_UNLOCK( hDest );
+	    GDI_HEAP_UNLOCK( hSrc1 );
+	    GDI_HEAP_UNLOCK( hSrc2 );
 	    return NULLREGION;
         case RGN_OR:
         case RGN_XOR:
             if (src1Obj->xrgn)
-                return REGION_CopyRegion( src1Obj, destObj );
+                result = REGION_CopyRegion( src1Obj, destObj );
             else
-                return REGION_CopyRegion( src2Obj, destObj );
+                result = REGION_CopyRegion( src2Obj, destObj );
+	    GDI_HEAP_UNLOCK( hDest );
+	    GDI_HEAP_UNLOCK( hSrc1 );
+	    GDI_HEAP_UNLOCK( hSrc2 );
+	    return result;
 	default:
+	    GDI_HEAP_UNLOCK( hDest );
+	    GDI_HEAP_UNLOCK( hSrc1 );
+	    GDI_HEAP_UNLOCK( hSrc2 );
 	    return ERROR;
         }
     }
 
       /* Perform the operation with the two X regions */
 
-    if (!(destrgn = XCreateRegion())) return ERROR;
+    if (!(destrgn = XCreateRegion()))
+    {
+      GDI_HEAP_UNLOCK( hDest );
+      GDI_HEAP_UNLOCK( hSrc1 );
+      GDI_HEAP_UNLOCK( hSrc2 );
+      return ERROR;
+    }
     switch(mode)
     {
     case RGN_AND:
@@ -661,6 +741,9 @@
         break;
     default:
         XDestroyRegion( destrgn );
+	GDI_HEAP_UNLOCK( hDest );
+	GDI_HEAP_UNLOCK( hSrc1 );
+	GDI_HEAP_UNLOCK( hSrc2 );
         return ERROR;
     }
     if (destObj->xrgn) XDestroyRegion( destObj->xrgn );
@@ -669,7 +752,13 @@
     {
         XDestroyRegion( destObj->xrgn );
         destObj->xrgn = 0;
+	GDI_HEAP_UNLOCK( hDest );
+	GDI_HEAP_UNLOCK( hSrc1 );
+	GDI_HEAP_UNLOCK( hSrc2 );
         return NULLREGION;
     }
+    GDI_HEAP_UNLOCK( hDest );
+    GDI_HEAP_UNLOCK( hSrc1 );
+    GDI_HEAP_UNLOCK( hSrc2 );
     return COMPLEXREGION;
 }
diff --git a/objects/text.c b/objects/text.c
index 176ddd6..1b46830 100644
--- a/objects/text.c
+++ b/objects/text.c
@@ -182,8 +182,8 @@
 /***********************************************************************
  *           DrawText16    (USER.85)
  */
-INT16 DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
-                  LPRECT16 rect, UINT16 flags )
+INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
+                         LPRECT16 rect, UINT16 flags )
 {
     SIZE16 size;
     const char *strPtr;
@@ -285,8 +285,8 @@
 /***********************************************************************
  *           DrawText32A    (USER32.163)
  */
-INT32 DrawText32A( HDC32 hdc, LPCSTR str, INT32 count,
-                   LPRECT32 rect, UINT32 flags )
+INT32 WINAPI DrawText32A( HDC32 hdc, LPCSTR str, INT32 count,
+                          LPRECT32 rect, UINT32 flags )
 {
     RECT16 rect16;
     INT16 ret;
@@ -303,8 +303,8 @@
 /***********************************************************************
  *           DrawText32W    (USER32.166)
  */
-INT32 DrawText32W( HDC32 hdc, LPCWSTR str, INT32 count,
-                   LPRECT32 rect, UINT32 flags )
+INT32 WINAPI DrawText32W( HDC32 hdc, LPCWSTR str, INT32 count,
+                          LPRECT32 rect, UINT32 flags )
 {
     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
     INT32 ret = DrawText32A( hdc, p, count, rect, flags );
@@ -316,9 +316,9 @@
 /***********************************************************************
  *           ExtTextOut16    (GDI.351)
  */
-BOOL16 ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
-                     const RECT16 *lprect, LPCSTR str, UINT16 count,
-                     const INT16 *lpDx )
+BOOL16 WINAPI ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
+                            const RECT16 *lprect, LPCSTR str, UINT16 count,
+                            const INT16 *lpDx )
 {
     BOOL32	ret;
     int		i;
@@ -338,9 +338,9 @@
 /***********************************************************************
  *           ExtTextOut32A    (GDI32.98)
  */
-BOOL32 ExtTextOut32A( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
-                      const RECT32 *lprect, LPCSTR str, UINT32 count,
-                      const INT32 *lpDx )
+BOOL32 WINAPI ExtTextOut32A( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
+                             const RECT32 *lprect, LPCSTR str, UINT32 count,
+                             const INT32 *lpDx )
 {
     DC * dc = DC_GetDCPtr( hdc );
     return dc && dc->funcs->pExtTextOut && 
@@ -351,9 +351,9 @@
 /***********************************************************************
  *           ExtTextOut32W    (GDI32.99)
  */
-BOOL32 ExtTextOut32W( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
-                      const RECT32 *lprect, LPCWSTR str, UINT32 count,
-                      const INT32 *lpDx )
+BOOL32 WINAPI ExtTextOut32W( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
+                             const RECT32 *lprect, LPCWSTR str, UINT32 count,
+                             const INT32 *lpDx )
 {
     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
     INT32 ret = ExtTextOut32A( hdc, x, y, flags, lprect, p, count, lpDx );
@@ -365,7 +365,7 @@
 /***********************************************************************
  *           TextOut16    (GDI.33)
  */
-BOOL16 TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
+BOOL16 WINAPI TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
 {
     return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
 }
@@ -374,7 +374,7 @@
 /***********************************************************************
  *           TextOut32A    (GDI32.355)
  */
-BOOL32 TextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR str, INT32 count )
+BOOL32 WINAPI TextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR str, INT32 count )
 {
     return ExtTextOut32A( hdc, x, y, 0, NULL, str, count, NULL );
 }
@@ -383,7 +383,7 @@
 /***********************************************************************
  *           TextOut32W    (GDI32.356)
  */
-BOOL32 TextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count )
+BOOL32 WINAPI TextOut32W(HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count)
 {
     return ExtTextOut32W( hdc, x, y, 0, NULL, str, count, NULL );
 }
@@ -392,9 +392,9 @@
 /***********************************************************************
  *           GrayString16   (USER.185)
  */
-BOOL16 GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
-                     LPARAM lParam, INT16 cch, INT16 x, INT16 y,
-                     INT16 cx, INT16 cy )
+BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
+                            LPARAM lParam, INT16 cch, INT16 x, INT16 y,
+                            INT16 cx, INT16 cy )
 {
     BOOL16 ret;
     COLORREF current_color;
@@ -411,9 +411,9 @@
 /***********************************************************************
  *           GrayString32A   (USER32.314)
  */
-BOOL32 GrayString32A( HDC32 hdc, HBRUSH32 hbr, GRAYSTRINGPROC32 gsprc,
-                      LPARAM lParam, INT32 cch, INT32 x, INT32 y,
-                      INT32 cx, INT32 cy )
+BOOL32 WINAPI GrayString32A( HDC32 hdc, HBRUSH32 hbr, GRAYSTRINGPROC32 gsprc,
+                             LPARAM lParam, INT32 cch, INT32 x, INT32 y,
+                             INT32 cx, INT32 cy )
 {
     BOOL32 ret;
     COLORREF current_color;
@@ -430,9 +430,9 @@
 /***********************************************************************
  *           GrayString32W   (USER32.315)
  */
-BOOL32 GrayString32W( HDC32 hdc, HBRUSH32 hbr, GRAYSTRINGPROC32 gsprc,
-                      LPARAM lParam, INT32 cch, INT32 x, INT32 y,
-                      INT32 cx, INT32 cy )
+BOOL32 WINAPI GrayString32W( HDC32 hdc, HBRUSH32 hbr, GRAYSTRINGPROC32 gsprc,
+                             LPARAM lParam, INT32 cch, INT32 x, INT32 y,
+                             INT32 cx, INT32 cy )
 {
     BOOL32 ret;
     COLORREF current_color;
@@ -523,8 +523,9 @@
 /***********************************************************************
  *           TabbedTextOut16    (USER.196)
  */
-LONG TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr, INT16 count, 
-                      INT16 cTabStops, const INT16 *lpTabPos, INT16 nTabOrg )
+LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
+                             INT16 count, INT16 cTabStops,
+                             const INT16 *lpTabPos, INT16 nTabOrg )
 {
     dprintf_text( stddeb, "TabbedTextOut16: %04x %d,%d '%.*s' %d\n",
                   hdc, x, y, count, lpstr, count );
@@ -536,8 +537,9 @@
 /***********************************************************************
  *           TabbedTextOut32A    (USER32.541)
  */
-LONG TabbedTextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR lpstr, INT32 count, 
-                       INT32 cTabStops, const INT32 *lpTabPos, INT32 nTabOrg )
+LONG WINAPI TabbedTextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR lpstr,
+                              INT32 count, INT32 cTabStops,
+                              const INT32 *lpTabPos, INT32 nTabOrg )
 {
     dprintf_text( stddeb, "TabbedTextOut32A: %04x %d,%d '%.*s' %d\n",
                   hdc, x, y, count, lpstr, count );
@@ -549,8 +551,9 @@
 /***********************************************************************
  *           TabbedTextOut32W    (USER32.542)
  */
-LONG TabbedTextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count, 
-                       INT32 cTabStops, const INT32 *lpTabPos, INT32 nTabOrg )
+LONG WINAPI TabbedTextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str,
+                              INT32 count, INT32 cTabStops,
+                              const INT32 *lpTabPos, INT32 nTabOrg )
 {
     LONG ret;
     LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
@@ -565,8 +568,8 @@
 /***********************************************************************
  *           GetTabbedTextExtent16    (USER.197)
  */
-DWORD GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count, 
-                             INT16 cTabStops, const INT16 *lpTabPos )
+DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count, 
+                                    INT16 cTabStops, const INT16 *lpTabPos )
 {
     dprintf_text( stddeb, "GetTabbedTextExtent: %04x '%.*s' %d\n",
                   hdc, count, lpstr, count );
@@ -578,8 +581,8 @@
 /***********************************************************************
  *           GetTabbedTextExtent32A    (USER32.292)
  */
-DWORD GetTabbedTextExtent32A( HDC32 hdc, LPCSTR lpstr, INT32 count, 
-                              INT32 cTabStops, const INT32 *lpTabPos )
+DWORD WINAPI GetTabbedTextExtent32A( HDC32 hdc, LPCSTR lpstr, INT32 count, 
+                                     INT32 cTabStops, const INT32 *lpTabPos )
 {
     dprintf_text( stddeb, "GetTabbedTextExtent: %04x '%.*s' %d\n",
                   hdc, count, lpstr, count );
@@ -591,8 +594,8 @@
 /***********************************************************************
  *           GetTabbedTextExtent32W    (USER32.293)
  */
-DWORD GetTabbedTextExtent32W( HDC32 hdc, LPCWSTR lpstr, INT32 count, 
-                              INT32 cTabStops, const INT32 *lpTabPos )
+DWORD WINAPI GetTabbedTextExtent32W( HDC32 hdc, LPCWSTR lpstr, INT32 count, 
+                                     INT32 cTabStops, const INT32 *lpTabPos )
 {
     LONG ret;
     LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
@@ -605,11 +608,13 @@
 /***********************************************************************
  *           GetTextCharset    (USER32.226) (USER.612)
  */
-INT32 GetTextCharset32(HDC32 hdc) {
+INT32 WINAPI GetTextCharset32(HDC32 hdc)
+{
     fprintf(stdnimp,"GetTextCharset(0x%x)\n",hdc);
     return DEFAULT_CHARSET; /* FIXME */
 }
 
-INT16 GetTextCharset16(HDC16 hdc) {
+INT16 WINAPI GetTextCharset16(HDC16 hdc)
+{
     return GetTextCharset32(hdc);
 }