Marshal.AllocHGlobal Marshal.AllocHGlobal Marshal.AllocHGlobal Marshal.AllocHGlobal Method

Definition

Allocates memory from the unmanaged memory of the process.

Overloads

AllocHGlobal(Int32) AllocHGlobal(Int32) AllocHGlobal(Int32) AllocHGlobal(Int32)

Allocates memory from the unmanaged memory of the process by using the specified number of bytes.

AllocHGlobal(IntPtr) AllocHGlobal(IntPtr) AllocHGlobal(IntPtr) AllocHGlobal(IntPtr)

Allocates memory from the unmanaged memory of the process by using the pointer to the specified number of bytes.

AllocHGlobal(Int32) AllocHGlobal(Int32) AllocHGlobal(Int32) AllocHGlobal(Int32)

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);
static member AllocHGlobal : int -> nativeint
Public Shared Function AllocHGlobal (cb As Integer) As IntPtr

Parameters

cb
Int32 Int32 Int32 Int32

The required number of bytes in memory.

Returns

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

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

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.

Security

SecurityCriticalAttribute
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

See also

AllocHGlobal(IntPtr) AllocHGlobal(IntPtr) AllocHGlobal(IntPtr) AllocHGlobal(IntPtr)

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);
static member AllocHGlobal : nativeint -> nativeint
Public Shared Function AllocHGlobal (cb As IntPtr) As IntPtr

Parameters

cb
IntPtr IntPtr IntPtr IntPtr

The required number of bytes in memory.

Returns

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

Exceptions

There is insufficient memory to satisfy the request.

Remarks

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.

Security

SecurityCriticalAttribute
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

See also

Applies to