Marshal.AllocHGlobal 方法

定義

從處理序的 Unmanaged 記憶體來配置記憶體。

多載

AllocHGlobal(Int32)

使用指定的位元組數目,從處理序的 Unmanaged 記憶體中配置記憶體。

AllocHGlobal(IntPtr)

使用指定數目的位元組指標,從處理序的 Unmanaged 記憶體中配置記憶體。

AllocHGlobal(Int32)

使用指定的位元組數目,從處理序的 Unmanaged 記憶體中配置記憶體。

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 (是 other.) 這個方法會從 Kernel32.dll 公開 Win32 LocalAlloc函式。

呼叫 LocalAllocAllocHGlobal ,它會傳遞 LMEM_FIXED 旗標,這會導致配置記憶體就地鎖定。 此外,配置的記憶體不會填滿零。

另請參閱

適用於

AllocHGlobal(IntPtr)

使用指定數目的位元組指標,從處理序的 Unmanaged 記憶體中配置記憶體。

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 (是 other.) 這個方法會從 Kernel32.dll 公開 Win32 LocalAlloc函式。

呼叫 LocalAllocAllocHGlobal ,它會傳遞 LMEM_FIXED 旗標,這會導致配置記憶體就地鎖定。 此外,配置的記憶體不會填滿零。

如需範例程式碼,請參閱 MarshalAllocHGlobal

另請參閱

適用於