Index: trunk/src/kernel32/winimagebase.cpp
===================================================================
--- trunk/src/kernel32/winimagebase.cpp	(revision 3402)
+++ trunk/src/kernel32/winimagebase.cpp	(revision 3404)
@@ -1,9 +1,9 @@
-/* $Id: winimagebase.cpp,v 1.17 2000-04-16 06:47:22 bird Exp $ */
+/* $Id: winimagebase.cpp,v 1.18 2000-04-16 10:42:12 sandervl Exp $ */
 
 /*
  * Win32 PE Image base class
  *
- * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
- * Copyright 1998-2000 Knut St. Osmundsen (knut.stange.osmundsen@pmsc.no)
+ * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
+ * Copyright 1998 Knut St. Osmundsen
  *
  * Project Odin Software License can be found in LICENSE.TXT
@@ -11,33 +11,33 @@
  */
 
-#define INCL_DOSFILEMGR                 /* File Manager values */
-#define INCL_DOSMODULEMGR               /* DOS Module manager */
-#define INCL_DOSERRORS                  /* DOS Error values */
-#define INCL_DOSPROCESS                 /* DOS Process values */
-#define INCL_DOSMISC                    /* DOS Miscellanous values */
-#define INCL_WIN                        /* All Win API */
-#define INCL_BASE                       /* All Dos API */
-#include <os2wrap.h>                    /* Odin32 OS/2 API wrappers. */
-
-#include <stdio.h>                      /* C Library Standard I/O */
-#include <string.h>                     /* C Library string operations */
-#include <stdlib.h>                     /* C Library Standard stuff */
-
-#include <misc.h>                       /* Odin32 Miscellaneous definitions, debug stuff*/
-#include <win32type.h>                  /* Odin32 Common types and definition. */
-#include "winimagebase.h"               /* Odin32 Executable Image Base Class */
-#include "windllbase.h"                 /* Odin32 Dll Base Class */
-#include "winexebase.h"                 /* Odin32 Exe Base Class */
-#include <pefile.h>                     /* Odin32 PE definitions */
-#include <unicode.h>                    /* Odin32 Unicode conversion */
-#include <winres.h>                     /* Odin32 Resource Class */
+#define INCL_DOSFILEMGR          /* File Manager values      */
+#define INCL_DOSMODULEMGR
+#define INCL_DOSERRORS           /* DOS Error values         */
+#define INCL_DOSPROCESS          /* DOS Process values       */
+#define INCL_DOSMISC             /* DOS Miscellanous values  */
+#define INCL_WIN
+#define INCL_BASE
+#include <os2wrap.h>             //Odin32 OS/2 api wrappers
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <assert.h>
+#include <misc.h>
+#include <win32type.h>
+#include <winimagebase.h>
+#include <windllbase.h>
+#include <winexebase.h>
+#include <pefile.h>
+#include <unicode.h>
+#include <winres.h>
 #include "oslibmisc.h"
 #include "oslibdos.h"
 #include "initterm.h"
+#include "directory.h"
 #include <win\virtual.h>
-#include "directory.h"                  /* InternalGet<Windows/System>Directory. */
-#include <os2newapi.h>                  /* DosQueryHeaderInfo. */
-
-#define DBG_LOCALLOG    DBG_winimagebase
+
+#define DBG_LOCALLOG	DBG_winimagebase
 #include "dbglocal.h"
 
@@ -115,9 +115,9 @@
   item = loadedDlls.Head();
   while(item) {
-    if(loadedDlls.getItem(item) == (ULONG)image) {
-        ret = TRUE;
-        break;
-    }
-    item = loadedDlls.getNext(item);
+	if(loadedDlls.getItem(item) == (ULONG)image) {
+		ret = TRUE;
+		break;
+	}	  
+	item = loadedDlls.getNext(item);
   }
   dlllistmutex.leave();
@@ -132,338 +132,61 @@
   return 0x40000; //NT 4
 }
