Release 970928

Sat Sep 27 12:36:56 1997  Alexandre Julliard  <julliard@lrc.epfl.ch>

	* [if1632/relay.c]
	Made Catch and Throw also save %si and %di (untested).

	* [memory/selector.c]
	Added check for %fs and %gs in SELECTOR_FreeBlock.

	* [rc/winerc.c]
	Generated files no longer depend on Wine includes.
	Made .h generation optional.

	* [tools/build.c] [loader/task.c]
	Added CALL32_Init function.
	Added possibility to pass arguments when using CALLTO16_regs_.
	32-bit stack pointer is now saved on the 16-bit stack, instead of
	using IF1632_Saved32_esp.
	Removed CallTo32 callbacks.

	* [tools/makedep.c] [*/Makefile.in]
	Added support for directly generating dependencies for .y, .l and
	.rc files. Modified the makefiles to use this feature.

	* [windows/winproc.c] [if1632/thunk.c]
	Use CALLTO16_regs to call window procedures.

Thu Sep 25 12:18:57 1997  Kristian Nielsen <kristian.nielsen@risoe.dk>

	* [if1632/kernel.spec]
	Changed entry for SwitchStackBack to remove arguments from stack
	upon return (arguments left over from previous SwitchStackTo()).
	Borland C++ 4.0 now compiles "Hello World" (but crashes after
	outputting the .exe).

Wed Sep 24 13:54:44 1997  Marcus Meissner <msmeissn@cip.informatik.uni-erlangen.de>

	* [files/directory.c]
	SearchPath might get NULL buffer (empty LRU list in wordpad).

	* [memory/selector.c]
	Added SUnMapLS*.

	* [loader/pe_image.c]
	Be able to run executeables from non mmap()ble filesystems.
	PE_LoadLibrary adds librarys loaded by another process to
	its own modref list too.

	* [windows/keyboard.c][include/accel.h][loader/resource.c]
	Fixed accelerator leakage, use SDK defines/names.

	* [graphics/env.c][misc/main.c]
	Set/GetEnvironemnt have nothing to do with environment vars,
	but with Printer Environment.

	* [graphics/escape.c]
	Escape32: map args back to segmented pointers.

	* [windows/win.c]
	WS_POPUP|WS_CHILD windows don't need a parent window (SDK).

Tue Sep 16 14:40:16 1997  Robert Wilhelm  <robert@physiol.med.tu-muenchen.de>

	* [if1632/crtdll.spec] [misc/crtdll.c]
	Added signal().
diff --git a/loader/main.c b/loader/main.c
index b3e4f8b..944ffb7 100644
--- a/loader/main.c
+++ b/loader/main.c
@@ -23,6 +23,7 @@
 #include "queue.h"
 #include "syscolor.h"
 #include "sysmetrics.h"
+#include "callback.h"
 #include "file.h"
 #include "gdi.h"
 #include "heap.h"
@@ -180,10 +181,11 @@
 int main(int argc, char *argv[] )
 {
     extern BOOL32 MAIN_WineInit( int *argc, char *argv[] );
+    extern void *CALL32_Init(void);
     extern char * DEBUG_argv0;
 
     int i,loaded;
-    HINSTANCE16 handle;
+    HINSTANCE32 handle;
 
     __winelib = 0;  /* First of all, clear the Winelib flag */
 
@@ -196,6 +198,10 @@
     if (!MAIN_WineInit( &argc, argv )) return 1;
     if (!MAIN_Init()) return 1;
 
+    /* Initialize CALL32 routines */
+    /* This needs to be done just before task-switching starts */
+    IF1632_CallLargeStack = (int (*)(int (*func)(), void *arg))CALL32_Init();
+
     loaded=0;
     for (i = 1; i < argc; i++)
     {
diff --git a/loader/module.c b/loader/module.c
index c40d240..d1cc2b5 100644
--- a/loader/module.c
+++ b/loader/module.c
@@ -639,7 +639,9 @@
                   ne_header.rname_tab_offset - ne_header.resource_tab_offset,
                   pData )) return (HMODULE32)11;  /* invalid exe */
         pData += ne_header.rname_tab_offset - ne_header.resource_tab_offset;
