bool HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset, nsAString& aFilename) { aFilename.Truncate(); // This implementation is nice because it uses fully documented APIs that // are available on all Windows versions that we support. nsAutoHandle fileMapping(CreateFileMapping(aHandle, nullptr, PAGE_READONLY, 0, 1, nullptr)); if (!fileMapping) { return false; } ScopedMappedView view(MapViewOfFile(fileMapping, FILE_MAP_READ, aOffset.HighPart, aOffset.LowPart, 1)); if (!view) { return false; } nsAutoString mappedFilename; DWORD len = 0; SetLastError(ERROR_SUCCESS); do { mappedFilename.SetLength(mappedFilename.Length() + MAX_PATH); len = GetMappedFileNameW(GetCurrentProcess(), view, wwc(mappedFilename.BeginWriting()), mappedFilename.Length()); } while (!len && GetLastError() == ERROR_INSUFFICIENT_BUFFER); if (!len) { return false; } mappedFilename.Truncate(len); return NtPathToDosPath(mappedFilename, aFilename); }
//---------------------------------------------------------------------------- // writeHandle() // Writes a window handle to the mem-file. // Creates the memfile and returns in case of success the handle for that // file for cleaning up. This method is called to pass a window handle to the // page actions dll. // The dll is responsible for closing the file! HRESULT Base::writeHandle(const HWND aWindowHandle, HANDLE & aFileMapping) { __int32 ihwndMain = (__int32)aWindowHandle; DWORD dataSize = sizeof(__int32); CHandle fileMapping(::CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, dataSize, sFileMappingName )); if (!fileMapping) { return HRESULT_FROM_WIN32(::GetLastError()); } LPVOID data = ::MapViewOfFile( fileMapping, FILE_MAP_ALL_ACCESS, 0, 0, dataSize ); if( !data ) { return HRESULT_FROM_WIN32(::GetLastError()); } ::CopyMemory( data, &ihwndMain, dataSize ); ::UnmapViewOfFile( data ); aFileMapping = fileMapping.Detach(); return S_OK; }
//---------------------------------------------------------------------------- // readHandle() // Read a window handle (from an IE main window) from the mem-file. HRESULT Base::readHandle(HWND & aWindowHandle) { CHandle fileMapping(::OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, sFileMappingName )); if (!fileMapping) { return HRESULT_FROM_WIN32(::GetLastError()); } DWORD dataSize = sizeof(__int32); LPVOID data = ::MapViewOfFile( fileMapping, FILE_MAP_ALL_ACCESS, 0, 0, dataSize ); if( !data ) { return HRESULT_FROM_WIN32(::GetLastError()); } __int32 ihwndMain = 0; ::CopyMemory( &ihwndMain, data, dataSize ); ::UnmapViewOfFile( data ); fileMapping.Close(); if (!ihwndMain || !::IsWindow((HWND)ihwndMain)) { return E_INVALIDARG; } aWindowHandle = (HWND)ihwndMain; return S_OK; }