-
-
-/**
- * Finds a executable module (or really any file) using the DLL search order.
- * The search order used is:
- *      1. The directory from which the application loaded.
- *      2. The current directory.
- *      3. System directory. (GetSystemDirectory returns its path)
- *         (Windows NT/2k directory name: SYSTEM32)
- *      4. (Windows NT/2k: The 16-bit Windows system directory. There
- *         is no function that obtains the path of this directory.
- *         (Directory name: SYSTEM)
- *         THIS IS NOT SEARCHED BY ODIN.)
- *      5. The Windows directory. (GetWindowsDirectory returns its path)
- *      6. The Directories listed in the PATH environment variable.
- *      7. The Directories listed in the BEGINLIBPATH.
- *      8. The Directories listed in the LIBPATH.
- *      9. The Directories listed in the ENDLIBPATH.
- *
- * @returns   Success indicator. TRUE: found  FALSE: not found.
- *            Note! pszFullname will normally contain the contents of
- *            pszFilename upon return, there is one case FALSE case when
- *            pszFullname will be empty upon return. That's when the buffer
- *            isn't large enough to hold the content of pszFilename including
- *            an extention.
- *            So, please check for the return value!
- * @param     pszFilename   File to find. This name should not have a path!
- *                          If it don't contains an '.', ".DLL" is appended to
- *                          the name.
- * @param     pszFullname   Pointer to output buffer, this will hold the
- *                          a fully qualified, uppercased, filename upon
- *                          successful return.
- * @param     cchFullname   Size of the buffer pointer to by pszFullname.
- *                          (A length of at least CCHMAXPATH is recommended.)
- * @parm      pszAltPath    Pointer to alternate first executable path. This
- *                          will include a filename or at least a backslash at
- *                          the end.
- *                          If this is NULL (which is it by default) the
- *                          executable path is used. If this is specified,
- *                          this path is used instead. This is intented used
- *                          to implement the LoadLibraryEx flag
- *                          LOAD_WITH_ALTERED_SEARCH_PATH.
- *
- * @status    Completely implemented.
- * @author    Sander van Leeuwen (sandervl@xs4all.nl)
- *            knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
- * @remark
- */
-BOOL Win32ImageBase::findDll(const char *pszFileName,
-                             char *pszFullName, int cchFullName,
-                             const char *pszAltPath /*=NULL*/)
-{
-    BOOL            fRet = FALSE;       /* Return value. (Pessimistic attitude! Init it to FALSE...) */
-    char *          psz;                /* General string pointer. */
-    int             cchFileName;        /* Length of the normalized filename (after ".DLL" is added). */
-    struct localvars                    /* local variables which are to big to fit onto the stack. */
-    {
-        char            szPath[1024];   /* Path buffer. Used to store pathlists. 1024 should be enough */
-                                        /* for LIBPATH (which at least had a limit of ca. 750 chars). */
-        char            sz[CCHMAXPATH]; /* Filename/path buffer. (Normally used to build filenames */
-                                        /* which are passed in as search experessions to DosFindFirst.) */
-        FILEFINDBUF3    findbuf3;       /* DosFindFirst buffer. */
-    } *             plv;
-    int             iPath;              /* Current path or pathlist being examined. This is the loop */
-                                        /* variable looping on the FINDDLL_* defines. */
-
-    /* These defines sets the order the paths and pathlists are examined. */
-    #define FINDDLL_EXECUTABLEDIR   1
-    #define FINDDLL_CURRENTDIR      2
-    #define FINDDLL_SYSTEM32DIR     3
-    #define FINDDLL_SYSTEM16DIR     4
-    #define FINDDLL_WINDIR          5
-    #define FINDDLL_PATH            6
-    #define FINDDLL_BEGINLIBPATH    7
-    #define FINDDLL_LIBPATH         8
-    #define FINDDLL_ENDLIBPATH      9
-    #define FINDDLL_FIRST           FINDDLL_EXECUTABLEDIR
-    #define FINDDLL_LAST            FINDDLL_ENDLIBPATH
-
-
-    /** @sketch
-     * Copy the filename to be found to the outputbuffer, and add .DLL if not '.'
-     * is found in the name. This has two reasons:
-     *  1) When searching paths we simply append the buffer contents to the path
-     *     being examined.
-     *  2) The buffer will at least return the passed in filename.
-     */
-    psz = strchr(pszFileName, '.');
-    cchFileName = strlen(pszFileName) + (psz ? 0 : 4);
-    if (cchFileName >= cchFullName)
-    {
-        dassert(cchFileName < cchFullName, ("KERNEL32:Win32ImageBase::findDll(%s, 0x%08x, %d): "
-                 "cchFileName (%d) >= cchFullName (%d)",
-                 pszFileName, pszFullName, cchFullName, cchFileName, cchFullName));
-        *pszFullName = '\0';
-        return FALSE;
-    }
-    strcpy(pszFullName, pszFileName);
-    if (psz == NULL)
-        strcpy(pszFullName + cchFileName - 4, ".DLL");
-
-
-    /** @sketch
-     * Allocate memory for local variables.
-     */
-    plv = (struct localvars *)malloc(sizeof(*plv));
-    if (!plv)
-    {
-        dassert(plv, ("KERNEL32:Win32ImageBase::findDll(%s, 0x%08x, %d): "
-                "malloc failed allocating %d bytes of memory for local variables.",
-                pszFileName, pszFullName, cchFullName, sizeof(*plv)));
-        return FALSE;
-    }
-
-
-    /** @sketch
-     * Loop thru the paths and pathlists searching them for the filename.
-     */
-    for (iPath = FINDDLL_FIRST; iPath <= FINDDLL_LAST; iPath++)
-    {
-        APIRET          rc;             /* Returncode from OS/2 APIs. */
-        const char  *   pszPath;        /* Pointer to the path being examined. */
-
-        /** @sketch
-         * Get the path/dir to examin. (This is determined by the value if iPath.)
-         */
-        switch (iPath)
-        {
-            case FINDDLL_EXECUTABLEDIR:
-                if (pszAltPath)
-                    pszPath = strcpy(plv->szPath, pszAltPath);
-                else
-                {
-                    /* ASSUMES: getFullPath allways returns a fully qualified
-                     *      path, ie. with at least one backslash. and that all
-                     *      slashes are backslashes!
-                     */
-                    if (!WinExe) continue;
-                    pszPath = strcpy(plv->szPath, WinExe->getFullPath());
-                }
-                psz = strrchr(plv->szPath, '\\');
-                dassert(psz, ("KERNEL32:Win32ImageBase::findDll(%s, 0x%08x, %d): "
-                        "WinExe->getFullPath returned a path not fully qualified: %s",
-                        pszFileName, pszFullName, cchFullName, pszPath));
-                if (psz)
-                    *psz = '\0';
-                else
-                    continue;
-                break;
-
-            case FINDDLL_CURRENTDIR:
-                pszPath = ".";
-                break;
-
-            case FINDDLL_SYSTEM32DIR:
-                pszPath = InternalGetSystemDirectoryA();
-                break;
-
-            case FINDDLL_SYSTEM16DIR:
-                #if 1
-                continue;               /* Skip this index */
-                #else
-                pszPath = InternalGetWindowsDirectoryA();
-                strcpy(plv->sz2, InternalGetWindowsDirectoryA());
-                strcat(plv->sz2, "\SYSTEM");
-                break;
-                #endif
-
-            case FINDDLL_WINDIR:
-                pszPath = InternalGetWindowsDirectoryA();
-                break;
-
-            case FINDDLL_PATH:
-                pszPath = getenv("PATH");
-                break;
-
-            case FINDDLL_BEGINLIBPATH:
-                rc = DosQueryExtLIBPATH(plv->szPath, BEGIN_LIBPATH);
-                if (rc != NO_ERROR)
-                {
-                    dassert(rc == NO_ERROR, ("KERNEL32:Win32ImageBase::findDll(%s, 0x%08x, %d): "
-                             "DosQueryExtLIBPATH failed with rc=%d, iPath=%d",
-                             pszFileName, pszFullName, cchFullName, rc, iPath));
-                    continue;
-                }
-                pszPath = plv->szPath;
-                break;
-
-            case FINDDLL_LIBPATH:
-                rc = DosQueryHeaderInfo(NULLHANDLE, 0, plv->szPath, sizeof(plv->szPath), QHINF_LIBPATH);
-                if (rc != NO_ERROR)
-                {
-                    dassert(rc == NO_ERROR, ("KERNEL32:Win32ImageBase::findDll(%s, 0x%08x, %d): "
-                             "DosQueryHeaderInfo failed with rc=%d, iPath=%d",
-                             pszFileName, pszFullName, cchFullName, rc, iPath));
-                    continue;
-                }
-                pszPath = plv->szPath;
-                break;
-
-            case FINDDLL_ENDLIBPATH:
-                rc = DosQueryExtLIBPATH(plv->szPath, END_LIBPATH);
-                if (rc != NO_ERROR)
-                {
-                    dassert(rc == NO_ERROR, ("KERNEL32:Win32ImageBase::findDll(%s, 0x%08x, %d): "
-                             "DosQueryExtLIBPATH failed with rc=%d, iPath=%d",
-                             pszFileName, pszFullName, cchFullName, rc, iPath));
-                    continue;
-                }
-                pszPath = plv->szPath;
-                break;
-
-            default: /* !internalerror! */
-                goto end;
-        }
-
-
-        /** @sketch
-         * pszPath is now set to the pathlist to be searched.
-         * So we'll loop thru all the paths in the list.
-         */
-        while (pszPath != NULL && *pszPath != '\0')
-        {
-            HDIR    hDir;               /* Find handle used when calling FindFirst. */
-            ULONG   culFiles;           /* Number of files to find / found. */
-            char *  pszNext;            /* Pointer to the next pathlist path */
-            int     cch;                /* Length of path (including the slash after the slash is added). */
-
-            /** @sketch
-             * Find the end of the path.
-             * Copy the path into the plv->sz buffer.
-             * Set pszNext.
-             */
-            pszNext = strchr(pszPath, ';');
-            if (pszNext != NULL)
-            {
-                cch = pszNext - pszPath;
-                pszNext++;
-            }
-            else
-                cch = strlen(pszPath);
-
-            if (cch + cchFileName + 1 >= sizeof(plv->sz)) /* assertion */
-            {
-                dassert(cch + cchFileName + 1 < sizeof(plv->sz), ("KERNEL32:Win32ImageBase::findDll(%s, 0x%08x, %d): "
-                        "cch (%d) + cchFileName (%d) + 1 < sizeof(plv->sz) (%d) - paths too long!, iPath=%d",
-                        pszFileName, pszFullName, cchFullName, cch, cchFileName, sizeof(plv->sz), iPath));
-                pszPath = pszNext;
-                continue;
-            }
-            memcpy(plv->sz, pszPath, cch); //arg! Someone made strncpy not work as supposed!
-
-
-            /** @sketch
-             * Add a '\\' and the filename (pszFullname) to the path;
-             * then we'll have a fullpath.
-             */
-            plv->sz[cch++] = '\\';
-            strcpy(&plv->sz[cch], pszFullName);
-
-
-            /** @sketch
-             *  Use DosFindFirst to check if the file exists.
-             *  IF the file exists THEN
-             *      Query Fullpath using OS/2 API.
-             *      IF unsuccessful THEN return relative name.
-             *          Check that the fullname buffer is large enough.
-             *          Copy the filename found to the fullname buffer.
-             *      ENDIF
-             *      IF successful THEN uppercase the fullname buffer.
-             *      goto end
-             *  ENDIF
-             */
-            hDir = HDIR_CREATE;
-            culFiles = 1;
-            rc = DosFindFirst(plv->sz, &hDir, FILE_NORMAL,
-                              &plv->findbuf3, sizeof(plv->findbuf3),
-                              &culFiles, FIL_STANDARD);
-            DosFindClose(hDir);
-            if (culFiles >= 1 && rc == NO_ERROR)
-            {
-                /* Return full path - we'll currently return a relative path. */
-                rc = DosQueryPathInfo(plv->sz, FIL_QUERYFULLNAME, pszFullName, cchFullName);
-                fRet = rc == NO_ERROR;
-                if (!fRet)
-                {
-                    /* Return a relative path - probably better that failing... */
-                    dassert(rc == NO_ERROR, ("KERNEL32:Win32ImageBase::findDll(%s, 0x%08x, %d): "
-                            "rc = %d",
-                            pszFileName, pszFullName, cchFullName, rc));
-
-                    if (cch + cchFileName + 1 <= cchFullName)
-                    {
-                        strcpy(pszFullName, plv->sz);
-                        strcpy(pszFullName + cch, plv->findbuf3.achName);
-                        fRet = TRUE;
-                    }
-                    else
-                    {
-                        dassert(cch + cchFileName + 1 > cchFullName, ("KERNEL32:Win32ImageBase::findDll(%s, 0x%08x, %d): "
-                                "cch (%d) + cchFileName (%d) + 1 < cchFullName (%d); %s",
-                                pszFileName, pszFullName, cchFullName, cch, cchFileName, cchFullName, plv->sz));
-                    }
-                }
-                if (fRet) strupr(pszFullName);
-                goto end;
-            }
-
-            pszPath = pszNext;
-        }
-    } /* for iPath */
-
-
-end:
-    /*
-     * Cleanup: free local variables.
-     */
-    free(plv);
-    return fRet;
-}
-
-
-//******************************************************************************
-//******************************************************************************
-/**
- * Checks if a file is a PE executable image valid to be executed on this machine.
- * @returns   FALSE if not PE image.
- *            > 0 if valid PE image.
- *                1 DLL
- *                2 EXE
- * @param     szFilename    Pointer to filename
- * @author    knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
- * @remark    Should not call findDll!
- */
+//******************************************************************************
+//******************************************************************************
+BOOL Win32ImageBase::findDll(const char *szFileName, char *szFullName, 
+                             int cchFullFileName, const char *pszAltPath)
+{
+ char   modname[CCHMAXPATH];
+ HFILE  dllfile = NULL;
+ char  *imagepath;
+
+  strcpy(szFullName, szFileName);
+  strupr(szFullName);
+  if(!strchr(szFullName, (int)'.')) {
+    	strcat(szFullName,".DLL");
+  }
+
+  //search order:
+  //1) exe dir
+  //2) current dir
+  //3) windows system dir (kernel32 path)
+  //4) windows dir
+  //5) path
+  if(WinExe) {
+  	strcpy(modname, WinExe->getFullPath());
+  	//remove file name from full path
+  	imagepath = modname + strlen(modname) - 1;
+  	while(*imagepath != '\\') imagepath--;
+  	imagepath[1] = 0;
+  	strcat(modname, szFullName);
+  	dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
+  }
+  if(dllfile == NULL) {
+	strcpy(modname, szFullName);
+  	dllfile = OSLibDosOpen(szFullName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
+	if(dllfile == NULL) {
+	    	strcpy(modname, InternalGetSystemDirectoryA());
+		strcat(modname, "\\");
+	    	strcat(modname, szFullName);
+		dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
+		if(dllfile == NULL) {
+			strcpy(modname, InternalGetWindowsDirectoryA());
+			strcat(modname, "\\");
+	    		strcat(modname, szFullName);
+			dllfile = OSLibDosOpen(modname, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
+			if(dllfile == NULL) {
+				if(OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFullName, modname, sizeof(modname)) == 0) {
+					return FALSE;
+				}
+			}
+		}
+	}
+  }
+  strcpy(szFullName, modname);
+  if(dllfile) OSLibDosClose(dllfile);
+  return TRUE;
+}
+//******************************************************************************
+//******************************************************************************
 BOOL Win32ImageBase::isPEImage(char *szFileName)
 {
@@ -486,5 +209,4 @@
         return FALSE;
   }
-
   rc = DosOpen(filename,                       /* File path name */
                &win32handle,                   /* File handle */
@@ -550,5 +272,5 @@
   }
   DosClose(win32handle);
-  return (fh.Characteristics & IMAGE_FILE_DLL ? 1 : 2);
+  return(TRUE);
 
 failure:
Index: trunk/src/kernel32/winimagebase.h
===================================================================
--- trunk/src/kernel32/winimagebase.h	(revision 3402)
+++ trunk/src/kernel32/winimagebase.h	(revision 3404)
@@ -1,8 +1,8 @@
-/* $Id: winimagebase.h,v 1.5 2000-04-16 04:27:38 bird Exp $ */
+/* $Id: winimagebase.h,v 1.6 2000-04-16 10:42:12 sandervl Exp $ */
 
 /*
  * Win32 PE Image base class
  *
- * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
+ * Copyright 1998-1999 Sander van Leeuwen (sandervl@xs4all.nl)
  *
  *
@@ -65,5 +65,5 @@
         char *getFullPath()           { return fullpath; };
 
-    char *getModuleName()         { return szModule; };
+	char *getModuleName()	      { return szModule; };
 
 virtual HRSRC findResourceA(LPCSTR lpszName, LPSTR lpszType, ULONG lang = LANG_GETFIRST);
@@ -73,7 +73,7 @@
 virtual BOOL  enumResourceNamesA(HMODULE hmod, LPCTSTR  lpszType, ENUMRESNAMEPROCA lpEnumFunc, LONG lParam);
 virtual BOOL  enumResourceNamesW(HMODULE hmod, LPCWSTR  lpszType, ENUMRESNAMEPROCW lpEnumFunc, LONG lParam);
-virtual BOOL  enumResourceTypesA(HMODULE hmod, ENUMRESTYPEPROCA lpEnumFunc,
+virtual BOOL  enumResourceTypesA(HMODULE hmod, ENUMRESTYPEPROCA lpEnumFunc, 
                                  LONG lParam);
-virtual BOOL  enumResourceTypesW(HMODULE hmod, ENUMRESTYPEPROCW lpEnumFunc,
+virtual BOOL  enumResourceTypesW(HMODULE hmod, ENUMRESTYPEPROCW lpEnumFunc, 
                                  LONG lParam);
 
@@ -85,20 +85,20 @@
                       int cchFullName, const char *pszAltPath = NULL);
 
-    void  setEntryPoint(ULONG startAddress) { entryPoint = startAddress; };
+	void  setEntryPoint(ULONG startAddress) { entryPoint = startAddress; };
 
-        void  setTLSAddress(LPVOID dwTlsAddress)        { tlsAddress = dwTlsAddress; };
-        void  setTLSIndexAddr(LPDWORD dwTlsIndexAddr)   { tlsIndexAddr = dwTlsIndexAddr; };
-        void  setTLSInitSize(ULONG dwTlsSize)       { tlsInitSize = dwTlsSize; };
-        void  setTLSTotalSize(ULONG dwTlsSize)      { tlsTotalSize = dwTlsSize; };
+        void  setTLSAddress(LPVOID dwTlsAddress)     	{ tlsAddress = dwTlsAddress; };
+        void  setTLSIndexAddr(LPDWORD dwTlsIndexAddr)	{ tlsIndexAddr = dwTlsIndexAddr; };
+        void  setTLSInitSize(ULONG dwTlsSize)		{ tlsInitSize = dwTlsSize; };
+        void  setTLSTotalSize(ULONG dwTlsSize)		{ tlsTotalSize = dwTlsSize; };
         void  setTLSCallBackAddr(PIMAGE_TLS_CALLBACK *dwTlsCallBackAddr)
-    {
-       tlsCallBackAddr = dwTlsCallBackAddr;
-    };
+	{
+	   tlsCallBackAddr = dwTlsCallBackAddr;
+	};
 
-    void  tlsAttachThread();    //setup TLS structures for new thread
-    void  tlsDetachThread();    //destroy TLS structures
+	void  tlsAttachThread();	//setup TLS structures for new thread
+	void  tlsDetachThread();	//destroy TLS structures
 
-virtual ULONG     getApi(char *name)  = 0;
-virtual ULONG     getApi(int ordinal) = 0;
+virtual	ULONG getApi(char *name)  = 0;
+virtual ULONG getApi(int ordinal) = 0;
 
 virtual BOOL  isDll() = 0;
@@ -106,11 +106,11 @@
 static Win32ImageBase * findModule(HMODULE hModule);
 
-    //Add image to dependency list of this image
-    void  addDependency(Win32DllBase *dll);
+	//Add image to dependency list of this image
+	void  addDependency(Win32DllBase *dll);
         BOOL  dependsOn(Win32DllBase *dll);
 
 protected:
-    void tlsAlloc();        //Allocate TLS index for this module
-    void tlsDelete();       //Destroy TLS index for this module
+	void tlsAlloc();		//Allocate TLS index for this module
+	void tlsDelete();		//Destroy TLS index for this module
 
         Win32Resource        *winres;
@@ -119,15 +119,15 @@
 
         char                 *fullpath;
-    char              szModule[CCHMAXPATH];
-    char                  szFileName[CCHMAXPATH];
+	char      	      szModule[CCHMAXPATH];
+	char                  szFileName[CCHMAXPATH];
 
         HINSTANCE             hinstance;
 
-    LPVOID                tlsAddress;       //address of TLS data
-    LPDWORD           tlsIndexAddr;     //address of DWORD that receives the TLS index
-    ULONG             tlsInitSize;      //size of initialized TLS memory block
-    ULONG             tlsTotalSize;     //size of TLS memory block
-    PIMAGE_TLS_CALLBACK  *tlsCallBackAddr;  //ptr to TLS callback array
-    ULONG                 tlsIndex;     //module TLS index
+  	LPVOID 	              tlsAddress;		//address of TLS data
+  	LPDWORD		      tlsIndexAddr;		//address of DWORD that receives the TLS index
+  	ULONG 		      tlsInitSize;		//size of initialized TLS memory block
+  	ULONG 		      tlsTotalSize;		//size of TLS memory block
+  	PIMAGE_TLS_CALLBACK  *tlsCallBackAddr;	//ptr to TLS callback array
+	ULONG                 tlsIndex;		//module TLS index
 
         ULONG getPEResourceSize(ULONG id, ULONG type, ULONG lang = LANG_GETFIRST);
@@ -144,6 +144,6 @@
         ULONG                     ulRVAResourceSection;
 
-    //linked list of dlls loaded on behalf of this executable image (dll or exe)
-        Queue                  loadedDlls;
+	//linked list of dlls loaded on behalf of this executable image (dll or exe)
+        Queue         		   loadedDlls;
 private:
 
@@ -162,5 +162,5 @@
 } WINIMAGE_LOOKUP;
 
-#define WINIMAGE_LOOKUPADDR(a)  (WINIMAGE_LOOKUP *)((ULONG)a + PAGE_SIZE - sizeof(WINIMAGE_LOOKUP))
+#define WINIMAGE_LOOKUPADDR(a)	(WINIMAGE_LOOKUP *)((ULONG)a + PAGE_SIZE - sizeof(WINIMAGE_LOOKUP))
 
 #endif //__WINIMAGEBASE_H__
Index: trunk/src/kernel32/winimagepeldr.cpp
===================================================================
--- trunk/src/kernel32/winimagepeldr.cpp	(revision 3402)
+++ trunk/src/kernel32/winimagepeldr.cpp	(revision 3404)
@@ -1,3 +1,3 @@
-/* $Id: winimagepeldr.cpp,v 1.40 2000-04-15 21:17:06 bird Exp $ */
+/* $Id: winimagepeldr.cpp,v 1.41 2000-04-16 10:42:12 sandervl Exp $ */
 
 /*
@@ -18,4 +18,5 @@
  *       So an instance of this type can't be used for anything but resource lookup!
  *
+ *
  */
 #define INCL_DOSFILEMGR          /* File Manager values      */
@@ -77,11 +78,11 @@
  char logname[CCHMAXPATH];
 
-    sprintf(logname, "pe_%d.log", loadNr);
-        _privateLogFile = fopen(logname, "w");
-    if(_privateLogFile == NULL) {
-        sprintf(logname, "%spe_%d.log", kernel32Path, loadNr);
-            _privateLogFile = fopen(logname, "w");
-    }
-    dprintfGlobal(("PE LOGFILE : %s", logname));
+	sprintf(logname, "pe_%d.log", loadNr);
+    	_privateLogFile = fopen(logname, "w");
+	if(_privateLogFile == NULL) {
+		sprintf(logname, "%spe_%d.log", kernel32Path, loadNr);
+    		_privateLogFile = fopen(logname, "w");
+	}
+	dprintfGlobal(("PE LOGFILE : %s", logname));
 #endif
 }