+#ifndef WINELIB
 	NE_InitResourceHandler( hModule );
+#endif
     }
     else pModule->res_table = 0;  /* No resource table */
 
@@ -1219,7 +1221,6 @@
 	if (pModule->flags & NE_FFLAGS_SELFLOAD)
 	{
                 HFILE32 hf;
-                HGLOBAL16 hInitialStack32 = 0;
 		/* Handle self loading modules */
 		SEGTABLEENTRY * pSegTable = (SEGTABLEENTRY *) NE_SEG_TABLE(pModule);
 		SELFLOADHEADER *selfloadheader;
@@ -1259,44 +1260,14 @@
                 stack16Top->ip = 0;
                 stack16Top->cs = 0;
 
-		if (!IF1632_Saved32_esp)
-                {
-		  STACK32FRAME* frame32;
-		  char *stack32Top;
-		  /* Setup an initial 32 bit stack frame */
-		  hInitialStack32 = GLOBAL_Alloc( GMEM_FIXED, 0x10000,
-						  hModule, FALSE, FALSE, 
-						  FALSE );
-
-		  /* Create the 32-bit stack frame */
-		  
-		  stack32Top = (char*)GlobalLock16(hInitialStack32) + 
-		    0x10000;
-		  frame32 = (STACK32FRAME *)stack32Top - 1;
-		  frame32->saved_esp = (DWORD)stack32Top;
-		  frame32->edi = 0;
-		  frame32->esi = 0;
-		  frame32->edx = 0;
-		  frame32->ecx = 0;
-		  frame32->ebx = 0;
-		  frame32->ebp = 0;
-		  frame32->restore_addr = 0;
-		  frame32->retaddr      = 0;
-		  frame32->codeselector = WINE_CODE_SELECTOR;
-		  /* pTask->esp = (DWORD)frame32; */
-		}
                 hf = FILE_DupUnixHandle( MODULE_OpenFile( hModule ) );
 		CallTo16_word_ww( selfloadheader->BootApp, hModule, hf );
                 _lclose32(hf);
 		/* some BootApp procs overwrite the selector of dgroup */
 		pSegTable[pModule->dgroup - 1].selector = saved_dgroup;
 		IF1632_Saved16_ss_sp = oldstack;
-		for (i = 2; i <= pModule->seg_count; i++) NE_LoadSegment( hModule, i );
-		if (hInitialStack32)
-                {
-		  GlobalFree16(hInitialStack32);
-		  hInitialStack32 = 0;
-		}
+		for (i = 2; i <= pModule->seg_count; i++)
+                    NE_LoadSegment( hModule, i );
 	} 
 	else
         {
@@ -1638,7 +1609,7 @@
         return 2;  /* File not found */
     if (!(cmdShowHandle = GlobalAlloc16( 0, 2 * sizeof(WORD) )))
         return 8;  /* Out of memory */
-    if (!(cmdLineHandle = GlobalAlloc16( 0, 1024 )))
+    if (!(cmdLineHandle = GlobalAlloc16( 0, 256 )))
     {
         GlobalFree16( cmdShowHandle );
         return 8;  /* Out of memory */
@@ -1683,11 +1654,11 @@
 	}
 
 	if (*p)
-	    lstrcpyn32A( cmdline + 1, p + 1, 1023 );
+	    lstrcpyn32A( cmdline + 1, p + 1, 255 );
 	else
 	    cmdline[1] = '\0';
 
-	cmdline[0] = strlen( cmdline + 1 ) + 1;
+	cmdline[0] = strlen( cmdline + 1 );
 	*p = '\0';
 
 	/* Now load the executable file */
diff --git a/loader/ne_image.c b/loader/ne_image.c
index e1631e9..48df65f 100644
--- a/loader/ne_image.c
+++ b/loader/ne_image.c
@@ -520,7 +520,7 @@
     dprintf_dll( stddeb, "Calling LibMain, cs:ip=%04lx:%04x ds=%04lx di=%04x cx=%04x\n", 
                  CS_reg(&context), IP_reg(&context), DS_reg(&context),
                  DI_reg(&context), CX_reg(&context) );
-    CallTo16_regs_( &context );
+    CallTo16_regs_( &context, 0 );
     return TRUE;
 }
 
