Index: trunk/src/kernel32/HandleManager.cpp
===================================================================
--- trunk/src/kernel32/HandleManager.cpp	(revision 10071)
+++ trunk/src/kernel32/HandleManager.cpp	(revision 10073)
@@ -1,3 +1,3 @@
-/* $Id: HandleManager.cpp,v 1.101 2003-05-06 10:12:00 sandervl Exp $ */
+/* $Id: HandleManager.cpp,v 1.102 2003-05-06 12:06:07 sandervl Exp $ */
 
 /*
@@ -1406,4 +1406,54 @@
 
 /*****************************************************************************
+ * Name      : HANDLE  HMGetFileNameFromHandle
+ * Purpose   : Query the name of the file associated with the system handle (if any)
+ * Parameters:
+ * Variables :
+ * Result    :
+ * Remark    :
+ * Status    :
+ *
+ * Author    : SvL
+ *****************************************************************************/
+BOOL HMGetFileNameFromHandle(HANDLE hObject, LPSTR lpszFileName, DWORD cbFileName)
+{
+  int       iIndex;                           /* index into the handle table */
+  BOOL      fResult;       /* result from the device handler's CloseHandle() */
+  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
+
+
+  if(lpszFileName == NULL) {
+      DebugInt3();
+      SetLastError(ERROR_INVALID_PARAMETER);
+      return FALSE;
+  }
+                                                          /* validate handle */
+  iIndex = _HMHandleQuery(hObject);                         /* get the index */
+  if (-1 == iIndex)                                               /* error ? */
+  {
+    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
+    return (FALSE);                                        /* signal failure */
+  }
+
+  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
+  //SvL: Check if pDeviceHandler is set
+  if (pHMHandle->pDeviceHandler)
+  {
+    fResult = pHMHandle->pDeviceHandler->GetFileNameFromHandle(&pHMHandle->hmHandleData,
+                                                               lpszFileName, cbFileName);
+  }
+  else
+  {
+    dprintf(("HMGetFileNameFromHandle(%08xh): pDeviceHandler not set", hObject));
+    fResult = FALSE;
+  }
+
+  if(fResult == TRUE) {
+      SetLastError(ERROR_SUCCESS);
+  }
+  return (fResult);                                   /* deliver return code */
+}
+
+/*****************************************************************************
  * Name      : HANDLE  HMCloseFile
  * Purpose   : Wrapper for the CloseHandle() API
Index: trunk/src/kernel32/hmdevice.cpp
===================================================================
--- trunk/src/kernel32/hmdevice.cpp	(revision 10071)
+++ trunk/src/kernel32/hmdevice.cpp	(revision 10073)
@@ -1,3 +1,3 @@
-/* $Id: hmdevice.cpp,v 1.35 2003-04-02 12:58:29 sandervl Exp $ */
+/* $Id: hmdevice.cpp,v 1.36 2003-05-06 12:06:09 sandervl Exp $ */
 
 /*
@@ -1920,2 +1920,22 @@
     return WAIT_FAILED;
 }
+
+
+/*****************************************************************************
+ * Name      : BOOL HMDeviceHandler::GetFileNameFromHandle
+ * Purpose   :
+ * Variables :
+ * Result    :
+ * Remark    :
+ * Status    :
+ *
+ * Author    : SvL
+ *****************************************************************************/
+BOOL HMDeviceHandler::GetFileNameFromHandle(PHMHANDLEDATA pHMHandleData, LPSTR lpszFileName, DWORD cbFileName)
+{
+    dprintf(("KERNEL32: ERROR: HandleManager::DeviceHandler::GetFileNameFromHandle %08x %x %d",
+              pHMHandleData->hHMHandle, lpszFileName, cbFileName));
+
+    return FALSE;
+}
+
Index: trunk/src/kernel32/hmdevice.h
===================================================================
--- trunk/src/kernel32/hmdevice.h	(revision 10071)
+++ trunk/src/kernel32/hmdevice.h	(revision 10073)
@@ -1,3 +1,3 @@
-/* $Id: hmdevice.h,v 1.34 2003-02-04 11:28:56 sandervl Exp $ */
+/* $Id: hmdevice.h,v 1.35 2003-05-06 12:06:09 sandervl Exp $ */
 
 /*
@@ -465,4 +465,7 @@
   virtual BOOL SetMailslotInfo(PHMHANDLEDATA pHMHandleData,
                                DWORD  dwReadTimeout);
+
+  virtual BOOL GetFileNameFromHandle(PHMHANDLEDATA pHMHandleData, LPSTR lpszFileName, DWORD cbFileName);
+
 };
 
Index: trunk/src/kernel32/hmfile.cpp
===================================================================
--- trunk/src/kernel32/hmfile.cpp	(revision 10071)
+++ trunk/src/kernel32/hmfile.cpp	(revision 10073)
@@ -1,3 +1,3 @@
-/* $Id: hmfile.cpp,v 1.44 2003-05-06 10:12:00 sandervl Exp $ */
+/* $Id: hmfile.cpp,v 1.45 2003-05-06 12:06:09 sandervl Exp $ */
 
 /*
@@ -1083,4 +1083,28 @@
 //                                 arg4));
 }
+/******************************************************************************
+ * Name      : DWORD HMDeviceFileClass::GetFileNameFromHandle
+ * Purpose   : the name of the file associated with the system handle (if any)
+ * Parameters: PHMHANDLEDATA pHMHandleData
+ * Variables :
+ * Result    : BOOLEAN
+ * Remark    : 
+ * Status    :
+ *
+ * Author    : SvL
+ ******************************************************************************/
+BOOL HMDeviceFileClass::GetFileNameFromHandle(PHMHANDLEDATA pHMHandleData,
+                                              LPSTR lpszFileName, DWORD cbFileName)
+{
+  HMFileInfo *fileInfo = (HMFileInfo *)pHMHandleData->dwUserData;
+
+  if(fileInfo == NULL || strlen(fileInfo->lpszFileName) >= cbFileName) {
+      if(fileInfo) DebugInt3();
+      return FALSE;
+  }
+  strcpy(lpszFileName, fileInfo->lpszFileName);
+
+  return TRUE;
+}
 
 //******************************************************************************