@@ -91,8 +92,8 @@
 {
 #ifdef DEBUG
-    if(_privateLogFile) {
-        fclose(_privateLogFile);
-        _privateLogFile = NULL;
-    }
+  	if(_privateLogFile) {
+		fclose(_privateLogFile);
+		_privateLogFile = NULL;
+	}
 #endif
 }
@@ -113,22 +114,22 @@
   strupr(szFileName);
   if(isExe) {
-    if(!strchr(szFileName, '.')) {
-        strcat(szFileName,".EXE");
-    }
-    dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
-    if(dllfile == NULL) {
-        if(!strstr(szFileName, ".EXE")) {
-            strcat(szFileName,".EXE");
-        }
-        dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
-        if(dllfile == NULL) {
-            OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFileName, szFileName, sizeof(szFileName));
-        }
-    }
-    else    OSLibDosClose(dllfile);
+	if(!strchr(szFileName, '.')) {
+		strcat(szFileName,".EXE");
+	}
+  	dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
+  	if(dllfile == NULL) {
+		if(!strstr(szFileName, ".EXE")) {
+			strcat(szFileName,".EXE");
+		}
+  		dllfile = OSLibDosOpen(szFileName, OSLIB_ACCESS_READONLY|OSLIB_ACCESS_SHAREDENYNONE);
+  		if(dllfile == NULL) {
+			OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", szFileName, szFileName, sizeof(szFileName));
+		}
+	}
+  	else	OSLibDosClose(dllfile);
   }
   else {
-    if (findDll(szFileName, szModule, sizeof(szModule)))
-        strcpy(szFileName, szModule);
+  	findDll(szFileName, szModule, sizeof(szModule));
+	strcpy(szFileName, szModule);
   }
   strcpy(szModule, OSLibStripPath(szFileName));
@@ -136,10 +137,10 @@
   char *dot = strstr(szModule, ".");
   while(dot) {
-    char *newdot = strstr(dot+1, ".");
-    if(newdot == NULL)  break;
-    dot = newdot;
+	char *newdot = strstr(dot+1, ".");
+	if(newdot == NULL)	break;
+	dot = newdot;
   }
   if(dot)
-    *dot = 0;
+	*dot = 0;
 }
 //******************************************************************************
@@ -148,19 +149,19 @@
 {
   if(memmap)
-    delete memmap;
+	delete memmap;
 
   if(hFile) {
-    OSLibDosClose(hFile);
-    hFile = 0;
+  	OSLibDosClose(hFile);
+	hFile = 0;
   }
 
   if(realBaseAddress)
-        DosFreeMem((PVOID)realBaseAddress);
+    	DosFreeMem((PVOID)realBaseAddress);
 
   if(nameexports)
-        free(nameexports);
+    	free(nameexports);
 
   if(ordexports)
-        free(ordexports);
+    	free(ordexports);
 }
 //******************************************************************************
