Marshal.AllocHGlobal 方法

定义

从进程的非托管内存中分配内存。

重载

AllocHGlobal(Int32)

通过使用指定的字节数,从进程的非托管内存中分配内存。

AllocHGlobal(IntPtr)

通过使用指向指定字节数的指针,从进程的非托管内存中分配内存。

AllocHGlobal(Int32)

通过使用指定的字节数,从进程的非托管内存中分配内存。

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

参数

cb
Int32

内存中的所需字节数。

返回

IntPtr

nativeint

指向新分配的内存的指针。 必须使用 FreeHGlobal(IntPtr) 方法释放该内存。

属性

例外

没有足够的内存来满足该请求。

示例

下面的示例演示如何调用 AllocHGlobal 方法。 此代码示例是为 Marshal 类提供的一个更大示例的一部分。

// 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)

注解

AllocHGlobal 是类中两种内存分配方法之 Marshal 一。 Marshal.AllocCoTaskMem (是另一个.) 此方法从 Kernel32.dll 公开 Win32 LocalAlloc 函数。

调用LocalAllocAllocHGlobal,它会传递一个LMEM_FIXED标志,这会导致分配的内存被锁定到位。 此外,分配的内存不会为零填充。

另请参阅

适用于

AllocHGlobal(IntPtr)

通过使用指向指定字节数的指针,从进程的非托管内存中分配内存。

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

参数

cb
IntPtr

nativeint

内存中的所需字节数。

返回

IntPtr

nativeint

指向新分配的内存的指针。 必须使用 FreeHGlobal(IntPtr) 方法释放该内存。

属性

例外

没有足够的内存来满足该请求。

注解

AllocHGlobal 是类中两种内存分配方法之 Marshal 一。 Marshal.AllocCoTaskMem (是另一个.) 此方法从 Kernel32.dll 公开 Win32 LocalAlloc 函数。

调用LocalAllocAllocHGlobal,它会传递一个LMEM_FIXED标志,这会导致分配的内存被锁定到位。 此外,分配的内存不会为零填充。

有关代码示例,请参阅 MarshalAllocHGlobal

另请参阅

适用于