@@ -1261,4 +1285,28 @@
 }
 /******************************************************************************
+ * Name      : DWORD HMDeviceFileClass::GetFileNameFromHandle
+ * Purpose   : the name of the file associated with the system handle (if any)
+ * Parameters: PHMHANDLEDATA pHMHandleData
+ * Variables :
+ * Result    : BOOLEAN
+ * Remark    : 
+ * Status    :
+ *
+ * Author    : SvL
+ ******************************************************************************/
+BOOL HMDeviceInfoFileClass::GetFileNameFromHandle(PHMHANDLEDATA pHMHandleData,
+                                                  LPSTR lpszFileName, DWORD cbFileName)
+{
+  HMFileInfo *fileInfo = (HMFileInfo *)pHMHandleData->dwUserData;
+
+  if(fileInfo == NULL || strlen(fileInfo->lpszFileName) >= cbFileName) {
+      if(fileInfo) DebugInt3();
+      return FALSE;
+  }
+  strcpy(lpszFileName, fileInfo->lpszFileName);
+
+  return TRUE;
+}
+/******************************************************************************
  * Name      : DWORD HMDeviceFileClass::GetFileType
  * Purpose   : determine the handle type
Index: trunk/src/kernel32/hmfile.h
===================================================================
--- trunk/src/kernel32/hmfile.h	(revision 10071)
+++ trunk/src/kernel32/hmfile.h	(revision 10073)
@@ -1,3 +1,3 @@
-/* $Id: hmfile.h,v 1.10 2003-05-05 10:51:05 sandervl Exp $ */
+/* $Id: hmfile.h,v 1.11 2003-05-06 12:06:10 sandervl Exp $ */
 
 /*
@@ -158,4 +158,5 @@
                                    BOOL          arg4);
 
+  virtual BOOL GetFileNameFromHandle(PHMHANDLEDATA pHMHandleData, LPSTR lpszFileName, DWORD cbFileName);
 };
 
@@ -187,4 +188,7 @@
   /* this is a handler method for calls to GetFileType() */
   virtual DWORD GetFileType (PHMHANDLEDATA pHMHandleData);
+
+  virtual BOOL GetFileNameFromHandle(PHMHANDLEDATA pHMHandleData, LPSTR lpszFileName, DWORD cbFileName);
+
 };
 