diff --git a/loader/pe_image.c b/loader/pe_image.c
index 415094f..f53575b 100644
--- a/loader/pe_image.c
+++ b/loader/pe_image.c
@@ -1,4 +1,3 @@
-#ifndef WINELIB
 /* 
  *  Copyright	1994	Eric Youndale & Erik Bos
  *  Copyright	1995	Martin von Löwis
@@ -12,6 +11,7 @@
  */
 
 #include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -33,8 +33,10 @@
 #include "options.h"
 #include "stddebug.h"
 #include "debug.h"
-#include "debugger.h"
 #include "xmalloc.h"
+#ifndef WINELIB
+#include "debugger.h"
+#endif
 
 static void PE_InitDLL(PE_MODREF* modref, DWORD type, LPVOID lpReserved);
 
@@ -43,6 +45,7 @@
 
 void dump_exports(IMAGE_EXPORT_DIRECTORY * pe_exports, unsigned int load_addr)
 { 
+#ifndef WINELIB
   char		*Module;
   int		i;
   u_short	*ordinal;
@@ -81,6 +84,7 @@
       }
       DEBUG_AddSymbol(buffer,&daddr, NULL, SYM_WIN32 | SYM_FUNC);
   }
+#endif
 }
 
 /* Look up the specified function or ordinal in the exportlist:
@@ -428,11 +432,8 @@
 static PE_MODULE *PE_LoadImage( int fd )
 {
 	struct pe_data		*pe;
-	DBG_ADDR		daddr;
 	struct stat		stbuf;
 
-	daddr.seg=0;
-	daddr.type = NULL;
 	if (-1==fstat(fd,&stbuf)) {
 		perror("PE_LoadImage:fstat");
 		return NULL;
@@ -443,9 +444,33 @@
 	/* map the PE image somewhere */
 	pe->mappeddll = (HMODULE32)mmap(NULL,stbuf.st_size,PROT_READ,MAP_SHARED,fd,0);
 	if (!pe->mappeddll || pe->mappeddll==-1) {
-		perror("PE_LoadImage:mmap");
-		free(pe);
-		return NULL;
+		if (errno==ENOEXEC) {
+			int	res=0,curread = 0;
+
+			lseek(fd,0,SEEK_SET);
+			/* linux: some filesystems don't support mmap (samba,
+			 * ntfs apparently) so we have to read the image the
+			 * hard way
+			 */
+			pe->mappeddll = xmalloc(stbuf.st_size);
+			while (curread < stbuf.st_size) {
+				res = read(fd,pe->mappeddll+curread,stbuf.st_size-curread);
+				if (res<=0) 
+					break;
+				curread+=res;
+			}
+			if (res == -1) {
+				perror("PE_LoadImage:mmap compat read");
+				free(pe->mappeddll);
+				free(pe);
+				return NULL;
+			}
+
+		} else {
+			perror("PE_LoadImage:mmap");
+			free(pe);
+			return NULL;
+		}
 	}
 	/* link PE header */
 	pe->pe_header = (IMAGE_NT_HEADERS*)(pe->mappeddll+(((IMAGE_DOS_HEADER*)pe->mappeddll)->e_lfanew));
@@ -499,7 +524,6 @@
 	int			load_addr;
 	IMAGE_DATA_DIRECTORY	dir;
 	char			buffer[200];
-	DBG_ADDR		daddr;
 	char			*modname;
 	int			vma_size;
 	
@@ -604,6 +628,7 @@
 		pem->pe_reloc = (void *) RVA(dir.VirtualAddress);
 	}
 
+#ifndef WINELIB
 	if(pe->pe_header->OptionalHeader.DataDirectory
 		[IMAGE_DIRECTORY_ENTRY_DEBUG].Size)
 	  {
@@ -611,6 +636,7 @@
 			pe->pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress,
 			pe->pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size);
 	  }
+#endif
 
 	if(pe->pe_header->OptionalHeader.DataDirectory
 		[IMAGE_DIRECTORY_ENTRY_COPYRIGHT].Size)
