From 38fadc70eedc17698fec0642e64a9ce430a97274 Mon Sep 17 00:00:00 2001 From: David Hall Date: Mon, 4 Feb 2019 12:41:34 -0700 Subject: [PATCH] Added MapViewOfFile3 and MapViewOfFile3FromApp --- PInvoke/Kernel32/MemoryApi.cs | 174 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/PInvoke/Kernel32/MemoryApi.cs b/PInvoke/Kernel32/MemoryApi.cs index 26563773..d7f835a4 100644 --- a/PInvoke/Kernel32/MemoryApi.cs +++ b/PInvoke/Kernel32/MemoryApi.cs @@ -1626,6 +1626,88 @@ namespace Vanara.PInvoke [PInvokeData("WinBase.h", MSDNShortId = "aa366761")] public static extern IntPtr MapViewOfFile([In] HFILE hFileMappingObject, FILE_MAP dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap); + /// + /// Maps a view of a file or a pagefile-backed section into the address space of the specified process. + /// + /// Using this function, you can: for new allocations, specify a range of virtual address space and a power-of-2 alignment + /// restriction; specify an arbitrary number of extended parameters; specify a preferred NUMA node for the physical memory as an + /// extended parameter; and specify a placeholder operation (specifically, replacement). + /// + /// To specify the NUMA node, see the ExtendedParameters parameter. + /// + /// A HANDLE to a section that is to be mapped into the address space of the specified process. + /// A HANDLE to a process into which the section will be mapped. + /// + /// The desired base address of the view. The address is rounded down to the nearest 64k boundary. If this parameter is NULL, + /// the system picks the base address. + /// + /// The offset from the beginning of the section. This must be 64k aligned. + /// + /// The number of bytes to map. A value of zero (0) specifies that the entire section is to be mapped. + /// The size must always be a multiple of the page size. + /// + /// + /// The type of memory allocation. This parameter can be zero (0) or one of the following values. + /// + /// + /// Value + /// Meaning + /// + /// + /// MEM_RESERVE 0x00002000 + /// Maps a reserved view. + /// + /// + /// MEM_REPLACE_PLACEHOLDER 0x00004000 + /// + /// Replaces a placeholder with a mapped view. Only data/pf-backed section views are supported (no images, physical memory, etc.). + /// When you replace a placeholder, BaseAddress and ViewSize must exactly match those of the placeholder. After you replace a + /// placeholder with a mapped view, to free that mapped view back to a placeholder, see the UnmapFlags parameter of UnmapViewOfFileEx + /// and UnmapViewOfFile2. A placeholder is a type of reserved memory region. + /// + /// + /// + /// MEM_LARGE_PAGES 0x20000000 + /// Maps a large page view. See large page support. + /// + /// + /// + /// + /// The desired page protection. + /// + /// For file-mapping objects created with the SEC_IMAGE attribute, the PageProtection parameter has no effect, and should be + /// set to any valid value such as PAGE_READONLY. + /// + /// + /// + /// An optional pointer to one or more extended parameters of type MEM_EXTENDED_PARAMETER. Each of those extended parameter values + /// can itself have a Type field of either MemExtendedParameterAddressRequirements or MemExtendedParameterNumaNode. If + /// no MemExtendedParameterNumaNode extended parameter is provided, then the behavior is the same as for the + /// VirtualAlloc/MapViewOfFile functions (that is, the preferred NUMA node for the physical pages is determined based on the ideal + /// processor of the thread that first accesses the memory). + /// + /// The number of extended parameters pointed to by ExtendedParameters. + /// + /// Returns the base address of the mapped view, if successful. Otherwise, returns NULL and extended error status is available + /// using GetLastError. + /// + /// + /// + /// This API helps support high-performance games, and server applications, which have particular requirements around managing their + /// virtual address space. For example, mapping memory on top of a previously reserved region; this is useful for implementing an + /// automatically wrapping ring buffer. And allocating memory with specific alignment; for example, to enable your application to + /// commit large/huge page-mapped regions on demand. + /// + /// Examples + /// For a code example, see Scenario 1 in Virtual2Alloc. + /// + // https://docs.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-mapviewoffile3 + // PVOID MapViewOfFile3( HANDLE FileMapping, HANDLE Process, PVOID BaseAddress, ULONG64 Offset, SIZE_T ViewSize, ULONG AllocationType, ULONG PageProtection, MEM_EXTENDED_PARAMETER *ExtendedParameters, ULONG ParameterCount ); + [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] + [PInvokeData("memoryapi.h", MSDNShortId = "585D7BA1-688F-4F24-8D8D-46A2FC137193")] + public static extern IntPtr MapViewOfFile3([In] HFILE FileMapping, [In] HPROCESS Process, [In] IntPtr BaseAddress, ulong Offset, SizeT ViewSize, MEM_ALLOCATION_TYPE AllocationType, + MEM_PROTECTION PageProtection, [In] MEM_EXTENDED_PARAMETER[] ExtendedParameters, uint ParameterCount); + /// /// /// Maps a view of a file mapping into the address space of a calling process. A caller can optionally specify a suggested base @@ -1816,6 +1898,98 @@ namespace Vanara.PInvoke [PInvokeData("MemoryApi.h", MSDNShortId = "hh994454")] public static extern IntPtr MapViewOfFileFromApp([In] HFILE hFileMappingObject, FILE_MAP DesiredAccess, ulong FileOffset, SizeT NumberOfBytesToMap); + /// + /// Maps a view of a file mapping into the address space of a calling Windows Store app. + /// + /// Using this function, you can: for new allocations, specify a range of virtual address space and a power-of-2 alignment + /// restriction; specify an arbitrary number of extended parameters; specify a preferred NUMA node for the physical memory as an + /// extended parameter; and specify a placeholder operation (specifically, replacement). + /// + /// To specify the NUMA node, see the ExtendedParameters parameter. + /// + /// A HANDLE to a section that is to be mapped into the address space of the specified process. + /// A HANDLE to a process into which the section will be mapped. + /// + /// The desired base address of the view. The address is rounded down to the nearest 64k boundary. If this parameter is NULL, + /// the system picks the base address. + /// + /// The offset from the beginning of the section. This must be 64k aligned. + /// + /// The number of bytes to map. A value of zero (0) specifies that the entire section is to be mapped. + /// The size must always be a multiple of the page size. + /// + /// + /// The type of memory allocation. This parameter can be zero (0) or one of the following values. + /// + /// + /// Value + /// Meaning + /// + /// + /// MEM_RESERVE 0x00002000 + /// Maps a reserved view. + /// + /// + /// MEM_REPLACE_PLACEHOLDER 0x00004000 + /// + /// Replaces a placeholder with a mapped view. Only data/pf-backed section views are supported (no images, physical memory, etc.). + /// When you replace a placeholder, BaseAddress and ViewSize must exactly match those of the placeholder. After you replace a + /// placeholder with a mapped view, to free that mapped view back to a placeholder, see the UnmapFlags parameter of UnmapViewOfFileEx + /// and UnmapViewOfFile2. A placeholder is a type of reserved memory region. + /// + /// + /// + /// MEM_LARGE_PAGES 0x20000000 + /// Maps a large page view. See large page support. + /// + /// + /// + /// + /// The desired page protection. + /// + /// For file-mapping objects created with the SEC_IMAGE attribute, the PageProtection parameter has no effect, and should be + /// set to any valid value such as PAGE_READONLY. + /// + /// + /// + /// An optional pointer to one or more extended parameters of type MEM_EXTENDED_PARAMETER. Each of those extended parameter values + /// can itself have a Type field of either MemExtendedParameterAddressRequirements or MemExtendedParameterNumaNode. If + /// no MemExtendedParameterNumaNode extended parameter is provided, then the behavior is the same as for the + /// VirtualAlloc/MapViewOfFile functions (that is, the preferred NUMA node for the physical pages is determined based on the ideal + /// processor of the thread that first accesses the memory). + /// + /// The number of extended parameters pointed to by ExtendedParameters. + /// + /// Returns the base address of the mapped view, if successful. Otherwise, returns NULL and extended error status is available + /// using GetLastError. + /// + /// + /// + /// This API helps support high-performance games, and server applications, which have particular requirements around managing their + /// virtual address space. For example, mapping memory on top of a previously reserved region; this is useful for implementing an + /// automatically wrapping ring buffer. And allocating memory with specific alignment; for example, to enable your application to + /// commit large/huge page-mapped regions on demand. + /// + /// + /// With one important exception, file views derived from any file mapping object that is backed by the same file are coherent or + /// identical at a specific time. Coherency is guaranteed for views within a process and for views that are mapped by different processes. + /// + /// + /// The exception is related to remote files. Although MapViewOfFile3FromApp works with remote files, it does not keep them + /// coherent. For example, if two computers both map a file as writable, and both change the same page, each computer only sees its + /// own writes to the page. When the data gets updated on the disk, it is not merged. + /// + /// You can only successfully request executable protection if your app has the codeGeneration capability. + /// Examples + /// For a code example, see Scenario 1 in Virtual2Alloc. + /// + // https://docs.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-mapviewoffile3fromapp + // PVOID MapViewOfFile3FromApp( HANDLE FileMapping, HANDLE Process, PVOID BaseAddress, ULONG64 Offset, SIZE_T ViewSize, ULONG AllocationType, ULONG PageProtection, MEM_EXTENDED_PARAMETER *ExtendedParameters, ULONG ParameterCount ); + [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] + [PInvokeData("memoryapi.h", MSDNShortId = "5E10E1B2-69D9-4F68-8F06-D411CF7FE2ED")] + public static extern IntPtr MapViewOfFile3FromApp([In] HFILE FileMapping, [In] HPROCESS Process, [In] IntPtr BaseAddress, ulong Offset, SizeT ViewSize, MEM_ALLOCATION_TYPE AllocationType, + MEM_PROTECTION PageProtection, [In] MEM_EXTENDED_PARAMETER[] ExtendedParameters, uint ParameterCount); + /// Maps a view of a file or a pagefile-backed section into the address space of the specified process. /// A HANDLE to a section that is to be mapped into the address space of the specified process. /// A HANDLE to a process into which the section will be mapped.