@@ -183,33 +184,33 @@
   strcpy(szErrorModule, OSLibStripPath(szFileName));
   if(hFile == NULL) {
-        goto failure;
+    	goto failure;
   }
   //read dos header
   if(DosRead(hFile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
-        goto failure;
+    	goto failure;
   }
   if(OSLibDosSetFilePtr(hFile, doshdr.e_lfanew, OSLIB_SETPTR_FILE_BEGIN) == -1) {
-        goto failure;
+    	goto failure;
   }
   //read signature dword
   if(DosRead(hFile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
-        goto failure;
+    	goto failure;
   }
   //read pe header
   if(DosRead(hFile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
-        goto failure;
+    	goto failure;
   }
   //read optional header
   if(DosRead(hFile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
-        goto failure;
+    	goto failure;
   }
   if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
-        dprintf((LOG, "Not a valid PE file (probably a 16 bits windows exe/dll)!"));
+    	dprintf((LOG, "Not a valid PE file (probably a 16 bits windows exe/dll)!"));
         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-        goto failure;
+    	goto failure;
   }
 
   if(oh.SizeOfImage == 0) {//just in case
-    oh.SizeOfImage = OSLibDosGetFileSize(hFile);
+	oh.SizeOfImage = OSLibDosGetFileSize(hFile);
   }
 
@@ -217,37 +218,37 @@
   //Allocate memory to hold the entire image
   if(allocSections(reservedMem) == FALSE) {
-        dprintf((LOG, "Failed to allocate image memory, rc %d", errorState));;
-        goto failure;
+    	dprintf((LOG, "Failed to allocate image memory, rc %d", errorState));;
+    	goto failure;
   }
 
   memmap = new Win32MemMap(this, realBaseAddress, imageSize);
   if(memmap == NULL || !memmap->Init(0)) {
-        goto failure;
+    	goto failure;
   }
   win32file = memmap->mapViewOfFile(0, 0, 2);
 
   if(DosQueryPathInfo(szFileName, FIL_QUERYFULLNAME, szFullPath, sizeof(szFullPath)) == 0) {
-    setFullPath(szFullPath);
+	setFullPath(szFullPath);
   }
 
   if(!(fh.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE)) {//not valid
-        dprintf((LOG, "Not a valid PE file!"));
+    	dprintf((LOG, "Not a valid PE file!"));
         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szPEErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-        goto failure;
+    	goto failure;
   }
   if(fh.Machine != IMAGE_FILE_MACHINE_I386) {
-        dprintf((LOG, "Doesn't run on x86 processors!"));
+    	dprintf((LOG, "Doesn't run on x86 processors!"));
         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szCPUErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-        goto failure;
+    	goto failure;
   }
   //IMAGE_FILE_SYSTEM == only drivers (device/file system/video etc)?
   if(fh.Characteristics & IMAGE_FILE_SYSTEM) {
-        dprintf((LOG, "Can't convert system files"));
+    	dprintf((LOG, "Can't convert system files"));
         WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szExeErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-        goto failure;
+    	goto failure;
   }
 
   if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
-        dprintf((LOG, "No fixups, might not run!"));
+    	dprintf((LOG, "No fixups, might not run!"));
   }
 
@@ -283,5 +284,5 @@
    if ((psh = (PIMAGE_SECTION_HEADER)SECTIONHDROFF (win32file)) != NULL) {
     dprintf((LOG, "*************************PE SECTIONS START**************************" ));
-    for (i=0; i<nSections; i++) {
+    for (i=0; i<nSections; i++) { 
         dprintf((LOG, "Raw data size:        %x", psh[i].SizeOfRawData ));
         dprintf((LOG, "Virtual Address:      %x", psh[i].VirtualAddress ));
@@ -312,14 +313,14 @@
             continue;
         }
-    if(strcmp(psh[i].Name, ".tls") == 0)
-    {
-        tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
-        if(tlsDir) {
-                addSection(SECTION_TLS, psh[i].PointerToRawData,
-                           psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
-                           psh[i].Misc.VirtualSize, psh[i].Characteristics);
-        }
-        continue;
-    }
+ 	if(strcmp(psh[i].Name, ".tls") == 0)
+	{
+		tlsDir = (IMAGE_TLS_DIRECTORY *)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_TLS);
+		if(tlsDir) {
+		        addSection(SECTION_TLS, psh[i].PointerToRawData,
+                		   psh[i].SizeOfRawData, psh[i].VirtualAddress + oh.ImageBase,
+                   		   psh[i].Misc.VirtualSize, psh[i].Characteristics);
+		}
+		continue;
+	}
 
         if(strcmp(psh[i].Name, ".debug") == 0) {
@@ -330,6 +331,6 @@
             continue;
         }
-    if(IsImportSection(win32file, &psh[i]))
-        {
+	if(IsImportSection(win32file, &psh[i]))
+    	{
           int type = SECTION_IMPORT;
             dprintf((LOG, "Import Data Section" ));
@@ -385,15 +386,15 @@
         }
         dprintf((LOG, "Unknown section" ));
-        goto failure;
+    	goto failure;
      }
    }
   }
   else {
-    if(GetSectionHdrByName (win32file, &sh, ".rsrc"))
+  	if(GetSectionHdrByName (win32file, &sh, ".rsrc"))
         {
             addSection(SECTION_RESOURCE, sh.PointerToRawData,
                        sh.SizeOfRawData, sh.VirtualAddress + oh.ImageBase,
                        sh.Misc.VirtualSize, sh.Characteristics);
-    }
+  	}
   }
   dprintf((LOG, "*************************PE SECTIONS END **************************" ));
@@ -405,24 +406,24 @@
   //In case there are any gaps between sections, adjust size
   if(imageSize != imageVirtEnd - oh.ImageBase) {
-        dprintf((LOG, "imageSize != imageVirtEnd - oh.ImageBase!" ));
-        imageSize = imageVirtEnd - oh.ImageBase;
+    	dprintf((LOG, "imageSize != imageVirtEnd - oh.ImageBase!" ));
+    	imageSize = imageVirtEnd - oh.ImageBase;
   }
   if(imageSize < oh.SizeOfImage) {
-    imageSize = oh.SizeOfImage;
+	imageSize = oh.SizeOfImage;
   }
 
   dprintf((LOG, "OS/2 base address %x", realBaseAddress ));
   if(oh.AddressOfEntryPoint) {
-    entryPoint = realBaseAddress + oh.AddressOfEntryPoint;
+  	entryPoint = realBaseAddress + oh.AddressOfEntryPoint;
   }
   else {
-    dprintf((LOG, "EntryPoint == NULL" ));
-    entryPoint = NULL;
+	dprintf((LOG, "EntryPoint == NULL" ));
+	entryPoint = NULL;
   }
 
   //set memory protection flags
   if(setMemFlags() == FALSE) {
-        dprintf((LOG, "Failed to set memory protection" ));
-        goto failure;
+    	dprintf((LOG, "Failed to set memory protection" ));
+    	goto failure;
   }
 
@@ -432,62 +433,62 @@
     Section *sect = findSection(SECTION_TLS);
 
-    if(sect == NULL) {
-        dprintf((LOG, "Couldn't find TLS section!!" ));
-            goto failure;
-    }
-    dprintf((LOG, "TLS Directory" ));
-    dprintf((LOG, "TLS Address of Index     %x", tlsDir->AddressOfIndex ));
-    dprintf((LOG, "TLS Address of Callbacks %x", tlsDir->AddressOfCallBacks ));
-    dprintf((LOG, "TLS SizeOfZeroFill       %x", tlsDir->SizeOfZeroFill ));
-    dprintf((LOG, "TLS Characteristics      %x", tlsDir->Characteristics ));
-    setTLSAddress((char *)sect->realvirtaddr);
-    setTLSInitSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData);
-    setTLSTotalSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData + tlsDir->SizeOfZeroFill);
-
-    sect = findSectionByAddr((ULONG)tlsDir->AddressOfIndex);
-    if(sect == NULL) {
-        dprintf((LOG, "Couldn't find TLS AddressOfIndex section!!" ));
-            goto failure;
-    }
-    setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
-
-    if((ULONG)tlsDir->AddressOfCallBacks != 0) {
-        sect = findSectionByAddr((ULONG)tlsDir->AddressOfCallBacks);
-        if(sect == NULL) {
-            dprintf((LOG, "Couldn't find TLS AddressOfCallBacks section!!" ));
-                goto failure;
-        }
-        setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
-    }
+	if(sect == NULL) {
+		dprintf((LOG, "Couldn't find TLS section!!" ));
+	    	goto failure;
+	}
+	dprintf((LOG, "TLS Directory" ));
+	dprintf((LOG, "TLS Address of Index     %x", tlsDir->AddressOfIndex ));
+	dprintf((LOG, "TLS Address of Callbacks %x", tlsDir->AddressOfCallBacks ));
+	dprintf((LOG, "TLS SizeOfZeroFill       %x", tlsDir->SizeOfZeroFill ));
+	dprintf((LOG, "TLS Characteristics      %x", tlsDir->Characteristics ));
+  	setTLSAddress((char *)sect->realvirtaddr);
+  	setTLSInitSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData);
+  	setTLSTotalSize(tlsDir->EndAddressOfRawData - tlsDir->StartAddressOfRawData + tlsDir->SizeOfZeroFill);
+
+	sect = findSectionByAddr((ULONG)tlsDir->AddressOfIndex);
+	if(sect == NULL) {
+		dprintf((LOG, "Couldn't find TLS AddressOfIndex section!!" ));
+	    	goto failure;
+	}
+  	setTLSIndexAddr((LPDWORD)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfIndex - sect->virtaddr)));
+
+	if((ULONG)tlsDir->AddressOfCallBacks != 0) {
+		sect = findSectionByAddr((ULONG)tlsDir->AddressOfCallBacks);
+		if(sect == NULL) {
+			dprintf((LOG, "Couldn't find TLS AddressOfCallBacks section!!" ));
+		    	goto failure;
+		}
+		setTLSCallBackAddr((PIMAGE_TLS_CALLBACK *)(sect->realvirtaddr + ((ULONG)tlsDir->AddressOfCallBacks - sect->virtaddr)));
+	}
    }
 
    if(realBaseAddress != oh.ImageBase) {
-    pFixups = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
-    commitPage((ULONG)pFixups, FALSE);
+	pFixups = (PIMAGE_BASE_RELOCATION)ImageDirectoryOffset(win32file, IMAGE_DIRECTORY_ENTRY_BASERELOC);
+	commitPage((ULONG)pFixups, FALSE);
    }
 #ifdef COMMIT_ALL
    for (i=0; i<nSections; i++) {
-    commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
+	commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
    }
 #else
    for (i=0; i<nSections; i++) {
-    switch(section[i].type)
-    {
-    case SECTION_IMPORT:
-    case SECTION_RELOC:
-    case SECTION_EXPORT:
-        commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
-        break;
-    }
+	switch(section[i].type)
+	{
+	case SECTION_IMPORT:
+	case SECTION_RELOC:
+	case SECTION_EXPORT:
+		commitPage((ULONG)section[i].realvirtaddr, FALSE, COMPLETE_SECTION);
+		break;
+	}
    }
 #endif
    if(processExports((char *)win32file) == FALSE) {
-        dprintf((LOG, "Failed to process exported apis" ));
-        goto failure;
+       	dprintf((LOG, "Failed to process exported apis" ));
+    	goto failure;
    }
   }
 #ifdef COMMIT_ALL
   else {
-    commitPage((ULONG)section[0].realvirtaddr, FALSE, COMPLETE_SECTION);
+	commitPage((ULONG)section[0].realvirtaddr, FALSE, COMPLETE_SECTION);
   }
 #endif