@@ -657,19 +683,24 @@
 			*s='\0';
 	}
 
-	/* add start of sections as debugsymbols */
-	for(i=0;i<pe->pe_header->FileHeader.NumberOfSections;i++) {
+#ifndef WINELIB
+        {
+            DBG_ADDR daddr = { NULL, 0, 0 };
+            /* add start of sections as debugsymbols */
+            for(i=0;i<pe->pe_header->FileHeader.NumberOfSections;i++) {
 		sprintf(buffer,"%s_%s",modname,pe->pe_seg[i].Name);
 		daddr.off= RVA(pe->pe_seg[i].VirtualAddress);
 		DEBUG_AddSymbol(buffer,&daddr, NULL, SYM_WIN32 | SYM_FUNC);
-	}
-	/* add entry point */
-	sprintf(buffer,"%s_EntryPoint",modname);
-	daddr.off=RVA(pe->pe_header->OptionalHeader.AddressOfEntryPoint);
-	DEBUG_AddSymbol(buffer,&daddr, NULL, SYM_WIN32 | SYM_FUNC);
-	/* add start of DLL */
-	daddr.off=load_addr;
-	DEBUG_AddSymbol(modname,&daddr, NULL, SYM_WIN32 | SYM_FUNC);
+            }
+            /* add entry point */
+            sprintf(buffer,"%s_EntryPoint",modname);
+            daddr.off=RVA(pe->pe_header->OptionalHeader.AddressOfEntryPoint);
+            DEBUG_AddSymbol(buffer,&daddr, NULL, SYM_WIN32 | SYM_FUNC);
+            /* add start of DLL */
+            daddr.off=load_addr;
+            DEBUG_AddSymbol(modname,&daddr, NULL, SYM_WIN32 | SYM_FUNC);
+        }
+#endif
 }
 
 HINSTANCE16 MODULE_CreateInstance(HMODULE16 hModule,LOADPARAMS *params);
@@ -682,36 +713,47 @@
 	OFSTRUCT	ofs;
 	HMODULE32	hModule;
 	NE_MODULE	*pModule;
+	PE_MODREF	*pem;
 
