Index: /trunk/src/kWorker/kWorker.c
===================================================================
--- /trunk/src/kWorker/kWorker.c	(revision 2939)
+++ /trunk/src/kWorker/kWorker.c	(revision 2940)
@@ -815,4 +815,8 @@
 /** The current directory (referenced). */
 static PKFSOBJ      g_pCurDirObj = NULL;
+#ifdef KBUILD_OS_WINDOWS
+/** The windows system32 directory (referenced). */
+static PKFSDIR      g_pWinSys32 = NULL;
+#endif
 
 /** Verbosity level. */
@@ -2114,4 +2118,38 @@
 
 /**
+ * Lazily initializes the g_pWinSys32 variable.
+ */
+static PKFSDIR kwLdrResolveWinSys32(void)
+{
+    KFSLOOKUPERROR  enmError;
+    PKFSDIR         pWinSys32;
+
+    /* Get the path first. */
+    char            szSystem32[MAX_PATH];
+    if (GetSystemDirectoryA(szSystem32, sizeof(szSystem32)) >= sizeof(szSystem32))
+    {
+        kwErrPrintf("GetSystemDirectory failed: %u\n", GetLastError());
+        strcpy(szSystem32, "C:\\Windows\\System32");
+    }
+
+    /* Look it up and verify it. */
+    pWinSys32 = (PKFSDIR)kFsCacheLookupA(g_pFsCache, szSystem32, &enmError);
+    if (pWinSys32)
+    {
+        if (pWinSys32->Obj.bObjType == KFSOBJ_TYPE_DIR)
+        {
+            g_pWinSys32 = pWinSys32;
+            return pWinSys32;
+        }
+
+        kwErrPrintf("System directory '%s' isn't of 'DIR' type: %u\n", szSystem32, g_pWinSys32->Obj.bObjType);
+    }
+    else
+        kwErrPrintf("Failed to lookup system directory '%s': %u\n", szSystem32, enmError);
+    return NULL;
+}
+
+
+/**
  * Whether we can load this DLL natively or not.
  *
@@ -2119,6 +2157,7 @@
  * @param   pszFilename         The filename (no path).
  * @param   enmLocation         The location.
- */
-static KBOOL kwLdrModuleCanLoadNatively(const char *pszFilename, KWLOCATION enmLocation)
+ * @param   pszFullPath         The full filename and path.
+ */
+static KBOOL kwLdrModuleCanLoadNatively(const char *pszFilename, KWLOCATION enmLocation, const char *pszFullPath)
 {
     if (enmLocation == KWLOCATION_SYSTEM32)
@@ -2126,4 +2165,26 @@
     if (enmLocation == KWLOCATION_UNKNOWN_NATIVE)
         return K_TRUE;
+
+    /* If the location is unknown, we must check if it's some dynamic loading
+       of a SYSTEM32 DLL with a full path.  We do not want to load these ourselves! */
+    if (enmLocation == KWLOCATION_UNKNOWN)
+    {
+        PKFSDIR pWinSys32 = g_pWinSys32;
+        if (!pWinSys32)
+            pWinSys32 = kwLdrResolveWinSys32();
+        if (pWinSys32)
+        {
+            KFSLOOKUPERROR enmError;
+            PKFSOBJ pFsObj = kFsCacheLookupA(g_pFsCache, pszFullPath, &enmError);
+            if (pFsObj)
+            {
+                KBOOL fInWinSys32 = pFsObj->pParent == pWinSys32;
+                kFsCacheObjRelease(g_pFsCache, pFsObj);
+                if (fInWinSys32)
+                    return K_TRUE;
+            }
+        }
+    }
+
     return kHlpStrNICompAscii(pszFilename, TUPLE("msvc"))   == 0
         || kHlpStrNICompAscii(pszFilename, TUPLE("msdis"))  == 0
@@ -2218,5 +2279,5 @@
              */
             pszName = kHlpGetFilename(szNormPath);
-            if (kwLdrModuleCanLoadNatively(pszName, enmLocation))
+            if (kwLdrModuleCanLoadNatively(pszName, enmLocation, szNormPath))
                 pMod = kwLdrModuleCreateNative(szNormPath, uHashPath,
                                                kwLdrModuleShouldDoNativeReplacements(pszName, enmLocation));
@@ -4841,8 +4902,8 @@
     switch (cwcExt)
     {
-        case 3: kwFsIsCacheableExtensionCommon(pwszExt[0], pwszExt[1], pwszExt[2], fAttrQuery);
-        case 2: kwFsIsCacheableExtensionCommon(pwszExt[0], pwszExt[1], 0,          fAttrQuery);
-        case 1: kwFsIsCacheableExtensionCommon(pwszExt[0], 0,          0,          fAttrQuery);
-        case 0: kwFsIsCacheableExtensionCommon(0,          0,          0,          fAttrQuery);
+        case 3: return kwFsIsCacheableExtensionCommon(pwszExt[0], pwszExt[1], pwszExt[2], fAttrQuery);
+        case 2: return kwFsIsCacheableExtensionCommon(pwszExt[0], pwszExt[1], 0,          fAttrQuery);
+        case 1: return kwFsIsCacheableExtensionCommon(pwszExt[0], 0,          0,          fAttrQuery);
+        case 0: return kwFsIsCacheableExtensionCommon(0,          0,          0,          fAttrQuery);
     }
     return K_FALSE;
@@ -4906,4 +4967,5 @@
             {
                 KU32 cbCache = (KU32)cbFile.QuadPart;
+#if 0
                 KU8 *pbCache = (KU8 *)kHlpAlloc(cbCache);
                 if (pbCache)
@@ -4917,4 +4979,14 @@
                         if (SetFilePointerEx(hFile, offZero, NULL /*poffNew*/, FILE_BEGIN))
                         {
+#else
+                HANDLE hMapping = CreateFileMappingW(hFile, NULL /*pSecAttrs*/,  PAGE_READONLY,
+                                                     0 /*cbMaxLow*/, 0 /*cbMaxHigh*/, NULL /*pwszName*/);
+                if (hMapping != NULL)
+                {
+                    KU8 *pbCache = (KU8 *)MapViewOfFile(hMapping, FILE_MAP_READ, 0 /*offFileHigh*/, 0 /*offFileLow*/, cbCache);
+                    CloseHandle(hMapping);
+                    if (pbCache)
+                    {
+#endif
                             /*
                              * Create the cached file object.
@@ -4932,8 +5004,17 @@
                                 kFsCacheObjGetFullPathA(pFsObj, pCachedFile->szPath, cbPath, '/');
                                 kFsCacheObjRetain(pFsObj);
+                                KWFS_LOG(("Cached '%s': %p LB %#x, hCached=%p\n", pCachedFile->szPath, pbCache, cbCache, hFile));
                                 return pCachedFile;
                             }
 
                             KWFS_LOG(("Failed to allocate KFSWCACHEDFILE structure!\n"));
+#if 1
+                    }
+                    else
+                        KWFS_LOG(("Failed to cache file: MapViewOfFile failed: %u\n", GetLastError()));
+                }
+                else
+                    KWFS_LOG(("Failed to cache file: CreateFileMappingW failed: %u\n", GetLastError()));
+#else
                         }
                         else
@@ -4947,4 +5028,5 @@
                 else
                     KWFS_LOG(("Failed to allocate %#x bytes for cache!\n", cbCache));
+#endif
             }
             else
@@ -5024,4 +5106,8 @@
 {
     HANDLE hFile;
+
+    /*
+     * Check for include files and similar that we do read-only caching of.
+     */
     if (dwCreationDisposition == FILE_OPEN_IF)
     {
@@ -5103,5 +5189,7 @@
 
 #ifdef WITH_TEMP_MEMORY_FILES
-    /* First check for temporary files (cl.exe only). */
+    /*
+     * Check for temporary files (cl.exe only).
+     */
     if (   g_Sandbox.pTool->u.Sandboxed.enmHint == KWTOOLHINT_VISUAL_CPP_CL
         && !(dwFlagsAndAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE | FILE_FLAG_BACKUP_SEMANTICS))
@@ -5115,5 +5203,7 @@
 #endif
 
-    /* Then check for include files and similar. */
+    /*
+     * Check for include files and similar that we do read-only caching of.
+     */
     if (dwCreationDisposition == FILE_OPEN_IF)
     {
@@ -5129,10 +5219,40 @@
                     if (kwFsIsCacheablePathExtensionW(pwszFilename, K_FALSE /*fAttrQuery*/))
                     {
-                        /** @todo rewrite in pure UTF-16. */
-                        char szTmp[2048];
-                        KSIZE cch = kwUtf16ToStr(pwszFilename, szTmp, sizeof(szTmp));
-                        if (cch < sizeof(szTmp))
-                            return kwSandbox_Kernel32_CreateFileA(szTmp, dwDesiredAccess, dwShareMode, pSecAttrs,
-                                                                  dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
+                        KFSLOOKUPERROR enmError;
+                        PKFSOBJ pFsObj;
+                        kHlpAssert(GetCurrentThreadId() == g_Sandbox.idMainThread);
+
+                        pFsObj = kFsCacheLookupNoMissingW(g_pFsCache, pwszFilename, &enmError);
+                        if (pFsObj)
+                        {
+                            KBOOL fRc = kwFsObjCacheCreateFile(pFsObj, dwDesiredAccess, pSecAttrs && pSecAttrs->bInheritHandle,
+                                                               &hFile);
+                            kFsCacheObjRelease(g_pFsCache, pFsObj);
+                            if (fRc)
+                            {
+                                KWFS_LOG(("CreateFileW(%ls) -> %p [cached]\n", pwszFilename, hFile));
+                                return hFile;
+                            }
+                        }
+                        /* These are for nasm and yasm style header searching.  Cache will
+                           already have checked the directories for the file, no need to call
+                           CreateFile to do it again. */
+                        else if (enmError == KFSLOOKUPERROR_NOT_FOUND)
+                        {
+                            KWFS_LOG(("CreateFileW(%ls) -> INVALID_HANDLE_VALUE, ERROR_FILE_NOT_FOUND\n", pwszFilename));
+                            return INVALID_HANDLE_VALUE;
+                        }
+                        else if (   enmError == KFSLOOKUPERROR_PATH_COMP_NOT_FOUND
+                                 || enmError == KFSLOOKUPERROR_PATH_COMP_NOT_DIR)
+                        {
+                            KWFS_LOG(("CreateFileW(%ls) -> INVALID_HANDLE_VALUE, ERROR_PATH_NOT_FOUND\n", pwszFilename));
+                            return INVALID_HANDLE_VALUE;
+                        }
+
+                        /* fallback */
+                        hFile = CreateFileW(pwszFilename, dwDesiredAccess, dwShareMode, pSecAttrs,
+                                            dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
+                        KWFS_LOG(("CreateFileW(%ls) -> %p (err=%u) [fallback]\n", pwszFilename, hFile, GetLastError()));
+                        return hFile;
                     }
                 }
@@ -5976,4 +6096,5 @@
                                                            DWORD dwMaximumSizeLow, LPCWSTR pwszName)
 {
+    HANDLE      hMapping;
     KUPTR const idxHandle = KW_HANDLE_TO_INDEX(hFile);
     kHlpAssert(GetCurrentThreadId() == g_Sandbox.idMainThread);
@@ -5995,5 +6116,5 @@
                         && pwszName == NULL)
                     {
-                        HANDLE hMapping = kwFsTempFileCreateHandle(pHandle->u.pTempFile, GENERIC_READ, K_TRUE /*fMapping*/);
+                        hMapping = kwFsTempFileCreateHandle(pHandle->u.pTempFile, GENERIC_READ, K_TRUE /*fMapping*/);
                         KWFS_LOG(("CreateFileMappingW(%p, %u) -> %p [temp]\n", hFile, fProtect, hMapping));
                         return hMapping;
@@ -6004,16 +6125,21 @@
                     return INVALID_HANDLE_VALUE;
                 }
-            }
-        }
-    }
-
-    KWFS_LOG(("CreateFileMappingW(%p)\n", hFile));
-    return CreateFileMappingW(hFile, pSecAttrs, fProtect, dwMaximumSizeHigh, dwMaximumSizeLow, pwszName);
+
+                /** @todo read cached memory mapped files too for moc.   */
+            }
+        }
+    }
+
+    hMapping = CreateFileMappingW(hFile, pSecAttrs, fProtect, dwMaximumSizeHigh, dwMaximumSizeLow, pwszName);
+    KWFS_LOG(("CreateFileMappingW(%p, %p, %#x, %#x, %#x, %p) -> %p\n",
+              hFile, pSecAttrs, fProtect, dwMaximumSizeHigh, dwMaximumSizeLow, pwszName, hMapping));
+    return hMapping;
 }
 
 /** Kernel32 - MapViewOfFile  */