@@ -502,6 +503,6 @@
   //    implicitly call functions depending on it.
   if(GetSectionHdrByName (win32file, &sh, ".rsrc")) {
-        //get offset in resource object of directory entry
-    pResDir = (PIMAGE_RESOURCE_DIRECTORY)(sh.VirtualAddress + realBaseAddress);
+    	//get offset in resource object of directory entry
+	pResDir = (PIMAGE_RESOURCE_DIRECTORY)(sh.VirtualAddress + realBaseAddress);
         ulRVAResourceSection = sh.VirtualAddress;
   }
@@ -510,6 +511,6 @@
   {
    if(processImports((char *)win32file) == FALSE) {
-        dprintf((LOG, "Failed to process imports!" ));
-        goto failure;
+    	dprintf((LOG, "Failed to process imports!" ));
+     	goto failure;
    }
   }
@@ -518,10 +519,10 @@
 failure:
   if(memmap) {
-    delete memmap;
-    memmap = NULL;
+	delete memmap;
+	memmap = NULL;
   }
   if(hFile) {
-    OSLibDosClose(hFile);
-    hFile = 0;
+  	OSLibDosClose(hFile);
+	hFile = 0;
   }
   errorState = ERROR_INTERNAL;
@@ -541,38 +542,38 @@
   //Round down to nearest page boundary
   virtAddress = virtAddress & ~0xFFF;
-
+  
   section = findSectionByOS2Addr(virtAddress);
   if(section == NULL) {
-    size        = 4096;
-    sectionsize = 4096;
-    protflags   = PAG_READ|PAG_WRITE; //readonly?
-    section = findPreviousSectionByOS2Addr(virtAddress);
-    if(section == NULL) {//access to header
-        offset     = 0;
-        fileoffset = virtAddress - realBaseAddress;
-    }
-    else {
-        offset     = virtAddress - (section->realvirtaddr + section->virtualsize);
-        fileoffset = section->rawoffset + section->rawsize + offset;
-    }
+	size        = 4096;
+	sectionsize = 4096;
+	protflags   = PAG_READ|PAG_WRITE; //readonly?
+	section = findPreviousSectionByOS2Addr(virtAddress);
+  	if(section == NULL) {//access to header
+		offset     = 0;
+		fileoffset = virtAddress - realBaseAddress;
+	}
+	else {
+		offset     = virtAddress - (section->realvirtaddr + section->virtualsize);
+		fileoffset = section->rawoffset + section->rawsize + offset;
+	}
   }
   else {
-    protflags   = section->pageflags;
-    offset      = virtAddress - section->realvirtaddr;
-    sectionsize = section->virtualsize - offset;
-
-    if(offset > section->rawsize || section->type == SECTION_UNINITDATA) {
-        //unintialized data (set to 0)
-        size = 0;
-        fileoffset = -1;
-    }
-    else {
-        size = section->rawsize-offset;
-        fileoffset = section->rawoffset + offset;
-    }
-    if(fWriteAccess & !(section->pageflags & PAG_WRITE)) {
-        dprintf((LOG, "Win32PeLdrImage::commitPage: No write access to 0%x!", virtAddress));
-        return FALSE;
-    }
+  	protflags   = section->pageflags;
+  	offset      = virtAddress - section->realvirtaddr;
+  	sectionsize = section->virtualsize - offset;
+
+  	if(offset > section->rawsize || section->type == SECTION_UNINITDATA) {
+		//unintialized data (set to 0)
+		size = 0;
+		fileoffset = -1;
+	}
+  	else {
+		size = section->rawsize-offset;
+  		fileoffset = section->rawoffset + offset;
+	}
+  	if(fWriteAccess & !(section->pageflags & PAG_WRITE)) {
+		dprintf((LOG, "Win32PeLdrImage::commitPage: No write access to 0%x!", virtAddress));
+		return FALSE;
+	}
   }
   //Check range of pages with the same attributes starting at virtAddress
@@ -581,20 +582,20 @@
   rc = DosQueryMem((PVOID)virtAddress, &range, &attr);
   if(rc) {
-    dprintf((LOG, "Win32PeLdrImage::commitPage: DosQueryMem for %x returned %d", virtAddress, rc));
-    return FALSE;
+	dprintf((LOG, "Win32PeLdrImage::commitPage: DosQueryMem for %x returned %d", virtAddress, rc));
+	return FALSE;
   }
   if(attr & PAG_COMMIT) {
-    dprintf((LOG, "Win32PeLdrImage::commitPage: Memory at 0x%x already committed!", virtAddress));
-    return FALSE;
+	dprintf((LOG, "Win32PeLdrImage::commitPage: Memory at 0x%x already committed!", virtAddress));
+	return FALSE;
   }
 
   if(fPageCmd == SINGLE_PAGE) {
-    size = min(size, PAGE_SIZE);
-    sectionsize = min(sectionsize, PAGE_SIZE);
+	size = min(size, PAGE_SIZE);
+	sectionsize = min(sectionsize, PAGE_SIZE);
   }
   else
   if(fPageCmd == SECTION_PAGES) {
-    size = min(size, DEFAULT_NR_PAGES*PAGE_SIZE);
-    sectionsize = min(sectionsize, DEFAULT_NR_PAGES*PAGE_SIZE);
+	size = min(size, DEFAULT_NR_PAGES*PAGE_SIZE);
+	sectionsize = min(sectionsize, DEFAULT_NR_PAGES*PAGE_SIZE);
   }
   size = min(size, range);
@@ -602,46 +603,46 @@
 
   if(fileoffset != -1) {
-    rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
-    if(rc) {
-        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
-            return FALSE;
-    }
-
-    if(DosSetFilePtr(hFile, fileoffset, FILE_BEGIN, &ulNewPos) == -1) {
-        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetFilePtr failed for 0x%x!", fileoffset));
-            return FALSE;
-    }
-    if(DosRead(hFile, (PVOID)virtAddress, size, &ulRead)) {
-        dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed for 0x%x!", virtAddress));
-            return FALSE;
-    }
-    if(ulRead != size) {
-        dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed to read %x (%x) bytes at %x for 0x%x!", size, ulRead, fileoffset, virtAddress));
-            return FALSE;
-    }
-    if(realBaseAddress != oh.ImageBase) {
-        setFixups(virtAddress, sectionsize);
-    }
-
-    rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
-    if(rc) {
-        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
-        return FALSE;
-    }
+	rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
+	if(rc) {
+		dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
+    		return FALSE;
+	}
+
+  	if(DosSetFilePtr(hFile, fileoffset, FILE_BEGIN, &ulNewPos) == -1) {
+		dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetFilePtr failed for 0x%x!", fileoffset));
+    		return FALSE;
+  	}
+  	if(DosRead(hFile, (PVOID)virtAddress, size, &ulRead)) {
+		dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed for 0x%x!", virtAddress));
+    		return FALSE;
+  	}
+	if(ulRead != size) {
+		dprintf((LOG, "Win32PeLdrImage::commitPage: DosRead failed to read %x (%x) bytes at %x for 0x%x!", size, ulRead, fileoffset, virtAddress));
+    		return FALSE;
+	}
+   	if(realBaseAddress != oh.ImageBase) {
+		setFixups(virtAddress, sectionsize);
+	}
+
+  	rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
+  	if(rc) {
+		dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
+	  	return FALSE;
+	}
   }
   else {
-    rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
-    if(rc) {
-        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
-            return FALSE;
-    }
-    if(realBaseAddress != oh.ImageBase) {
-        setFixups(virtAddress, sectionsize);
-    }
-    rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
-    if(rc) {
-        dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
-        return FALSE;
-    }
+	rc = DosSetMem((PVOID)virtAddress, sectionsize, PAG_READ|PAG_WRITE|PAG_COMMIT);
+	if(rc) {
+		dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
+    		return FALSE;
+	}
+   	if(realBaseAddress != oh.ImageBase) {
+		setFixups(virtAddress, sectionsize);
+	}
+  	rc = DosSetMem((PVOID)virtAddress, sectionsize, protflags);
+  	if(rc) {
+		dprintf((LOG, "Win32PeLdrImage::commitPage: DosSetMem failed (%d)!", rc));
+	  	return FALSE;
+	}
   }
   return TRUE;
@@ -664,7 +665,7 @@
 
   if(virtaddress < imageVirtBase)
-        imageVirtBase = virtaddress;
+    	imageVirtBase = virtaddress;
   if(virtaddress + virtsize > imageVirtEnd)
-        imageVirtEnd  = virtaddress + virtsize;
+    	imageVirtEnd  = virtaddress + virtsize;
 
   nrsections++;
@@ -679,11 +680,11 @@
   //SvL: We don't care where the image is loaded for resource lookup
   if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED && loadType == REAL_LOAD) {
-        return allocFixedMem(reservedMem);
+    	return allocFixedMem(reservedMem);
   }
   rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | flAllocMem);
   if(rc) {
-    dprintf((LOG, "Win32PeLdrImage::allocSections, DosAllocMem returned %d", rc));
-        errorState = rc;
-        return(FALSE);
+	dprintf((LOG, "Win32PeLdrImage::allocSections, DosAllocMem returned %d", rc));
+    	errorState = rc;
+    	return(FALSE);
   }
   realBaseAddress = baseAddress;