-	if ((hModule = MODULE_FindModule( name )))
-		return hModule;
+	if ((hModule = MODULE_FindModule( name ))) {
+		/* the .DLL is either loaded or internal */
+		hModule = MODULE_HANDLEtoHMODULE32(hModule);
+		if (!HIWORD(hModule)) /* internal (or bad) */
+			return hModule;
+		/* check if this module is already mapped */
+		pem 	= ((PDB32*)GetCurrentProcessId())->modref_list;
+		while (pem) {
+			if (pem->pe_module->mappeddll == hModule)
+				return hModule;
+			pem = pem->next;
+		}
+		pModule = MODULE_GetPtr(hModule);
+	} else {
 
-	/* try to load builtin, enabled modules first */
-	if ((hModule = BUILTIN_LoadModule( name, FALSE )))
-		return hModule;
+		/* try to load builtin, enabled modules first */
+		if ((hModule = BUILTIN_LoadModule( name, FALSE )))
+			return hModule;
 
-	/* try to open the specified file */
-	if (HFILE_ERROR32==(hFile=OpenFile32(name,&ofs,OF_READ))) {
-		/* Now try the built-in even if disabled */
-		if ((hModule = BUILTIN_LoadModule( name, TRUE ))) {
-			fprintf( stderr, "Warning: could not load Windows DLL '%s', using built-in module.\n", name );
+		/* try to open the specified file */
+		if (HFILE_ERROR32==(hFile=OpenFile32(name,&ofs,OF_READ))) {
+			/* Now try the built-in even if disabled */
+			if ((hModule = BUILTIN_LoadModule( name, TRUE ))) {
+				fprintf( stderr, "Warning: could not load Windows DLL '%s', using built-in module.\n", name );
+				return hModule;
+			}
+			return 1;
+		}
+		if ((hModule = MODULE_CreateDummyModule( &ofs )) < 32) {
+			_lclose32(hFile);
 			return hModule;
 		}
-		return 1;
-	}
-	if ((hModule = MODULE_CreateDummyModule( &ofs )) < 32) {
+		pModule		= (NE_MODULE *)GlobalLock16( hModule );
+		pModule->flags	= NE_FFLAGS_WIN32;
+		pModule->pe_module = PE_LoadImage( FILE_GetUnixHandle(hFile) );
 		_lclose32(hFile);
-		return hModule;
+		if (!pModule->pe_module)
+			return 21;
 	}
-
-	pModule		= (NE_MODULE *)GlobalLock16( hModule );
-	pModule->flags	= NE_FFLAGS_WIN32;
-
-	/* FIXME: check if pe image loaded already ... */
-	pModule->pe_module = PE_LoadImage( FILE_GetUnixHandle(hFile) );
-	_lclose32(hFile);
-	if (!pModule->pe_module)
-		return 21;
 	/* recurse */
 	PE_MapImage(pModule->pe_module,(PDB32*)GetCurrentProcessId(),&ofs,flags);
 	return pModule->pe_module->mappeddll;
@@ -864,4 +906,3 @@
 	return TRUE;
 }
 
-#endif /* WINELIB */
diff --git a/loader/pe_resource.c b/loader/pe_resource.c
index 9b8ae7e..11ee003 100644
--- a/loader/pe_resource.c
+++ b/loader/pe_resource.c
@@ -1,4 +1,3 @@
-#ifndef WINELIB
 /*
  * PE (Portable Execute) File Resources
  *
@@ -149,48 +148,12 @@
 /**********************************************************************
  *	    PE_SizeofResource32
  */
-void
-_check_ptr(DWORD x,DWORD start,LPDWORD lastmax) {
-	if ((x>start) && (x<*lastmax))
-		*lastmax=x;
-}
-
-static void
-walk_resdir(DWORD loadaddr,DWORD rootresdir,DWORD xres,DWORD data,DWORD lvl,LPDWORD max){
-    LPIMAGE_RESOURCE_DIRECTORY		resdir;
-    LPIMAGE_RESOURCE_DATA_ENTRY		dataent;
-    LPIMAGE_RESOURCE_DIRECTORY_ENTRY	et;
-    int	i;
-
-    if (lvl==3) {
-    	dataent = (LPIMAGE_RESOURCE_DATA_ENTRY)(rootresdir+xres);
-	_check_ptr(loadaddr+dataent->OffsetToData,data,max);
-	return;
-    }
-    resdir = (LPIMAGE_RESOURCE_DIRECTORY)(rootresdir+xres);
-    et =(LPIMAGE_RESOURCE_DIRECTORY_ENTRY)((LPBYTE)resdir+sizeof(IMAGE_RESOURCE_DIRECTORY));
-    for (i=0;i<resdir->NumberOfNamedEntries+resdir->NumberOfIdEntries;i++)
-	walk_resdir(loadaddr,rootresdir,(lvl==2)?et[i].u2.OffsetToData:et[i].u2.s.OffsetToDirectory,data,lvl+1,max);
-}
-
 DWORD PE_SizeofResource32( HINSTANCE32 hModule, HANDLE32 hRsrc )
 {
-    PE_MODREF	*pem = HMODULE32toPE_MODREF(hModule);
-    DWORD	max,data;
-    IMAGE_DATA_DIRECTORY	dir;
-
-    if (!pem || !pem->pe_resource)
-    	return 0;
-    if (!hRsrc) return 0;
-
-    max=(DWORD)-1;
-    dir=pem->pe_module->pe_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
-    if(dir.Size)
-    	max=(DWORD)pem->pe_resource+dir.Size;
-
-    data=((DWORD)pem->load_addr+((LPIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->OffsetToData);
-    walk_resdir(pem->load_addr,(DWORD)pem->pe_resource,0,data,0,&max);
-    return max-data;
+    /* we don't need hModule */
+    if (!hRsrc)
+   	 return 0;
+    return ((LPIMAGE_RESOURCE_DATA_ENTRY)hRsrc)->Size;
 }
 
 /**********************************************************************
@@ -419,4 +382,3 @@
     }
     return ret;
 }
-#endif
diff --git a/loader/resource.c b/loader/resource.c
index 2654396..23ba6b6 100644
--- a/loader/resource.c
+++ b/loader/resource.c
@@ -416,18 +416,10 @@
 
 /**********************************************************************
  *			LoadAccelerators16	[USER.177]
- *
- * FIXME: this code leaks memory because HACCEL must be a result of LoadResource()
- *        (see TWIN for hints).
  */
 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, SEGPTR lpTableName)
 {
-    HACCEL16 	hAccel;
-    HGLOBAL16 	rsc_mem;
-    HRSRC16 hRsrc;
-    BYTE 	*lp;
-    ACCELHEADER	*lpAccelTbl;
-    int 	i, n;
+    HRSRC16	hRsrc;
 
     if (HIWORD(lpTableName))
         dprintf_accel( stddeb, "LoadAccelerators: %04x '%s'\n",
@@ -438,30 +430,7 @@
 
     if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATOR )))
       return 0;
-    if (!(rsc_mem = LoadResource16( instance, hRsrc ))) return 0;
-
-    lp = (BYTE *)LockResource16(rsc_mem);
-    n = SizeofResource16(instance,hRsrc)/sizeof(ACCELENTRY);
-    hAccel = GlobalAlloc16(GMEM_MOVEABLE, 
-    	sizeof(ACCELHEADER) + (n + 1)*sizeof(ACCELENTRY));
-    lpAccelTbl = (LPACCELHEADER)GlobalLock16(hAccel);
-    lpAccelTbl->wCount = 0;
-    for (i = 0; i < n; i++) {
-	lpAccelTbl->tbl[i].type = *(lp++);
-	lpAccelTbl->tbl[i].wEvent = *((WORD *)lp);
-	lp += 2;
-	lpAccelTbl->tbl[i].wIDval = *((WORD *)lp);
-	lp += 2;
-    	if (lpAccelTbl->tbl[i].wEvent == 0) break;
-	dprintf_accel(stddeb,
-		"Accelerator #%u / event=%04X id=%04X type=%02X \n", 
-		i, lpAccelTbl->tbl[i].wEvent, lpAccelTbl->tbl[i].wIDval, 
-		lpAccelTbl->tbl[i].type);
-	lpAccelTbl->wCount++;
- 	}
-    GlobalUnlock16(hAccel);
-    FreeResource16( rsc_mem );
-    return hAccel;
+    return LoadResource16(instance,hRsrc);
 }
 
 /**********************************************************************
@@ -475,12 +444,7 @@
  */
 HACCEL32 WINAPI LoadAccelerators32W(HINSTANCE32 instance,LPCWSTR lpTableName)
 {
-    HACCEL32 	hAccel;
-    HGLOBAL32 	rsc_mem;
     HRSRC32 hRsrc;
-    BYTE 	*lp;
-    ACCELHEADER	*lpAccelTbl;
-    int 	i, n;
 
     if (HIWORD(lpTableName))
         dprintf_accel( stddeb, "LoadAccelerators: %04x '%s'\n",
@@ -492,31 +456,7 @@
     if (!(hRsrc = FindResource32W( instance, lpTableName, 
 		(LPCWSTR)RT_ACCELERATOR )))
       return 0;
-    if (!(rsc_mem = LoadResource32( instance, hRsrc ))) return 0;
-
-    lp = (BYTE *)LockResource32(rsc_mem);
-    n = SizeofResource32(instance,hRsrc)/sizeof(ACCELENTRY);
-    hAccel = GlobalAlloc16(GMEM_MOVEABLE, 
-    	sizeof(ACCELHEADER) + (n + 1)*sizeof(ACCELENTRY));
-    lpAccelTbl = (LPACCELHEADER)GlobalLock16(hAccel);
-    lpAccelTbl->wCount = 0;
-    for (i = 0; i < n; i++) {
-	lpAccelTbl->tbl[i].type = *lp;
-	lp += 2;
-	lpAccelTbl->tbl[i].wEvent = *((WORD *)lp);
-	lp += 2;
-	lpAccelTbl->tbl[i].wIDval = *((WORD *)lp);
-	lp += 4;
-    	if (lpAccelTbl->tbl[i].wEvent == 0) break;
-	dprintf_accel(stddeb,
-		"Accelerator #%u / event=%04X id=%04X type=%02X \n", 
-		i, lpAccelTbl->tbl[i].wEvent, lpAccelTbl->tbl[i].wIDval, 
-		lpAccelTbl->tbl[i].type);
-	lpAccelTbl->wCount++;
- 	}
-    GlobalUnlock16(hAccel);
-    FreeResource32(rsc_mem);
-    return hAccel;
+    return LoadResource32( instance, hRsrc );
 }
 
 HACCEL32 WINAPI LoadAccelerators32A(HINSTANCE32 instance,LPCSTR lpTableName)
@@ -759,7 +699,7 @@
     return retval;
 }
 
