Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Allocates memory from the unmanaged memory of the process.
AllocHGlobal(Int32) |
Allocates memory from the unmanaged memory of the process by using the specified number of bytes. |
AllocHGlobal(IntPtr) |
Allocates memory from the unmanaged memory of the process by using the pointer to the specified number of bytes. |
Allocates memory from the unmanaged memory of the process by using the specified number of bytes.
public:
static IntPtr AllocHGlobal(int cb);
[System.Security.SecurityCritical]
public static IntPtr AllocHGlobal(int cb);
public static IntPtr AllocHGlobal(int cb);
[<System.Security.SecurityCritical>]
static member AllocHGlobal : int -> nativeint
static member AllocHGlobal : int -> nativeint
Public Shared Function AllocHGlobal (cb As Integer) As IntPtr
The required number of bytes in memory.
nativeint
A pointer to the newly allocated memory. This memory must be released using the FreeHGlobal(IntPtr) method.
There is insufficient memory to satisfy the request.
The following example demonstrates calling the AllocHGlobal method. This code example is part of a larger example provided for the Marshal class.
// Demonstrate how to call GlobalAlloc and
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal::AllocHGlobal(100);
Marshal::FreeHGlobal(hglobal);
// Demonstrate how to call GlobalAlloc and
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal.AllocHGlobal(100);
Marshal.FreeHGlobal(hglobal);
' Demonstrate how to call GlobalAlloc and
' GlobalFree using the Marshal class.
Dim hglobal As IntPtr = Marshal.AllocHGlobal(100)
Marshal.FreeHGlobal(hglobal)
Important
This native memory allocator is a legacy API that should be used exclusively when called for by specific Win32 APIs on the Windows platform. When targeting .NET 6 or later, use the NativeMemory class on all platforms to allocate native memory. When targeting .NET 6 or earlier, use AllocCoTaskMem on all platforms to allocate native memory.
AllocHGlobal is one of two memory allocation methods in the Marshal class. (Marshal.AllocCoTaskMem is the other.) This method exposes the Win32 LocalAlloc function from Kernel32.dll.
When AllocHGlobal calls LocalAlloc
, it passes a LMEM_FIXED
flag, which causes the allocated memory to be locked in place. Also, the allocated memory is not zero-filled.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Allocates memory from the unmanaged memory of the process by using the pointer to the specified number of bytes.
public:
static IntPtr AllocHGlobal(IntPtr cb);
[System.Security.SecurityCritical]
public static IntPtr AllocHGlobal(IntPtr cb);
public static IntPtr AllocHGlobal(IntPtr cb);
[<System.Security.SecurityCritical>]
static member AllocHGlobal : nativeint -> nativeint
static member AllocHGlobal : nativeint -> nativeint
Public Shared Function AllocHGlobal (cb As IntPtr) As IntPtr
nativeint
The required number of bytes in memory.
nativeint
A pointer to the newly allocated memory. This memory must be released using the FreeHGlobal(IntPtr) method.
There is insufficient memory to satisfy the request.
Important
This native memory allocator is a legacy API that should be used exclusively when called for by specific Win32 APIs on the Windows platform. When targeting .NET 6 or later, use the NativeMemory class on all platforms to allocate native memory. When targeting .NET 6 or earlier, use AllocCoTaskMem on all platforms to allocate native memory.
AllocHGlobal is one of two memory allocation methods in the Marshal class. (Marshal.AllocCoTaskMem is the other.) This method exposes the Win32 LocalAlloc function from Kernel32.dll.
When AllocHGlobal calls LocalAlloc
, it passes a LMEM_FIXED
flag, which causes the allocated memory to be locked in place. Also, the allocated memory is not zero-filled.
For example code, see Marshal and AllocHGlobal.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Please sign in to use this experience.
Sign in