Index: trunk/src/kernel32/mmap.cpp
===================================================================
--- trunk/src/kernel32/mmap.cpp	(revision 693)
+++ trunk/src/kernel32/mmap.cpp	(revision 695)
@@ -1,3 +1,3 @@
-/* $Id: mmap.cpp,v 1.11 1999-08-25 14:27:07 sandervl Exp $ */
+/* $Id: mmap.cpp,v 1.12 1999-08-25 15:27:19 sandervl Exp $ */
 
 /*
@@ -150,8 +150,8 @@
 
   dprintf(("Win32MemMap::commitPage %x (faultaddr %x), nr of pages %d", pageAddr, lpPageFaultAddr, nrpages));
-  if(VirtualAlloc((LPVOID)pageAddr, nrpages*PAGE_SIZE, MEM_COMMIT, newProt) == FALSE) {
-	goto fail;
-  }
   if(hMemFile != -1) {
+  	if(VirtualAlloc((LPVOID)pageAddr, nrpages*PAGE_SIZE, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
+		goto fail;
+  	}
 	offset = pageAddr - (ULONG)pMapping;
 	size   = nrpages*PAGE_SIZE;
@@ -171,4 +171,17 @@
 		goto fail;
 	}
+	if(mProtFlags & PAGE_READONLY) {
+//DosSetMem returns flags with EXECUTE bit set, even though the initial allocation is without this bit set!
+//Also returns access denied when trying to set it back to READONLY
+  		VirtualProtect((LPVOID)pageAddr, nrpages*PAGE_SIZE, newProt, &oldProt);
+//  		if(VirtualProtect((LPVOID)pageAddr, nrpages*PAGE_SIZE, newProt, &oldProt) == FALSE) {
+//			goto fail;
+//		}
+	}
+  }
+  else {
+  	if(VirtualAlloc((LPVOID)pageAddr, nrpages*PAGE_SIZE, MEM_COMMIT, newProt) == FALSE) {
+		goto fail;
+  	}
   }
 
@@ -224,5 +237,5 @@
 
   if(fMapped == FALSE) {//if not mapped, reserve/commit entire view
-  	pMapping = VirtualAlloc(0, mSize, fAlloc, mProtFlags);
+  	pMapping = VirtualAlloc(0, mSize, fAlloc, memFlags);
   	if(pMapping == NULL) {
 		dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
@@ -255,4 +268,5 @@
 
 //  mapMutex.enter();
+  dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
   if(fMapped == FALSE)
 	goto parmfail;
Index: trunk/src/kernel32/winimage.cpp
===================================================================
--- trunk/src/kernel32/winimage.cpp	(revision 693)
+++ trunk/src/kernel32/winimage.cpp	(revision 695)
@@ -1,3 +1,3 @@
-/* $Id: winimage.cpp,v 1.15 1999-08-23 17:02:35 sandervl Exp $ */
+/* $Id: winimage.cpp,v 1.16 1999-08-25 15:27:20 sandervl Exp $ */
 
 /*
@@ -36,4 +36,5 @@
 #include "os2util.h"
 #include "initterm.h"
+#include <win\virtual.h>
 
 char szErrorTitle[]     = "Win32 for OS/2";
@@ -192,8 +193,5 @@
 BOOL Win32Image::init(ULONG reservedMem)
 {
- HFILE  win32handle;
- ULONG  ulAction       = 0;      /* Action taken by DosOpen */
- ULONG  ulLocal        = 0;      /* File pointer position after DosSetFilePtr */
- APIRET rc             = NO_ERROR;            /* Return code */
+ HANDLE fImgMapping = 0;
  char   szErrorMsg[64];
  LPVOID win32file     = NULL;
@@ -203,43 +201,10 @@
  int    nSections, i;
 
