共用方式為


Marshal.WriteInt32 方法

定義

將 32 位元帶正負號的整數值寫入 Unmanaged 記憶體中。 支援寫入未配置的記憶體位置。

多載

WriteInt32(Object, Int32, Int32)
已淘汰.

在指定的位移,將 32 位元帶正負號的整數值寫入 Unmanaged 記憶體。

WriteInt32(IntPtr, Int32)

將 32 位元帶正負號的整數值寫入 Unmanaged 記憶體中。

WriteInt32(IntPtr, Int32, Int32)

將 32 位元帶正負號的整數值寫入 Unmanaged 記憶體中的指定位移。

WriteInt32(Object, Int32, Int32)

來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs
來源:
Marshal.CoreCLR.cs

警告

WriteInt32(Object, Int32, Int32) may be unavailable in future releases.

在指定的位移,將 32 位元帶正負號的整數值寫入 Unmanaged 記憶體。

public:
 static void WriteInt32(System::Object ^ ptr, int ofs, int val);
[System.Obsolete("WriteInt32(Object, Int32, Int32) may be unavailable in future releases.")]
[System.Security.SecurityCritical]
public static void WriteInt32 (object ptr, int ofs, int val);
[System.Obsolete("WriteInt32(Object, Int32, Int32) may be unavailable in future releases.")]
public static void WriteInt32 (object ptr, int ofs, int val);
public static void WriteInt32 (object ptr, int ofs, int val);
[System.Security.SecurityCritical]
public static void WriteInt32 (object ptr, int ofs, int val);
[<System.Obsolete("WriteInt32(Object, Int32, Int32) may be unavailable in future releases.")>]
[<System.Security.SecurityCritical>]
static member WriteInt32 : obj * int * int -> unit
[<System.Obsolete("WriteInt32(Object, Int32, Int32) may be unavailable in future releases.")>]
static member WriteInt32 : obj * int * int -> unit
static member WriteInt32 : obj * int * int -> unit
[<System.Security.SecurityCritical>]
static member WriteInt32 : obj * int * int -> unit
Public Shared Sub WriteInt32 (ptr As Object, ofs As Integer, val As Integer)

參數

ptr
Object

目標物件之 Unmanaged 記憶體中的基底位址。

ofs
Int32

額外的位元組位移,會先加入至參數 ptr,然後再進行寫入。

val
Int32

要寫入的值。

屬性

例外狀況

基底位址 (ptr) 加上位移位元組 (ofs) 會產生 Null 或無效的位址。

ptrArrayWithOffset 物件。 這個方法不會接受 ArrayWithOffset 參數。

備註