-
+#ifndef WINELIB
 /**********************************************************************
  *	SetResourceHandler	(KERNEL.43)
  */
@@ -784,7 +724,6 @@
     return NULL;
 }
 
-#ifndef WINELIB
 /**********************************************************************
  *	EnumResourceTypesA	(KERNEL32.90)
  */
diff --git a/loader/task.c b/loader/task.c
index 3266976..0dcf6a6 100644
--- a/loader/task.c
+++ b/loader/task.c
@@ -47,12 +47,6 @@
   /* Saved 16-bit stack for current process (Win16 only) */
 DWORD IF1632_Saved16_ss_sp = 0;
 
-  /* Saved 32-bit stack for current process (Win16 only) */
-DWORD IF1632_Saved32_esp = 0;
-
-  /* Original Unix stack */
-DWORD IF1632_Original32_esp = 0;
-
   /* Pointer to function to switch to a larger stack */
 int (*IF1632_CallLargeStack)( int (*func)(), void *arg ) = NULL;
 
@@ -412,7 +406,7 @@
                       SELECTOROF(IF1632_Saved16_ss_sp),
                       OFFSETOF(IF1632_Saved16_ss_sp) );
 
-        CallTo16_regs_( &context );
+        CallTo16_regs_( &context, 0 );
         /* This should never return */
         fprintf( stderr, "TASK_CallToStart: Main program returned!\n" );
         TASK_KillCurrentTask( 1 );