Index: trunk/src/kernel32/mmap.cpp
===================================================================
--- trunk/src/kernel32/mmap.cpp	(revision 10071)
+++ trunk/src/kernel32/mmap.cpp	(revision 10073)
@@ -1,3 +1,3 @@
-/* $Id: mmap.cpp,v 1.65 2003-04-02 11:03:31 sandervl Exp $ */
+/* $Id: mmap.cpp,v 1.66 2003-05-06 12:06:10 sandervl Exp $ */
 
 /*
@@ -75,5 +75,5 @@
 Win32MemMap::Win32MemMap(HANDLE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName)
                : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0), 
-                 image(0), pWriteBitmap(NULL)
+                 image(0), pWriteBitmap(NULL), lpszFileName(NULL)
 {
     DosEnterCriticalSection(&globalmapcritsect);
@@ -101,5 +101,5 @@
 Win32MemMap::Win32MemMap(Win32PeLdrImage *pImage, ULONG baseAddress, ULONG size)
                : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0), 
-                 image(0), pWriteBitmap(NULL)
+                 image(0), pWriteBitmap(NULL), lpszFileName(NULL)
 {
     DosEnterCriticalSection(&globalmapcritsect);
@@ -127,4 +127,8 @@
     if(hMemFile != -1)
     {
+        lpszFileName = (char *)_smalloc(MMAP_MAX_FILENAME_LENGTH);
+        if(HMGetFileNameFromHandle(hMemFile, lpszFileName, MMAP_MAX_FILENAME_LENGTH) == FALSE) {
+            return FALSE;
+        }
 #if 0
         if(DuplicateHandle(GetCurrentProcess(), hMemFile, GetCurrentProcess(),
@@ -181,4 +185,7 @@
     if(lpszMapName) {
         free(lpszMapName);
+    }
+    if(lpszFileName) {
+        free(lpszFileName);
     }
     if(pMapping && !image) {
@@ -917,5 +924,11 @@
 Win32MemMap *Win32MemMap::findMapByFile(HANDLE hFile)
 {
+  DWORD processId = GetCurrentProcessId();
+  char  szFileName[260];
+
   if(hFile == -1)
+    return NULL;
+
+  if(HMGetFileNameFromHandle(hFile, szFileName, sizeof(szFileName)) == FALSE)
     return NULL;
 
@@ -926,6 +939,10 @@
   {
     while(map) {
-        if(map->hOrgMemFile == hFile)
-            break;
+        //TODO: we currently don't support sharing file maps between processes
+        if(map->mProcessId == processId && map->lpszFileName) 
+        {
+            if(!strcmp(map->lpszFileName, szFileName))
+                break;
+        }
         map = map->next;
     }
Index: trunk/src/kernel32/mmap.h
===================================================================
--- trunk/src/kernel32/mmap.h	(revision 10071)
+++ trunk/src/kernel32/mmap.h	(revision 10073)
@@ -1,3 +1,3 @@
-/* $Id: mmap.h,v 1.28 2003-04-02 11:03:32 sandervl Exp $ */
+/* $Id: mmap.h,v 1.29 2003-05-06 12:06:11 sandervl Exp $ */
 
 /*
@@ -37,4 +37,6 @@
 
 #define MMAP_FLUSHVIEW_ALL		0xFFFFFFFF
+
+#define MMAP_MAX_FILENAME_LENGTH        260
 
 typedef enum
@@ -124,4 +126,5 @@
    HANDLE hMemFile;
    HANDLE hOrgMemFile;
+   LPSTR  lpszFileName;
    ULONG  mSize;
    ULONG  mProtFlags;
@@ -169,4 +172,5 @@
             Win32MemMap *parent;
 
+            HANDLE       hDupMemFile;
 private:
 };
Index: trunk/src/kernel32/mmapdup.cpp
===================================================================
--- trunk/src/kernel32/mmapdup.cpp	(revision 10071)
+++ trunk/src/kernel32/mmapdup.cpp	(revision 10073)
@@ -1,3 +1,3 @@
-/* $Id: mmapdup.cpp,v 1.2 2003-04-02 11:03:32 sandervl Exp $ */
+/* $Id: mmapdup.cpp,v 1.3 2003-05-06 12:06:11 sandervl Exp $ */
 
 /*
@@ -67,6 +67,7 @@
 {
     //copy values from original map
-    mSize    = parent->getMapSize();
-    hMemFile = parent->getFileHandle();
+    mSize       = parent->getMapSize();
+    //can't use hMemFile or else we'll close this file handle in the memmap dtor!!
+    hDupMemFile = parent->getFileHandle();
 
     //If the parent is a readonly map and we allow write access, then we must
@@ -127,5 +128,5 @@
              goto parmfail;
 
-    if(offset+size > mSize && (!(fdwAccess & FILE_MAP_WRITE) || hMemFile == -1))
+    if(offset+size > mSize && (!(fdwAccess & FILE_MAP_WRITE) || hDupMemFile == -1))
         goto parmfail;
 