@@ -695,7 +696,7 @@
 {
   for(int i=0;i<nrsections;i++) {
-    if(section[i].type == type) {
-        return &section[i];
-    }
+	if(section[i].type == type) {
+		return &section[i];
+	}
   }
   return NULL;
@@ -706,7 +707,7 @@
 {
   for(int i=0;i<nrsections;i++) {
-    if(section[i].virtaddr <= addr && section[i].virtaddr + section[i].virtualsize > addr) {
-        return &section[i];
-    }
+	if(section[i].virtaddr <= addr && section[i].virtaddr + section[i].virtualsize > addr) {
+		return &section[i];
+	}
   }
   return NULL;
@@ -717,7 +718,7 @@
 {
   for(int i=0;i<nrsections;i++) {
-    if(section[i].realvirtaddr <= addr && section[i].realvirtaddr + section[i].virtualsize > addr) {
-        return &section[i];
-    }
+	if(section[i].realvirtaddr <= addr && section[i].realvirtaddr + section[i].virtualsize > addr) {
+		return &section[i];
+	}
   }
   return NULL;
@@ -731,13 +732,13 @@
 
   for(int i=0;i<nrsections;i++) {
-    if(section[i].realvirtaddr > addr) {
-        if(section[i].realvirtaddr < lowestAddr) {
-            lowestAddr = section[i].realvirtaddr;
-            index = i;
-        }
-    }
+	if(section[i].realvirtaddr > addr) {
+		if(section[i].realvirtaddr < lowestAddr) {
+			lowestAddr = section[i].realvirtaddr;
+			index = i;
+		}
+	}
   }
   if(index == -1)
-    return NULL;
+  	return NULL;
 
   return &section[index];
@@ -761,6 +762,6 @@
   //Allocated in peldr.dll
   if(reservedMem && reservedMem == oh.ImageBase) {
-    realBaseAddress = oh.ImageBase;
-    return TRUE;
+	realBaseAddress = oh.ImageBase;
+	return TRUE;
   }
 
@@ -768,46 +769,46 @@
   memallocs = (ULONG *)malloc(4096*sizeof(ULONG *));
   if(memallocs == NULL) {
-    dprintf((LOG, "allocFixedMem: MALLOC FAILED for memallocs" ));
-    return FALSE;
+	dprintf((LOG, "allocFixedMem: MALLOC FAILED for memallocs" ));
+	return FALSE;
   }
 
   if(oh.ImageBase < 512*1024*1024) {
-    allocFlags = 0;
+	allocFlags = 0;
   }
   while(TRUE) {
-        rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
-        if(rc) break;
-
-        dprintf((LOG, "DosAllocMem returned %x", address ));
-        if(address + FALLOC_SIZE >= oh.ImageBase) {
-            if(address > oh.ImageBase) {//we've passed it!
-                    DosFreeMem((PVOID)address);
-                    break;
-            }
-            //found the right address
-            DosFreeMem((PVOID)address);
-
-            diff = oh.ImageBase - address;
-            if(diff) {
-                    rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
-                    if(rc) break;
-            }
-            rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
-            if(rc) break;
-
-            if(diff) DosFreeMem((PVOID)address);
-
-            realBaseAddress = baseAddress;
-            break;
-        }
-    memallocs[alloccnt++] = address;
+    	rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
+    	if(rc) break;
+
+    	dprintf((LOG, "DosAllocMem returned %x", address ));
+    	if(address + FALLOC_SIZE >= oh.ImageBase) {
+        	if(address > oh.ImageBase) {//we've passed it!
+            		DosFreeMem((PVOID)address);
+            		break;
+        	}
+        	//found the right address
+        	DosFreeMem((PVOID)address);
+
+        	diff = oh.ImageBase - address;
+        	if(diff) {
+            		rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
+            		if(rc) break;
+        	}
+        	rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
+        	if(rc) break;
+
+        	if(diff) DosFreeMem((PVOID)address);
+
+        	realBaseAddress = baseAddress;
+        	break;
+    	}
+	memallocs[alloccnt++] = address;
   }
   for(i=0;i<alloccnt;i++) {
-        DosFreeMem((PVOID)memallocs[i]);
+    	DosFreeMem((PVOID)memallocs[i]);
   }
   free(memallocs);
 
   if(realBaseAddress == 0) //Let me guess.. MS Office app?
-        return(FALSE);
+    	return(FALSE);
 
   return(TRUE);
@@ -827,28 +828,28 @@
   // Process all the image sections
   for(i=0;i<nrsections;i++) {
-        section[i].realvirtaddr = realBaseAddress + (section[i].virtaddr - oh.ImageBase);
+    	section[i].realvirtaddr = realBaseAddress + (section[i].virtaddr - oh.ImageBase);
   }
 
   for(i=0;i<nrsections;i++) {
-        switch(section[i].type)
-    {
+    	switch(section[i].type)
+	{
         case SECTION_CODE:
         case (SECTION_CODE | SECTION_IMPORT):
-                section[i].pageflags = PAG_EXECUTE | PAG_READ;
-        if(section[i].flags & IMAGE_SCN_MEM_WRITE)
-            section[i].pageflags |= PAG_WRITE;
-                break;
+            	section[i].pageflags = PAG_EXECUTE | PAG_READ;
+		if(section[i].flags & IMAGE_SCN_MEM_WRITE) 
+			section[i].pageflags |= PAG_WRITE;
+            	break;
         case SECTION_INITDATA:
         case SECTION_UNINITDATA:
         case SECTION_IMPORT: //TODO: read only?
-                section[i].pageflags = PAG_WRITE | PAG_READ;
-                break;
+            	section[i].pageflags = PAG_WRITE | PAG_READ;
+            	break;
         case SECTION_READONLYDATA:
         case SECTION_RESOURCE:
-    case SECTION_TLS:
+	case SECTION_TLS:
         default:
-                section[i].pageflags = PAG_READ;
-                break;
-        }
+            	section[i].pageflags = PAG_READ;
+            	break;
+    	}
   }
   return(TRUE);
@@ -865,5 +866,5 @@
 
   if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
-        return(TRUE);
+    	return(TRUE);
   }
 
@@ -872,79 +873,79 @@
   size  = (size-1) & ~0xFFF;
   size += PAGE_SIZE;
-
+ 
   if(prel) {
-    j = 1;
-    while(prel->VirtualAddress && prel->VirtualAddress < virtAddress) {
-            prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
-    }
-        while(prel->VirtualAddress && prel->VirtualAddress < virtAddress + size) {
-            page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
-            count  = (prel->SizeOfBlock - 8)/2;
-            j++;
-            for(i=0;i<count;i++) {
-                int type   = prel->TypeOffset[i] >> 12;
-                int offset = prel->TypeOffset[i] & 0xFFF;
-            int fixupsize = 0;
-
-                switch(type)
-            {
-                case IMAGE_REL_BASED_HIGHLOW:
-                fixupsize = 4;
-                break;
-                    case IMAGE_REL_BASED_HIGH:
-                    case IMAGE_REL_BASED_LOW:
-                fixupsize = 2;
-                break;
-            }
-            //If the fixup crosses the final page boundary,
-            //then we have to load another page
-            if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
-            {
-                newpage  = realBaseAddress + prel->VirtualAddress + offset + fixupsize;
-                newpage &= ~0xFFF;
-
-                    section  = findSectionByOS2Addr(newpage);
-                    if(section == NULL) {
-                    //should never happen
-                    dprintf((LOG, "::setFixups -> section == NULL!!"));
-                    return FALSE;
-                    }
-                //SvL: Read page from disk
-                commitPage(newpage, FALSE, SINGLE_PAGE);
-
-                    //SvL: Enable write access
-                    DosSetMem((PVOID)newpage, PAGE_SIZE, PAG_READ|PAG_WRITE);
-            }
-
-                switch(type)
-            {
-                case IMAGE_REL_BASED_ABSOLUTE:
-                    break;  //skip
-                case IMAGE_REL_BASED_HIGHLOW:
-                        AddOff32Fixup(prel->VirtualAddress + offset);
-                        break;
-                    case IMAGE_REL_BASED_HIGH:
-                AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
-                break;
-                    case IMAGE_REL_BASED_LOW:
-                AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
-                break;
-                    case IMAGE_REL_BASED_HIGHADJ:
-                    case IMAGE_REL_BASED_MIPS_JMPADDR:
-                default:
-                        break;
-                }
-            if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
-            {
-                    //SvL: Restore original page protection flags
-                    DosSetMem((PVOID)newpage, PAGE_SIZE, section->pageflags);
-            }
-            }
-            prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
-        }//while
+ 	j = 1;
+	while(prel->VirtualAddress && prel->VirtualAddress < virtAddress) {
+      		prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
+	}
+    	while(prel->VirtualAddress && prel->VirtualAddress < virtAddress + size) {
+      		page = (char *)((char *)prel + (ULONG)prel->VirtualAddress);
+      		count  = (prel->SizeOfBlock - 8)/2;
+      		j++;
+      		for(i=0;i<count;i++) {
+		        int type   = prel->TypeOffset[i] >> 12;
+		        int offset = prel->TypeOffset[i] & 0xFFF;
+			int fixupsize = 0;
+
+		        switch(type)
+			{
+		        case IMAGE_REL_BASED_HIGHLOW:
+				fixupsize = 4;
+				break;
+            		case IMAGE_REL_BASED_HIGH:
+            		case IMAGE_REL_BASED_LOW:
+				fixupsize = 2;
+				break;
+			}
+			//If the fixup crosses the final page boundary,
+			//then we have to load another page
+			if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
+			{
+				newpage  = realBaseAddress + prel->VirtualAddress + offset + fixupsize;
+				newpage &= ~0xFFF;
+
+    				section  = findSectionByOS2Addr(newpage);
+    				if(section == NULL) {
+					//should never happen
+					dprintf((LOG, "::setFixups -> section == NULL!!"));
+					return FALSE;
+    				}
+				//SvL: Read page from disk
+				commitPage(newpage, FALSE, SINGLE_PAGE);
+
+    				//SvL: Enable write access
+    				DosSetMem((PVOID)newpage, PAGE_SIZE, PAG_READ|PAG_WRITE);
+			}
+
+		        switch(type)
+			{
+		        case IMAGE_REL_BASED_ABSOLUTE:
+		        	break;  //skip
+		        case IMAGE_REL_BASED_HIGHLOW:
+		                AddOff32Fixup(prel->VirtualAddress + offset);
+		                break;
+            		case IMAGE_REL_BASED_HIGH:
+				AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
+				break;
+            		case IMAGE_REL_BASED_LOW:
+				AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
+				break;
+            		case IMAGE_REL_BASED_HIGHADJ:
+            		case IMAGE_REL_BASED_MIPS_JMPADDR:
+       			default:
+                		break;
+        		}
+			if(prel->VirtualAddress + offset + fixupsize > virtAddress + size)
+			{
+    				//SvL: Restore original page protection flags
+    				DosSetMem((PVOID)newpage, PAGE_SIZE, section->pageflags);
+			}
+      		}
+      		prel = (PIMAGE_BASE_RELOCATION)((char*)prel + prel->SizeOfBlock);
+    	}//while
   }
   else {
-    dprintf((LOG, "Win32PeLdrImage::setFixups, no fixups at %x, %d", virtAddress, size));
-        return(FALSE);
+	dprintf((LOG, "Win32PeLdrImage::setFixups, no fixups at %x, %d", virtAddress, size));
+    	return(FALSE);
   }
   return(TRUE);
@@ -959,5 +960,5 @@
 
   if(fh.Characteristics & IMAGE_FILE_RELOCS_STRIPPED) {
-        return(TRUE);
+    	return(TRUE);
   }
 
@@ -981,9 +982,9 @@
                 break;
             case IMAGE_REL_BASED_HIGH:
-        AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
-        break;
+		AddOff16Fixup(prel->VirtualAddress + offset, TRUE);
+		break;
             case IMAGE_REL_BASED_LOW:
-        AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
-        break;
+		AddOff16Fixup(prel->VirtualAddress + offset, FALSE);
+		break;
             case IMAGE_REL_BASED_HIGHADJ:
             case IMAGE_REL_BASED_MIPS_JMPADDR:
@@ -1025,9 +1026,9 @@
   orgaddr = *fixup;
   if(fHighFixup) {
-    *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
+  	*fixup  += (USHORT)((realBaseAddress - oh.ImageBase) >> 16);
 //        dprintf((LOG, "AddOff16FixupH 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
   }
   else {
-    *fixup  += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
+  	*fixup  += (USHORT)((realBaseAddress - oh.ImageBase) & 0xFFFF);
 //        dprintf((LOG, "AddOff16FixupL 0x%x org 0x%x -> new 0x%x", fixup, orgaddr, *fixup));
   }
@@ -1044,10 +1045,10 @@
   if(apiaddr == 0)
   {
-        dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n",
+    	dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%u not found\n",
                  WinDll->getName(),
                  ordinal));
 
-        dprintf((LOG, "--->>> NOT FOUND!" ));
-        *import = (ULONG)MissingApi;
+    	dprintf((LOG, "--->>> NOT FOUND!" ));
+    	*import = (ULONG)MissingApi;
   }
   else  *import = apiaddr;