@@ -436,7 +430,7 @@
     SEGTABLEENTRY *pSegTable;
     LPSTR name;
     char filename[256];
-    char *stack16Top, *stack32Top;
+    char *stack32Top;
     STACK16FRAME *frame16;
     STACK32FRAME *frame32;
 #ifndef WINELIB32
@@ -575,7 +569,6 @@
 
     stack32Top = (char*)pTask->thdb->teb.stack_top;
     frame32 = (STACK32FRAME *)stack32Top - 1;
-    frame32->saved_esp = (DWORD)stack32Top;
     frame32->edi = 0;
     frame32->esi = 0;
     frame32->edx = 0;
@@ -587,15 +580,14 @@
     frame32->retaddr = (DWORD)TASK_CallToStart;
     frame32->codeselector = WINE_CODE_SELECTOR;
 #endif
-    pTask->esp = (DWORD)frame32;
 
-      /* Create the 16-bit stack frame */
+    /* Create the 16-bit stack frame */
 
     pTask->ss_sp = PTR_SEG_OFF_TO_SEGPTR( hInstance,
                         ((pModule->sp != 0) ? pModule->sp :
                 pSegTable[pModule->ss-1].minsize + pModule->stack_size) & ~1 );
-    stack16Top = (char *)PTR_SEG_TO_LIN( pTask->ss_sp );
-    frame16 = (STACK16FRAME *)stack16Top - 1;
+    pTask->ss_sp -= sizeof(DWORD);  /* To store saved %%esp */
+    frame16 = (STACK16FRAME *)PTR_SEG_TO_LIN( pTask->ss_sp ) - 1;
     frame16->saved_ss_sp = 0;
     frame16->ebp = 0;
     frame16->ds = frame16->es = pTask->hInstance;
@@ -603,6 +595,8 @@
     frame16->entry_ip = OFFSETOF(TASK_RescheduleProc) + 14;
     frame16->entry_cs = SELECTOROF(TASK_RescheduleProc);
     frame16->bp = 0;
