Editare

Partajați prin


Marshal.AllocHGlobal Method

Definition

Allocates memory from the unmanaged memory of the process.

Overloads

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.

AllocHGlobal(Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

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

Parameters

cb
Int32

The required number of bytes in memory.

Returns

IntPtr

nativeint

A pointer to the newly allocated memory. This memory must be released using the FreeHGlobal(IntPtr) method.

Attributes

Exceptions

There is insufficient memory to satisfy the request.

Examples

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)

Remarks

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.

See also

Applies to

AllocHGlobal(IntPtr)

Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs

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

Parameters

cb
IntPtr

nativeint

The required number of bytes in memory.

Returns

IntPtr

nativeint

A pointer to the newly allocated memory. This memory must be released using the FreeHGlobal(IntPtr) method.

Attributes

Exceptions

There is insufficient memory to satisfy the request.

Remarks

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.

See also

Applies to