@@ -1064,10 +1065,10 @@
   if(apiaddr == 0)
   {
-        dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n",
+    	dprintf((LOG, "KERNEL32:Win32PeLdrImage - %s.%s not found\n",
                 WinDll->getName(),
                 impname));
 
-        dprintf((LOG, "--->>> NOT FOUND!" ));
-        *import = (ULONG)MissingApi;
+    	dprintf((LOG, "--->>> NOT FOUND!" ));
+    	*import = (ULONG)MissingApi;
   }
   else  *import = apiaddr;
@@ -1149,16 +1150,16 @@
 
   if(nameexports == NULL) {
-        nameExportSize= 4096;
-        nameexports   = (NameExport *)malloc(nameExportSize);
-        curnameexport = nameexports;
+    	nameExportSize= 4096;
+    	nameexports   = (NameExport *)malloc(nameExportSize);
+    	curnameexport = nameexports;
   }
   nsize = (ULONG)curnameexport - (ULONG)nameexports;
   if(nsize + sizeof(NameExport) + strlen(apiname) > nameExportSize) {
-        nameExportSize += 4096;
-        char *tmp = (char *)nameexports;
-        nameexports = (NameExport *)malloc(nameExportSize);
-        memcpy(nameexports, tmp, nsize);
-        curnameexport = (NameExport *)((ULONG)nameexports + nsize);
-        free(tmp);
+    	nameExportSize += 4096;
+    	char *tmp = (char *)nameexports;
+    	nameexports = (NameExport *)malloc(nameExportSize);
+    	memcpy(nameexports, tmp, nsize);
+    	curnameexport = (NameExport *)((ULONG)nameexports + nsize);
+    	free(tmp);
   }
   curnameexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
@@ -1169,5 +1170,5 @@
   curnameexport->nlength = strlen(apiname) + 1;
   if(curnameexport->nlength < sizeof(curnameexport->name))
-        curnameexport->nlength = sizeof(curnameexport->name);
+    	curnameexport->nlength = sizeof(curnameexport->name);
 
   curnameexport = (NameExport *)((ULONG)curnameexport->name + curnameexport->nlength);
@@ -1178,6 +1179,6 @@
 {
   if(ordexports == NULL) {
-        ordexports   = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport));
-        curordexport = ordexports;
+    	ordexports   = (OrdExport *)malloc(nrOrdExports * sizeof(OrdExport));
+    	curordexport = ordexports;
   }
   curordexport->virtaddr = realBaseAddress + (virtaddr - oh.ImageBase);
@@ -1329,82 +1330,82 @@
     if(WinDll == NULL)
     {  //not found, so load it
-    char modname[CCHMAXPATH];
-
-    strcpy(modname, pszCurModule);
-    //rename dll if necessary (i.e. OLE32 -> OLE32OS2)
-    Win32DllBase::renameDll(modname);
-
-    if(isPEImage(modname) == FALSE)
-    {//LX image, so let OS/2 do all the work for us
-        APIRET rc;
-        char   szModuleFailure[CCHMAXPATH] = "";
-        ULONG  hInstanceNewDll;
-
-        char *dot = strchr(modname, '.');
-        if(dot) {
-            *dot = 0;
-        }
-        strcat(modname, ".DLL");
-        rc = DosLoadModule(szModuleFailure, sizeof(szModuleFailure), modname, (HMODULE *)&hInstanceNewDll);
-        if(rc) {
-            dprintf((LOG, "DosLoadModule returned %X for %s\n", rc, szModuleFailure));
-            sprintf(szErrorModule, "%s.DLL", szModuleFailure);
-            errorState = rc;
-            return(FALSE);
-        }
-        WinDll = (Win32PeLdrDll *)Win32DllBase::findModule(hInstanceNewDll);
-        if(WinDll == NULL) {//shouldn't happen!
-            dprintf((LOG, "Just loaded the dll, but can't find it anywhere?!!?"));
-            errorState = ERROR_INTERNAL;
-            return(FALSE);
-        }
-        //Mark this dll as loaded by DosLoadModule
-        WinDll->setLoadLibrary();
-        WinDll->AddRef();
-    }
-    else {
-            WinDll = new Win32PeLdrDll(modname, this);
-
-            if(WinDll == NULL) {
-                dprintf((LOG, "WinDll: Error allocating memory" ));
-                WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
-                errorState = ERROR_INTERNAL;
-                return(FALSE);
-            }
-            dprintf((LOG, "**********************************************************************" ));
-            dprintf((LOG, "**********************     Loading Module        *********************" ));
-            dprintf((LOG, "**********************************************************************" ));
-            if(WinDll->init(0) == FALSE) {
-                dprintf((LOG, "Internal WinDll error ", WinDll->getError() ));
-                return(FALSE);
-            }
+	char modname[CCHMAXPATH];
+
+	strcpy(modname, pszCurModule);
+  	//rename dll if necessary (i.e. OLE32 -> OLE32OS2)
+  	Win32DllBase::renameDll(modname);
+
+	if(isPEImage(modname) == FALSE)
+	{//LX image, so let OS/2 do all the work for us
+		APIRET rc;
+  		char   szModuleFailure[CCHMAXPATH] = "";
+		ULONG  hInstanceNewDll;
+
+		char *dot = strchr(modname, '.');
+		if(dot) {
+			*dot = 0;
+		}
+		strcat(modname, ".DLL");
+  		rc = DosLoadModule(szModuleFailure, sizeof(szModuleFailure), modname, (HMODULE *)&hInstanceNewDll);
+  		if(rc) {
+			dprintf((LOG, "DosLoadModule returned %X for %s\n", rc, szModuleFailure));
+			sprintf(szErrorModule, "%s.DLL", szModuleFailure);
+			errorState = rc;
+			return(FALSE);
+  		}
+		WinDll = (Win32PeLdrDll *)Win32DllBase::findModule(hInstanceNewDll);
+		if(WinDll == NULL) {//shouldn't happen!
+			dprintf((LOG, "Just loaded the dll, but can't find it anywhere?!!?"));
+			errorState = ERROR_INTERNAL;
+			return(FALSE);
+		}
+		//Mark this dll as loaded by DosLoadModule
+  		WinDll->setLoadLibrary();
+		WinDll->AddRef();
+	}
+	else {
+        	WinDll = new Win32PeLdrDll(modname, this);
+
+        	if(WinDll == NULL) {
+	            dprintf((LOG, "WinDll: Error allocating memory" ));
+	            WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szMemErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
+	            errorState = ERROR_INTERNAL;
+	            return(FALSE);
+	        }
+	        dprintf((LOG, "**********************************************************************" ));
+	        dprintf((LOG, "**********************     Loading Module        *********************" ));
+	        dprintf((LOG, "**********************************************************************" ));
+	        if(WinDll->init(0) == FALSE) {
+	            dprintf((LOG, "Internal WinDll error ", WinDll->getError() ));
+	            return(FALSE);
+	        }
 #ifdef DEBUG
-            WinDll->AddRef(getModuleName());
+    		WinDll->AddRef(getModuleName());
 #else
-            WinDll->AddRef();
+    		WinDll->AddRef();
 #endif
-            if(WinDll->attachProcess() == FALSE) {
-                dprintf((LOG, "attachProcess failed!" ));
-            delete WinDll;
-                errorState = ERROR_INTERNAL;
-                return(FALSE);
-            }
-    }
-
-    dprintf((LOG, "**********************************************************************" ));
-    dprintf((LOG, "**********************  Finished Loading Module  *********************" ));
+	        if(WinDll->attachProcess() == FALSE) {
+        	    dprintf((LOG, "attachProcess failed!" ));
+		    delete WinDll;
+	            errorState = ERROR_INTERNAL;
+	            return(FALSE);
+	        }
+	}
+
+	dprintf((LOG, "**********************************************************************" ));
+	dprintf((LOG, "**********************  Finished Loading Module  *********************" ));
         dprintf((LOG, "**********************************************************************" ));
     }
     else {
-    if(WinDll->isLxDll() && !WinDll->isLoaded()) {
-        //can happen with i.e. wininet
-        //wininet depends on wsock32; when the app loads wsock32 afterwards
-        //with LoadLibrary or as a child of another dll, we need to make
+	if(WinDll->isLxDll() && !WinDll->isLoaded()) {
+		//can happen with i.e. wininet
+		//wininet depends on wsock32; when the app loads wsock32 afterwards
+	  	//with LoadLibrary or as a child of another dll, we need to make
                 //sure it's loaded once with DosLoadModule
-        WinDll->loadLibrary();
-    }
-    WinDll->AddRef();
-
-    dprintf((LOG, "Already found ", pszCurModule));
+		WinDll->loadLibrary();
+	}
+	WinDll->AddRef();
+	
+	dprintf((LOG, "Already found ", pszCurModule));
     }
     //add the dll we just loaded to dependency list for this image
@@ -1422,6 +1423,6 @@
     section    = findSectionByOS2Addr(ulCurFixup);
     if(section == NULL) {
-    dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
-    return FALSE;
+	dprintf((LOG, "Unable to find section for %x", ulCurFixup ));
+	return FALSE;
     }
     //SvL: Read page from disk
@@ -1452,9 +1453,9 @@
         ulCurFixup += sizeof(IMAGE_THUNK_DATA);
         j++;