+    frame16->args[0] = LOWORD(frame32);
+    frame16->args[1] = HIWORD(frame32);
 #ifndef WINELIB
     frame16->ip = LOWORD( CALLTO16_RetAddr_word );
     frame16->cs = HIWORD( CALLTO16_RetAddr_word );
@@ -631,7 +625,8 @@
         }
         else
         {
-            DBG_ADDR addr = { NULL, pSegTable[pModule->cs-1].selector, pModule->ip };
+            DBG_ADDR addr = { NULL, pSegTable[pModule->cs-1].selector,
+                              pModule->ip };
             fprintf( stderr, "Win16 task '%s': ", name );
             DEBUG_AddBreakpoint( &addr );
         }
@@ -824,14 +819,9 @@
     dprintf_task( stddeb, "Switching to task %04x (%.8s)\n",
                   hTask, pNewTask->module_name );
 
-      /* Save the stacks of the previous task (if any) */
+      /* Save the stack of the previous task (if any) */
 
-    if (pOldTask)
-    {
-        pOldTask->ss_sp = IF1632_Saved16_ss_sp;
-        pOldTask->esp   = IF1632_Saved32_esp;
-    }
-    else IF1632_Original32_esp = IF1632_Saved32_esp;
+    if (pOldTask) pOldTask->ss_sp = IF1632_Saved16_ss_sp;
 
      /* Make the task the last in the linked list (round-robin scheduling) */
 
@@ -840,13 +830,12 @@
     TASK_LinkTask( hTask );
     pNewTask->priority--;
 
-      /* Switch to the new stack */
+    /* Switch to the new stack */
 
     hCurrentTask = hTask;
     pCurrentThread = pNewTask->thdb;
     pCurrentProcess = pCurrentThread->process;
     IF1632_Saved16_ss_sp = pNewTask->ss_sp;
-    IF1632_Saved32_esp   = pNewTask->esp;
 }
 
 
@@ -1051,13 +1040,13 @@
     TDB *pCurTask = (TDB *)GlobalLock16( hCurrentTask );
     MESSAGEQUEUE *queue = (MESSAGEQUEUE *)GlobalLock16( pCurTask->hQueue );
     /* Handle sent messages */
-    if (queue && (queue->wakeBits & QS_SENDMESSAGE))
+    while (queue && (queue->wakeBits & QS_SENDMESSAGE))
         QUEUE_ReceiveMessage( queue );
 
     OldYield();
 
     queue = (MESSAGEQUEUE *)GlobalLock16( pCurTask->hQueue );
-    if (queue && (queue->wakeBits & QS_SENDMESSAGE))
+    while (queue && (queue->wakeBits & QS_SENDMESSAGE))
         QUEUE_ReceiveMessage( queue );
 }
 
@@ -1191,7 +1180,7 @@
 
     /* Switch to the new stack */
 
-    IF1632_Saved16_ss_sp = pTask->ss_sp = PTR_SEG_OFF_TO_SEGPTR( seg,
+    IF1632_Saved16_ss_sp = PTR_SEG_OFF_TO_SEGPTR( seg,
                                                   ptr - sizeof(STACK16FRAME) );
     newFrame = CURRENT_STACK16;
 
@@ -1230,18 +1219,18 @@
 
     /* Switch back to the old stack */
 
-    IF1632_Saved16_ss_sp = pTask->ss_sp = pData->old_ss_sp;
+    IF1632_Saved16_ss_sp = pData->old_ss_sp;
     pData->old_ss_sp = 0;
 
     /* Build a stack frame for the return */
 
     newFrame = CURRENT_STACK16;
     newFrame->saved_ss_sp = oldFrame->saved_ss_sp;
-    newFrame->entry_ip    = oldFrame->entry_ip;
-    newFrame->entry_cs    = oldFrame->entry_cs;
-    newFrame->bp          = oldFrame->bp;
-    newFrame->ip          = oldFrame->ip;
-    newFrame->cs          = oldFrame->cs;
+    if (debugging_relay)
+    {
+        newFrame->entry_ip = oldFrame->entry_ip;
+        newFrame->entry_cs = oldFrame->entry_cs;
+    }
 }