-  rc = DosOpen(szFileName,                     /* File path name */
-           &win32handle,                   /* File handle */
-               &ulAction,                      /* Action taken */
-               0L,                             /* File primary allocation */
-               0L,                     /* File attribute */
-               OPEN_ACTION_FAIL_IF_NEW |
-               OPEN_ACTION_OPEN_IF_EXISTS,     /* Open function type */
-               OPEN_FLAGS_NOINHERIT |
-               OPEN_SHARE_DENYNONE  |
-               OPEN_ACCESS_READONLY,           /* Open mode of the file */
-               0L);                            /* No extended attribute */
-
-  if (rc != NO_ERROR) {
+  fImgMapping = VIRTUAL_MapFileA(szFileName, &win32file);
+
+  if (fImgMapping == -1) {
     	sprintf(szErrorMsg, "Unable to open %32s\n", szFileName);
         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-    	errorState = ERROR_INTERNAL;
-        return(FALSE);
-  }
-
-  /* Move the file pointer back to the beginning of the file */
-  DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
-  DosSetFilePtr(win32handle, 0L, FILE_END, &filesize);
-  DosSetFilePtr(win32handle, 0L, FILE_BEGIN, &ulLocal);
-
-  win32file = malloc(filesize);
-  if(win32file == NULL) {
-    	fout << "Error allocating memory" << endl;
-        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-    	DosClose(win32handle);                /* Close the file */
-    	errorState = ERROR_INTERNAL;
-    	return(FALSE);
-  }
-  rc = DosRead(win32handle, win32file, filesize, &ulRead);
-  if(rc != NO_ERROR) {
-    	fout << "DosRead returned " << rc << endl;
-        WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szFileErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-    	DosClose(win32handle);                /* Close the file */
-    	errorState = ERROR_INTERNAL;
-    	return(FALSE);
+    	goto failure;
   }
 
@@ -247,7 +212,5 @@
     	fout << "Not a valid PE file (probably a 16 bits windows exe/dll)!" << endl;
         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-    	DosClose(win32handle);                /* Close the file */
-    	errorState = ERROR_INTERNAL;
-    	return(FALSE);
+    	goto failure;
   }
 
@@ -255,14 +218,10 @@
     	fout << "Not a valid PE file!" << endl;
         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-    	DosClose(win32handle);                /* Close the file */
-    	errorState = ERROR_INTERNAL;
-    	return(FALSE);
+    	goto failure;
   }
   if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
     	fout << "You need a REAL CPU to run this code" << endl;
         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szCPUErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-    	DosClose(win32handle);                /* Close the file */
-    	errorState = ERROR_INTERNAL;
-    	return(FALSE);
+    	goto failure;
   }
   //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
@@ -270,7 +229,5 @@
     	fout << "Can't convert system files" << endl;
         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szExeErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-    	DosClose(win32handle);                /* Close the file */
-    	errorState = ERROR_INTERNAL;
-    	return(FALSE);
+    	goto failure;
   }
 
@@ -405,7 +362,6 @@
         }
         fout << "Unknown section" << endl;
-        errorState = ERROR_INTERNAL;
-        return(FALSE);
-        }
+    	goto failure;
+     }
   }
   fout << "*************************PE SECTIONS END **************************" << endl;
@@ -422,10 +378,10 @@
   if(allocSections(reservedMem) == FALSE) {
     	fout << "Failed to allocate image memory, rc " << errorState << endl;
-    	return(FALSE);
+    	goto failure;
   }
   fout << "OS/2 base address " << realBaseAddress << endl;
   if(storeSections((char *)win32file) == FALSE) {
     	fout << "Failed to store sections, rc " << errorState << endl;
-    	return(FALSE);
+    	goto failure;
   }
   if(oh.AddressOfEntryPoint) {
@@ -442,5 +398,5 @@
 	if(sect == NULL) {
 		fout << "Couldn't find TLS section!!" << endl;
-		return(FALSE);
+	    	goto failure;
 	}
   	setTLSAddress((char *)(sect->realvirtaddr + (tlsDir->StartAddressOfRawData - sect->virtaddr)));
@@ -451,5 +407,5 @@
 	if(sect == NULL) {
 		fout << "Couldn't find TLS AddressOfIndex section!!" << endl;
-		return(FALSE);
+	    	goto failure;
 	}
   	setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
@@ -458,5 +414,5 @@
 	if(sect == NULL) {
 		fout << "Couldn't find TLS AddressOfCallBacks section!!" << endl;
-		return(FALSE);
+	    	goto failure;
 	}
   	setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
@@ -466,5 +422,5 @@
   	if(setFixups((PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC)) == FALSE) {
     		fout << "Failed to set fixups" << endl;
-    		return(FALSE);
+	    	goto failure;
 	}
   }
@@ -472,5 +428,5 @@
   if(processImports((char *)win32file) == FALSE) {
     	fout << "Failed to process imports!" << endl;
-    	return(FALSE);
+    	goto failure;
   }
 
@@ -478,5 +434,5 @@
     	if(processExports((char *)win32file) == FALSE) {
         	fout << "Failed to process exported apis" << endl;
-        	return(FALSE);
+	    	goto failure;
     	}
   }
@@ -494,11 +450,13 @@
   if(setMemFlags() == FALSE) {
     	fout << "Failed to set memory protection" << endl;
-    	return(FALSE);
-  }
-
-  //Now it's safe to free win32file
-  free(win32file);
-  DosClose(win32handle);                /* Close the file */
+    	goto failure;
+  }
+
+  CloseHandle(fImgMapping);
   return(TRUE);
+failure:
+  if(fImgMapping) CloseHandle(fImgMapping);
+  errorState = ERROR_INTERNAL;
+  return FALSE;
 }
 //******************************************************************************