-static HANDLE WINAPI kwSandbox_Kernel32_MapViewOfFile(HANDLE hSection, DWORD dwDesiredAccess,
-                                                      DWORD offFileHigh, DWORD offFileLow, SIZE_T cbToMap)
-{
+static PVOID WINAPI kwSandbox_Kernel32_MapViewOfFile(HANDLE hSection, DWORD dwDesiredAccess,
+                                                     DWORD offFileHigh, DWORD offFileLow, SIZE_T cbToMap)
+{
+    PVOID       pvRet;
     KUPTR const idxHandle = KW_HANDLE_TO_INDEX(hSection);
     kHlpAssert(GetCurrentThreadId() == g_Sandbox.idMainThread);
@@ -6093,6 +6219,8 @@
     }
 
-    KWFS_LOG(("MapViewOfFile(%p)\n", hSection));
-    return MapViewOfFile(hSection, dwDesiredAccess, offFileHigh, offFileLow, cbToMap);
+    pvRet = MapViewOfFile(hSection, dwDesiredAccess, offFileHigh, offFileLow, cbToMap);
+    KWFS_LOG(("MapViewOfFile(%p, %#x, %#x, %#x, %#x) -> %p\n",
+              hSection, dwDesiredAccess, offFileHigh, offFileLow, cbToMap, pvRet));
+    return pvRet;
 }
 /** @todo MapViewOfFileEx */
