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)

通过使用指定的字节数,从进程的非托管内存中分配内存。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);
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

指向新分配的内存的指针。A pointer to the newly allocated memory. 必须使用 FreeHGlobal(IntPtr) 方法释放该内存。This memory must be released using the FreeHGlobal(IntPtr) method.

Attributes

Exceptions

没有足够的内存来满足该请求。There is insufficient memory to satisfy the request.

Examples

下面的示例演示如何调用 AllocHGlobal 方法。The following example demonstrates calling the AllocHGlobal method. 此代码示例是为 Marshal 类提供的更大示例的一部分。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

AllocHGlobalMarshal 类中两个内存分配方法之一。AllocHGlobal is one of two memory allocation methods in the Marshal class. Marshal.AllocCoTaskMem 是另一个。)此方法从 Kernel32.dll 公开 Win32 LocalAlloc函数。(Marshal.AllocCoTaskMem is the other.) This method exposes the Win32 LocalAlloc function from Kernel32.dll.

AllocHGlobal 调用 LocalAlloc时,它将传递一个 LMEM_FIXED 标志,这会导致已分配的内存就地锁定。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

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

Parameters

cb
IntPtr

内存中的所需字节数。The required number of bytes in memory.

Returns

IntPtr

指向新分配的内存的指针。A pointer to the newly allocated memory. 必须使用 FreeHGlobal(IntPtr) 方法释放该内存。This memory must be released using the FreeHGlobal(IntPtr) method.

Attributes

Exceptions

没有足够的内存来满足该请求。There is insufficient memory to satisfy the request.

Remarks

AllocHGlobalMarshal 类中两个内存分配方法之一。AllocHGlobal is one of two memory allocation methods in the Marshal class. Marshal.AllocCoTaskMem 是另一个。)此方法从 Kernel32.dll 公开 Win32 LocalAlloc函数。(Marshal.AllocCoTaskMem is the other.) This method exposes the Win32 LocalAlloc function from Kernel32.dll.

AllocHGlobal 调用 LocalAlloc时,它将传递一个 LMEM_FIXED 标志,这会导致已分配的内存就地锁定。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.

有关代码示例,请参阅 MarshalAllocHGlobalFor example code, see Marshal and AllocHGlobal.

See also

Applies to