WriteInt32 可讓您直接與 Unmanaged 32 位帶正負號的陣列互動,避免) 在設定其元素值之前,先將 Marshal.Copy 整個 Unmanaged 陣組複製 ( (。

支援寫入未配置的記憶體位置。

另請參閱

適用於

WriteInt32(IntPtr, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

將 32 位元帶正負號的整數值寫入 Unmanaged 記憶體中。

public:
 static void WriteInt32(IntPtr ptr, int val);
[System.Security.SecurityCritical]
public static void WriteInt32 (IntPtr ptr, int val);
public static void WriteInt32 (IntPtr ptr, int val);
[<System.Security.SecurityCritical>]
static member WriteInt32 : nativeint * int -> unit
static member WriteInt32 : nativeint * int -> unit
Public Shared Sub WriteInt32 (ptr As IntPtr, val As Integer)

參數

ptr
IntPtr

nativeint

Unmanaged 記憶體中要寫入的位址。

val
Int32

要寫入的值。

屬性

例外狀況

ptr 不是可辨認的格式。

-或-

ptrnull

-或-

ptr 無效。

範例

下列範例示範如何使用 和 WriteInt32 方法來讀取和寫入 Unmanaged 陣列ReadInt32

static void ReadWriteInt32()
{
    // Allocate unmanaged memory. 
    int elementSize = 4;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt32(unmanagedArray, i * elementSize, ((Int32)(i + 1)));
    }
    Console.WriteLine("Unmanaged memory written.");

    Console.WriteLine("Reading unmanaged memory:");
    // Print the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(Marshal.ReadInt32(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt32()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 4
    Dim unmanagedArray As IntPtr = Marshal.AllocHGlobal(10 * elementSize)

    ' Set the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Marshal.WriteInt32(unmanagedArray, i * elementSize, CType(i + 1, Int32))
    Next i
    Console.WriteLine("Unmanaged memory written.")

    Console.WriteLine("Reading unmanaged memory:")
    ' Print the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Console.WriteLine(Marshal.ReadInt32(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

備註

WriteInt32 可讓您直接與 Unmanaged 32 位帶正負號的陣列互動,避免) 在設定其元素值之前,先將 Marshal.Copy 整個 Unmanaged 陣組複製 ( (。

支援寫入未配置的記憶體位置。

另請參閱

適用於

WriteInt32(IntPtr, Int32, Int32)

來源:
Marshal.cs
來源:
Marshal.cs
來源:
Marshal.cs

將 32 位元帶正負號的整數值寫入 Unmanaged 記憶體中的指定位移。

public:
 static void WriteInt32(IntPtr ptr, int ofs, int val);
[System.Security.SecurityCritical]
public static void WriteInt32 (IntPtr ptr, int ofs, int val);
public static void WriteInt32 (IntPtr ptr, int ofs, int val);
[<System.Security.SecurityCritical>]
static member WriteInt32 : nativeint * int * int -> unit
static member WriteInt32 : nativeint * int * int -> unit
Public Shared Sub WriteInt32 (ptr As IntPtr, ofs As Integer, val As Integer)

參數

ptr
IntPtr

nativeint

Unmanaged 記憶體中要寫入的基底位址。

ofs
Int32

額外的位元組位移,會先加入至參數 ptr,然後再進行寫入。

val
Int32

要寫入的值。

屬性

例外狀況

基底位址 (ptr) 加上位移位元組 (ofs) 會產生 Null 或無效的位址。

範例

下列範例示範如何使用 和 WriteInt32 方法來讀取和寫入 Unmanaged 陣列ReadInt32

static void ReadWriteInt32()
{
    // Allocate unmanaged memory. 
    int elementSize = 4;
    IntPtr unmanagedArray = Marshal.AllocHGlobal(10 * elementSize);

    // Set the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Marshal.WriteInt32(unmanagedArray, i * elementSize, ((Int32)(i + 1)));
    }
    Console.WriteLine("Unmanaged memory written.");

    Console.WriteLine("Reading unmanaged memory:");
    // Print the 10 elements of the C-style unmanagedArray
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine(Marshal.ReadInt32(unmanagedArray, i * elementSize));
    }

    Marshal.FreeHGlobal(unmanagedArray);

    Console.WriteLine("Done. Press Enter to continue.");
    Console.ReadLine();
}
Sub ReadWriteInt32()
    ' Allocate unmanaged memory. 
    Dim elementSize As Integer = 4
    Dim unmanagedArray As IntPtr = Marshal.AllocHGlobal(10 * elementSize)

    ' Set the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Marshal.WriteInt32(unmanagedArray, i * elementSize, CType(i + 1, Int32))
    Next i
    Console.WriteLine("Unmanaged memory written.")

    Console.WriteLine("Reading unmanaged memory:")
    ' Print the 10 elements of the C-style unmanagedArray
    For i As Integer = 0 To 9
        Console.WriteLine(Marshal.ReadInt32(unmanagedArray, i * elementSize))
    Next i

    Marshal.FreeHGlobal(unmanagedArray)

    Console.WriteLine("Done. Press Enter to continue.")
    Console.ReadLine()
End Sub

備註

WriteInt32 可讓您直接與 Unmanaged 32 位帶正負號的陣列互動,避免) 在設定其元素值之前,先將 Marshal.Copy 整個 Unmanaged 陣組複製 ( (。

支援寫入未配置的記憶體位置。

另請參閱

適用於