-    if((ulCurFixup & 0xfff) == 0) {
-            commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
-            DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
-            nrPages++;
-    }
+	if((ulCurFixup & 0xfff) == 0) {
+    		commitPage(ulCurFixup & ~0xfff, FALSE, SINGLE_PAGE);
+    		DosSetMem((PVOID)(ulCurFixup & ~0xfff), PAGE_SIZE, PAG_READ|PAG_WRITE);
+    		nrPages++;
+	}
     }
     //SvL: And restore original protection flags
@@ -1485,10 +1486,10 @@
   if(apilen < 4)
   {
-    *(ULONG *)tmp = 0;
-    strcpy(tmp, name);
-    apiname = tmp;
-    apilen  = 4;
-  }
-  else  apiname = name;
+	*(ULONG *)tmp = 0;
+	strcpy(tmp, name);
+	apiname = tmp;
+	apilen  = 4;
+  }
+  else	apiname = name;
 
   curexport = nameexports;
@@ -1498,6 +1499,6 @@
        *(ULONG *)curexport->name == *(ULONG *)apiname)
     {
-        if(strcmp(curexport->name, apiname) == 0)
-            return(curexport->virtaddr);
+      	if(strcmp(curexport->name, apiname) == 0)
+        	return(curexport->virtaddr);
     }
     curexport = (NameExport *)((ULONG)curexport->name + curexport->nlength);
@@ -1515,15 +1516,15 @@
   curexport = ordexports;
   for(i=0;i<nrOrdExports;i++) {
-    if(curexport->ordinal == ordinal)
-        return(curexport->virtaddr);
-    curexport++;
+	if(curexport->ordinal == ordinal)
+		return(curexport->virtaddr);
+	curexport++;
   }
   //Name exports also contain an ordinal, so check this
   nexport = nameexports;
   for(i=0;i<nrNameExports;i++) {
-    if(nexport->ordinal == ordinal)
-        return(nexport->virtaddr);
-
-    nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
+	if(nexport->ordinal == ordinal)
+		return(nexport->virtaddr);
+
+	nexport = (NameExport *)((ULONG)nexport->name + nexport->nlength);
   }
   return(0);
@@ -1554,5 +1555,5 @@
 
   if( r != MBID_IGNORE )
-    ExitProcess(987);
+	ExitProcess(987);
 
   fIgnore = TRUE;
Index: trunk/src/kernel32/wprocess.cpp
===================================================================
--- trunk/src/kernel32/wprocess.cpp	(revision 3402)
+++ trunk/src/kernel32/wprocess.cpp	(revision 3404)
@@ -1,3 +1,3 @@
-/* $Id: wprocess.cpp,v 1.78 2000-04-16 07:07:01 bird Exp $ */
+/* $Id: wprocess.cpp,v 1.79 2000-04-16 10:42:13 sandervl Exp $ */
 
 /*
@@ -12,10 +12,4 @@
  *
  */
-
-
-
-/*******************************************************************************
-*   Header Files                                                               *
-*******************************************************************************/
 #include <odin.h>
 #include <odinwrap.h>
@@ -55,7 +49,6 @@
 
 
-/******************************************************************************
-*   Global Variables                                                          *
-******************************************************************************/
+//******************************************************************************
+//******************************************************************************
 BOOL      fFreeLibrary = FALSE;
 BOOL      fIsOS2Image = FALSE;  //TRUE  -> Odin32 OS/2 application (not converted!)
@@ -69,7 +62,4 @@
 static THDB     *threadList = 0;
 static VMutex    threadListMutex;
-
-
-
 //******************************************************************************
 //******************************************************************************
@@ -365,6 +355,6 @@
 VOID WIN32API ExitProcess(DWORD exitcode)
 {
-    dprintf(("KERNEL32: ExitProcess %d\n", exitcode));
-    dprintf(("KERNEL32: ExitProcess FS = %x\n", GetFS()));
+    dprintf(("KERNEL32:  ExitProcess %d\n", exitcode));
+    dprintf(("KERNEL32:  ExitProcess FS = %x\n", GetFS()));
 
     SetOS2ExceptionChain(-1);
@@ -425,25 +415,5 @@
 }
 /******************************************************************************/
-//******************************************************************************
-HINSTANCE16 WIN32API LoadLibrary16(LPCTSTR lpszLibFile)
-{
-    dprintf(("ERROR: LoadLibrary16 %s, not implemented", lpszLibFile));
-    return 0;
-}
-//******************************************************************************
-//******************************************************************************
-VOID WIN32API FreeLibrary16(HINSTANCE16 hinstance)
-{
-    dprintf(("ERROR: FreeLibrary16 %x, not implemented", hinstance));
-}
-//******************************************************************************
-//******************************************************************************
-FARPROC WIN32API GetProcAddress16(HMODULE hModule, LPCSTR lpszProc)
-{
-    dprintf(("ERROR: GetProcAddress16 %x %x, not implemented", hModule, lpszProc));
-    return 0;
-}
-//******************************************************************************
-
+/******************************************************************************/
 /**
  * LoadLibraryA can be used to map a DLL module into the calling process's
@@ -676,4 +646,5 @@
                  lpszLibFile, hFile, dwFlags));
         SetLastError(ERROR_INVALID_PARAMETER);
+	return NULL;
     }
 
@@ -990,4 +961,24 @@
 //******************************************************************************
 //******************************************************************************
+HINSTANCE16 WIN32API LoadLibrary16(LPCTSTR lpszLibFile)
+{
+    dprintf(("ERROR: LoadLibrary16 %s, not implemented", lpszLibFile));
+    return 0;
+}
+//******************************************************************************
+//******************************************************************************
+VOID WIN32API FreeLibrary16(HINSTANCE16 hinstance)
+{
+    dprintf(("ERROR: FreeLibrary16 %x, not implemented", hinstance));
+}
+//******************************************************************************
+//******************************************************************************
+FARPROC WIN32API GetProcAddress16(HMODULE hModule, LPCSTR lpszProc)
+{
+    dprintf(("ERROR: GetProcAddress16 %x %x, not implemented", hModule, lpszProc));
+    return 0;
+}
+//******************************************************************************
+//******************************************************************************
 LPCSTR WIN32API GetCommandLineA()
 {
@@ -1000,6 +991,6 @@
     cmdline = O32_GetCommandLine();
 
-  dprintf(("KERNEL32: GetCommandLine %s\n", cmdline));
-  dprintf(("KERNEL32: FS = %x\n", GetFS()));
+  dprintf(("KERNEL32:  GetCommandLine %s\n", cmdline));
+  dprintf(("KERNEL32:  FS = %x\n", GetFS()));
   return(cmdline);
 }
@@ -1011,5 +1002,5 @@
          char *asciicmdline = NULL;
 
-    dprintf(("KERNEL32: FS = %x\n", GetFS()));
+    dprintf(("KERNEL32:  FS = %x\n", GetFS()));
 
     if(UnicodeCmdLine)
@@ -1026,8 +1017,8 @@
         UnicodeCmdLine = (WCHAR *)malloc(strlen(asciicmdline)*2 + 2);
         AsciiToUnicode(asciicmdline, UnicodeCmdLine);
-        dprintf(("KERNEL32: OS2GetCommandLineW: %s\n", asciicmdline));
+        dprintf(("KERNEL32:  OS2GetCommandLineW: %s\n", asciicmdline));
         return(UnicodeCmdLine);
     }
-    dprintf(("KERNEL32: OS2GetCommandLineW: asciicmdline == NULL\n"));
+    dprintf(("KERNEL32:  OS2GetCommandLineW: asciicmdline == NULL\n"));
     return NULL;
 }
@@ -1070,5 +1061,5 @@
  DWORD rc;
 
-    dprintf(("KERNEL32: OSLibGetModuleFileNameW\n"));
+    dprintf(("KERNEL32:  OSLibGetModuleFileNameW\n"));
     rc = GetModuleFileNameA(hModule, asciifilename, nSize);
     if(rc)      AsciiToUnicode(asciifilename, lpFileName);
@@ -1121,5 +1112,5 @@
   }
 
-  dprintf(("KERNEL32: GetModuleHandle %s returned %X\n", lpszModule, hMod));
+  dprintf(("KERNEL32:  GetModuleHandle %s returned %X\n", lpszModule, hMod));
   return(hMod);
 }
@@ -1133,5 +1124,5 @@
     astring = UnicodeToAsciiString((LPWSTR)arg1);
     rc = GetModuleHandleA(astring);
-    dprintf(("KERNEL32: OS2GetModuleHandleW %s returned %X\n", astring, rc));
+    dprintf(("KERNEL32:  OS2GetModuleHandleW %s returned %X\n", astring, rc));
     FreeAsciiString(astring);
     return(rc);
@@ -1203,5 +1194,5 @@
         sprintf(cmdline, "PE.EXE %s", lpCommandLine);
     }
-    dprintf(("KERNEL32: CreateProcess %s\n", cmdline));
+    dprintf(("KERNEL32:  CreateProcess %s\n", cmdline));
     rc = O32_CreateProcess("PE.EXE", (LPCSTR)cmdline,lpProcessAttributes,
                          lpThreadAttributes, bInheritHandles, dwCreationFlags,
@@ -1229,9 +1220,9 @@
 
     if(lpProcessInfo)
-      dprintf(("KERNEL32: CreateProcess returned %d hPro:%x hThr:%x pid:%x tid:%x\n",
+      dprintf(("KERNEL32:  CreateProcess returned %d hPro:%x hThr:%x pid:%x tid:%x\n",
                rc, lpProcessInfo->hProcess, lpProcessInfo->hThread,
                    lpProcessInfo->dwProcessId,lpProcessInfo->dwThreadId));
     else
-      dprintf(("KERNEL32: CreateProcess returned %d\n", rc));
+      dprintf(("KERNEL32:  CreateProcess returned %d\n", rc));
     return(rc);
 }
@@ -1296,5 +1287,9 @@
  ULONG     ulAPIOrdinal;
 
-  winmod = Win32ImageBase::findModule(hModule);
+  if(hModule == 0 || hModule == -1 || (WinExe && hModule == WinExe->getInstanceHandle())) {
+    winmod = WinExe;
+  }
+  else  winmod = (Win32ImageBase *)Win32DllBase::findModule((HINSTANCE)hModule);
+
   if(winmod) {
         ulAPIOrdinal = (ULONG)lpszProc;
@@ -1310,6 +1305,6 @@
   proc = O32_GetProcAddress(hModule, lpszProc);
   if(HIWORD(lpszProc))
-    dprintf(("KERNEL32: GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
-  else  dprintf(("KERNEL32: GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
+    dprintf(("KERNEL32:  GetProcAddress %s from %X returned %X\n", lpszProc, hModule, proc));
+  else  dprintf(("KERNEL32:  GetProcAddress %x from %X returned %X\n", lpszProc, hModule, proc));
   return(proc);
 }
