From cb5aeed8c3e24a181a36c3ba58b7983e52e68786 Mon Sep 17 00:00:00 2001 From: dahall Date: Fri, 10 Dec 2021 08:59:45 -0700 Subject: [PATCH] Fixed missing size in HeapAlloc, HeapReAlloc, and SafeHHEAP.GetBlock (#258) --- PInvoke/Kernel32/HeapApi.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/PInvoke/Kernel32/HeapApi.cs b/PInvoke/Kernel32/HeapApi.cs index 6cfb1312..f64b6199 100644 --- a/PInvoke/Kernel32/HeapApi.cs +++ b/PInvoke/Kernel32/HeapApi.cs @@ -453,7 +453,7 @@ namespace Vanara.PInvoke /// [PInvokeData("HeapApi.h", MSDNShortId = "aa366599")] [DllImport(Lib.Kernel32, ExactSpelling = true, SetLastError = true)] - public static extern SafeHHEAP HeapCreate(HeapFlags flOptions, SizeT dwInitialSize, SizeT dwMaximumSize); + public static extern SafeHHEAP HeapCreate(HeapFlags flOptions, SizeT dwInitialSize = default, SizeT dwMaximumSize = default); /// /// Destroys the specified heap object. It decommits and releases all the pages of a private heap object, and it invalidates the @@ -1474,7 +1474,7 @@ namespace Vanara.PInvoke /// /// /// size - The value of this argument must be non-negative - public SafeHeapBlock(HHEAP hHeap, SizeT size = default, HeapFlags flags = 0) : this(hHeap, IntPtr.Zero, 0) + public SafeHeapBlock(HHEAP hHeap, SizeT size = default, HeapFlags flags = 0) : this(hHeap, IntPtr.Zero, size) { if (size < 0) throw new ArgumentOutOfRangeException(nameof(size), "The value of this argument must be non-negative"); @@ -1561,15 +1561,32 @@ namespace Vanara.PInvoke /// Initializes a new instance of the class. private SafeHHEAP() : base() { } + /// Gets or sets the heap features that are enabled. + HeapCompatibility Compatibility + { + get => HeapQueryInformation(this, HEAP_INFORMATION_CLASS.HeapCompatibilityInformation); + set => HeapSetInformation(this, HEAP_INFORMATION_CLASS.HeapCompatibilityInformation, value); + } + /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator HHEAP(SafeHHEAP h) => h.handle; + /// + /// Enables the terminate-on-corruption feature. If the heap manager detects an error in any heap used by the process, it calls + /// the Windows Error Reporting service and terminates the process. + /// After a process enables this feature, it cannot be disabled. + /// + public void EnableTerminationOnCorruption() => Win32Error.ThrowLastErrorIfFalse(HeapSetInformation(this, HEAP_INFORMATION_CLASS.HeapEnableTerminationOnCorruption, default, default)); + /// Gets a block of memory from this private heap. /// The size of the block. /// A safe handle for the memory that will call HeapFree on disposal. - public SafeHeapBlock GetBlock(int size) => new(this, size); + public SafeHeapBlock GetBlock(SizeT size) => new(this, size); + + /// Optimizes caches for this heap, and decommits the memory if possible. + public void OptimizeResources() => HeapSetInformation(this, HEAP_INFORMATION_CLASS.HeapOptimizeResources, new HEAP_OPTIMIZE_RESOURCES_INFORMATION(0)); /// protected override bool InternalReleaseHandle() => HeapDestroy(this);