diff --git a/PInvoke/Kernel32/WinBase.MemMgmt.cs b/PInvoke/Kernel32/WinBase.MemMgmt.cs index 228b2379..4dd4c110 100644 --- a/PInvoke/Kernel32/WinBase.MemMgmt.cs +++ b/PInvoke/Kernel32/WinBase.MemMgmt.cs @@ -27,6 +27,12 @@ namespace Vanara.PInvoke /// Combines GMEM_FIXED and GMEM_ZEROINIT. GPTR = 0x0040, + + /// + /// The function modifies the attributes of the memory object only (the dwBytes parameter is ignored). This value can only be + /// used with . + /// + GMEM_MODIFY = 0x0080, } /// The memory allocation attributes. @@ -72,16 +78,16 @@ namespace Vanara.PInvoke LMEM_INVALID_HANDLE = 0x8000, /// Combines LMEM_MOVEABLE and LMEM_ZEROINIT. - LHND = (LMEM_MOVEABLE | LMEM_ZEROINIT), + LHND = LMEM_MOVEABLE | LMEM_ZEROINIT, /// Combines LMEM_FIXED and LMEM_ZEROINIT. - LPTR = (LMEM_FIXED | LMEM_ZEROINIT), + LPTR = LMEM_FIXED | LMEM_ZEROINIT, /// Same as LMEM_MOVEABLE. - NONZEROLHND = (LMEM_MOVEABLE), + NONZEROLHND = LMEM_MOVEABLE, /// Same as LMEM_FIXED. - NONZEROLPTR = (LMEM_FIXED) + NONZEROLPTR = LMEM_FIXED } /// Allocates the specified number of bytes from the heap. @@ -712,87 +718,6 @@ namespace Vanara.PInvoke [return: MarshalAs(UnmanagedType.Bool)] public static extern bool LocalUnlock([In] HLOCAL hMem); - /// - /// Maps previously allocated physical memory pages at a specified address in an Address Windowing Extensions (AWE) region. - /// - /// 64-bit Windows on Itanium-based systems: Due to the difference in page sizes, MapUserPhysicalPagesScatter is not - /// supported for 32-bit applications. - /// - /// - /// - /// A pointer to an array of starting addresses of the regions of memory to remap. - /// - /// Each entry in VirtualAddresses must be within the address range that the VirtualAlloc function returns when the Address - /// Windowing Extensions (AWE) region is allocated. The value in NumberOfPages indicates the size of the array. Entries can be from - /// multiple Address Windowing Extensions (AWE) regions. - /// - /// - /// - /// The size of the physical memory and virtual address space for which to establish translations, in pages. - /// The array at VirtualAddresses specifies the virtual address range. - /// - /// - /// A pointer to an array of values that indicates how each corresponding page in VirtualAddresses should be treated. - /// - /// A 0 (zero) indicates that the corresponding entry in VirtualAddresses should be unmapped, and any nonzero value that it has - /// should be mapped. - /// - /// If this parameter is NULL, then every address in the VirtualAddresses array is unmapped. - /// The value in NumberOfPages indicates the size of the array. - /// - /// - /// If the function succeeds, the return value is TRUE. - /// - /// If the function fails, the return value is FALSE, and the function does not map or unmap—partial or otherwise. To get - /// extended error information, call GetLastError. - /// - /// - // BOOL WINAPI MapUserPhysicalPagesScatter( _In_ PVOID *VirtualAddresses, _In_ ULONG_PTR NumberOfPages, _In_ PULONG_PTR PageArray); https://msdn.microsoft.com/en-us/library/windows/desktop/aa366755(v=vs.85).aspx - [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] - [PInvokeData("WinBase.h", MSDNShortId = "aa366755")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool MapUserPhysicalPagesScatter(IntPtr VirtualAddresses, SizeT NumberOfPages, [In] IntPtr PageArray); - - /// Changes the protection on a region of committed pages in the virtual address space of a specified process. - /// - /// A handle to the process whose memory protection is to be changed. The handle must have the PROCESS_VM_OPERATION access - /// right. For more information, see Process Security and Access Rights. - /// - /// - /// A pointer to the base address of the region of pages whose access protection attributes are to be changed. - /// - /// All pages in the specified region must be within the same reserved region allocated when calling the VirtualAlloc or - /// VirtualAllocEx function using MEM_RESERVE. The pages cannot span adjacent reserved regions that were allocated by - /// separate calls to VirtualAlloc or VirtualAllocEx using MEM_RESERVE. - /// - /// - /// - /// The size of the region whose access protection attributes are changed, in bytes. The region of affected pages includes all pages - /// containing one or more bytes in the range from the lpAddress parameter to . This means that a 2-byte range straddling a page - /// boundary causes the protection attributes of both pages to be changed. - /// - /// - /// The memory protection option. This parameter can be one of the memory protection constants. - /// - /// For mapped views, this value must be compatible with the access protection specified when the view was mapped (see - /// MapViewOfFile, MapViewOfFileEx, and MapViewOfFileExNuma). - /// - /// - /// - /// A pointer to a variable that receives the previous access protection of the first page in the specified region of pages. If this - /// parameter is NULL or does not point to a valid variable, the function fails. - /// - /// - /// If the function succeeds, the return value is nonzero. - /// If the function fails, the return value is zero. To get extended error information, call GetLastError. - /// - // BOOL WINAPI VirtualProtectEx( _In_ HANDLE hProcess, _In_ LPVOID lpAddress, _In_ SIZE_T dwSize, _In_ DWORD flNewProtect, _Out_ - // PDWORD lpflOldProtect); https://msdn.microsoft.com/en-us/library/windows/desktop/aa366899(v=vs.85).aspx - [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] - [PInvokeData("WinBase.h", MSDNShortId = "aa366899")] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool VirtualProtectEx([In] HPROCESS hProcess, [In] IntPtr lpAddress, SizeT dwSize, uint flNewProtect, out uint lpflOldProtect); - /// Provides a handle to heap allocated memory. [StructLayout(LayoutKind.Sequential)] public struct HGLOBAL : IHandle diff --git a/UnitTests/PInvoke/Kernel32/Kernel32.csproj b/UnitTests/PInvoke/Kernel32/Kernel32.csproj index 8bd10bee..b90f92c1 100644 --- a/UnitTests/PInvoke/Kernel32/Kernel32.csproj +++ b/UnitTests/PInvoke/Kernel32/Kernel32.csproj @@ -48,6 +48,9 @@ + + + diff --git a/UnitTests/PInvoke/Kernel32/WinBase.MemMgmtTests.cs b/UnitTests/PInvoke/Kernel32/WinBase.MemMgmtTests.cs new file mode 100644 index 00000000..8cdc50e0 --- /dev/null +++ b/UnitTests/PInvoke/Kernel32/WinBase.MemMgmtTests.cs @@ -0,0 +1,67 @@ +using NUnit.Framework; +using System; +using Vanara.InteropServices; +using static Vanara.PInvoke.Kernel32; + +namespace Vanara.PInvoke.Tests +{ + [TestFixture] + public partial class WinBaseTests_MemMgmt + { + [Test] + public void HGlobalTest() + { + HGLOBAL hMem; + Assert.That(hMem = GlobalAlloc(GMEM.GHND, 256), ResultIs.ValidHandle); + try + { + Assert.That(GlobalFlags(hMem), ResultIs.Value(GMEM.GMEM_FIXED)); + Assert.That(GlobalSize(hMem), ResultIs.Value(new SizeT(256))); + IntPtr ptr; + Assert.That(ptr = GlobalLock(hMem), ResultIs.ValidHandle); + Assert.That(GlobalHandle(ptr), ResultIs.Value(hMem)); + Assert.That(GlobalUnlock(hMem), ResultIs.Successful); + Assert.That(hMem = GlobalReAlloc(hMem, 128, 0), ResultIs.ValidHandle); + } + finally + { + Assert.That(GlobalFree(hMem), ResultIs.Value(HGLOBAL.NULL)); + } + } + + [Test] + public void HLocalTest() + { + HLOCAL hMem; + Assert.That(hMem = LocalAlloc(LMEM.LHND, 256), ResultIs.ValidHandle); + try + { + Assert.That(LocalFlags(hMem), ResultIs.Value(LMEM.LMEM_FIXED)); + Assert.That(LocalSize(hMem), ResultIs.Value(new SizeT(256))); + IntPtr ptr; + Assert.That(ptr = LocalLock(hMem), ResultIs.ValidHandle); + Assert.That(LocalHandle(ptr), ResultIs.Value(hMem)); + Assert.That(LocalUnlock(hMem), ResultIs.Successful); + Assert.That(hMem = LocalReAlloc(hMem, 128, 0), ResultIs.ValidHandle); + } + finally + { + Assert.That(LocalFree(hMem), ResultIs.Value(HLOCAL.NULL)); + } + } + + [Test] + public void IsBadPtrTest() + { + using (var mem = new SafeHGlobalHandle(8)) + { +#pragma warning disable CS0618 // Type or member is obsolete + Assert.That(IsBadCodePtr(mem), ResultIs.Successful); + Assert.That(IsBadReadPtr(mem, 8), ResultIs.Successful); + Assert.That(IsBadStringPtr("string", 4), ResultIs.Successful); + Assert.That(IsBadWritePtr(mem, 8), ResultIs.Successful); +#pragma warning restore CS0618 // Type or member is obsolete + } + } + } +} \ No newline at end of file diff --git a/UnitTests/PInvoke/Kernel32/WinBase.PowerTests.cs b/UnitTests/PInvoke/Kernel32/WinBase.PowerTests.cs new file mode 100644 index 00000000..21de9ecd --- /dev/null +++ b/UnitTests/PInvoke/Kernel32/WinBase.PowerTests.cs @@ -0,0 +1,15 @@ +using NUnit.Framework; +using System; +using System.Runtime.InteropServices; +using System.Text; +using Vanara.InteropServices; +using static Vanara.PInvoke.AdvApi32; +using static Vanara.PInvoke.Kernel32; + +namespace Vanara.PInvoke.Tests +{ + [TestFixture] + public partial class WinBaseTests_Power + { + } +} \ No newline at end of file