Index: /trunk/src/lib/nt/kFsCache.c
===================================================================
--- /trunk/src/lib/nt/kFsCache.c	(revision 2939)
+++ /trunk/src/lib/nt/kFsCache.c	(revision 2940)
@@ -2457,4 +2457,20 @@
                                        KU32 *poff, KFSLOOKUPERROR *penmError)
 {
+    /*
+     * Special case: Long path prefix w/ drive letter following it.
+     * Note! Must've been converted from wide char to ANSI.
+     */
+    if (   IS_SLASH(pszPath[0])
+        && IS_SLASH(pszPath[1])
+        && pszPath[2] == '?'
+        && IS_SLASH(pszPath[3])
+        && IS_ALPHA(pszPath[4])
+        && pszPath[5] == ':'
+        && IS_SLASH(pszPath[6]) )
+    {
+        *poff = 4 + 2;
+        return kFsCacheLookupDrive(pCache, pszPath[4], fFlags, penmError);
+    }
+
 #if 0 /* later */
     KU32 offStartServer;
@@ -2503,5 +2519,5 @@
  * @param   pwszPath            The path.
  * @param   fFlags              Lookup flags, KFSCACHE_LOOKUP_F_XXX.
- * @param   poff                Where to return the root dire.
+ * @param   poff                Where to return the root dir.
  * @param   penmError           Where to return details as to why the lookup
  *                              failed.
@@ -2510,4 +2526,20 @@
                                        KU32 *poff, KFSLOOKUPERROR *penmError)
 {
+    /*
+     * Special case: Long path prefix w/ drive letter following it.
+     */
+    if (   IS_SLASH(pwszPath[0])
+        && IS_SLASH(pwszPath[1])
+        && pwszPath[2] == '?'
+        && IS_SLASH(pwszPath[3])
+        && IS_ALPHA(pwszPath[4])
+        && pwszPath[5] == ':'
+        && IS_SLASH(pwszPath[6]) )
+    {
+        *poff = 4 + 2;
+        return kFsCacheLookupDrive(pCache, (char)pwszPath[4], fFlags, penmError);
+    }
+
+
 #if 0 /* later */
     KU32 offStartServer;
