IntPtr 結構

定義

表示帶正負號的整數,其中位寬度與指標相同。

public value class IntPtr
public value class IntPtr : IComparable, IComparable<IntPtr>, IEquatable<IntPtr>, IFormattable, System::Runtime::Serialization::ISerializable
public value class IntPtr : IComparable, IComparable<IntPtr>, IEquatable<IntPtr>, ISpanFormattable, System::Runtime::Serialization::ISerializable
public value class IntPtr : IComparable<IntPtr>, IEquatable<IntPtr>, IParsable<IntPtr>, ISpanParsable<IntPtr>, System::Numerics::IAdditionOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IAdditiveIdentity<IntPtr, IntPtr>, System::Numerics::IBinaryInteger<IntPtr>, System::Numerics::IBinaryNumber<IntPtr>, System::Numerics::IBitwiseOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IComparisonOperators<IntPtr, IntPtr, bool>, System::Numerics::IDecrementOperators<IntPtr>, System::Numerics::IDivisionOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IEqualityOperators<IntPtr, IntPtr, bool>, System::Numerics::IIncrementOperators<IntPtr>, System::Numerics::IMinMaxValue<IntPtr>, System::Numerics::IModulusOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IMultiplicativeIdentity<IntPtr, IntPtr>, System::Numerics::IMultiplyOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::INumber<IntPtr>, System::Numerics::INumberBase<IntPtr>, System::Numerics::IShiftOperators<IntPtr, int, IntPtr>, System::Numerics::ISignedNumber<IntPtr>, System::Numerics::ISubtractionOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IUnaryNegationOperators<IntPtr, IntPtr>, System::Numerics::IUnaryPlusOperators<IntPtr, IntPtr>, System::Runtime::Serialization::ISerializable
public value class IntPtr : IComparable<IntPtr>, IEquatable<IntPtr>, IParsable<IntPtr>, ISpanParsable<IntPtr>, IUtf8SpanParsable<IntPtr>, System::Numerics::IAdditionOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IAdditiveIdentity<IntPtr, IntPtr>, System::Numerics::IBinaryInteger<IntPtr>, System::Numerics::IBinaryNumber<IntPtr>, System::Numerics::IBitwiseOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IComparisonOperators<IntPtr, IntPtr, bool>, System::Numerics::IDecrementOperators<IntPtr>, System::Numerics::IDivisionOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IEqualityOperators<IntPtr, IntPtr, bool>, System::Numerics::IIncrementOperators<IntPtr>, System::Numerics::IMinMaxValue<IntPtr>, System::Numerics::IModulusOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IMultiplicativeIdentity<IntPtr, IntPtr>, System::Numerics::IMultiplyOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::INumber<IntPtr>, System::Numerics::INumberBase<IntPtr>, System::Numerics::IShiftOperators<IntPtr, int, IntPtr>, System::Numerics::ISignedNumber<IntPtr>, System::Numerics::ISubtractionOperators<IntPtr, IntPtr, IntPtr>, System::Numerics::IUnaryNegationOperators<IntPtr, IntPtr>, System::Numerics::IUnaryPlusOperators<IntPtr, IntPtr>, System::Runtime::Serialization::ISerializable
public value class IntPtr : IEquatable<IntPtr>, System::Runtime::Serialization::ISerializable
public value class IntPtr : System::Runtime::Serialization::ISerializable
public struct IntPtr
public readonly struct IntPtr : IComparable, IComparable<IntPtr>, IEquatable<IntPtr>, IFormattable, System.Runtime.Serialization.ISerializable
public readonly struct IntPtr : IComparable, IComparable<IntPtr>, IEquatable<IntPtr>, ISpanFormattable, System.Runtime.Serialization.ISerializable
public readonly struct IntPtr : IComparable<IntPtr>, IEquatable<IntPtr>, IParsable<IntPtr>, ISpanParsable<IntPtr>, System.Numerics.IAdditionOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IAdditiveIdentity<IntPtr,IntPtr>, System.Numerics.IBinaryInteger<IntPtr>, System.Numerics.IBinaryNumber<IntPtr>, System.Numerics.IBitwiseOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IComparisonOperators<IntPtr,IntPtr,bool>, System.Numerics.IDecrementOperators<IntPtr>, System.Numerics.IDivisionOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IEqualityOperators<IntPtr,IntPtr,bool>, System.Numerics.IIncrementOperators<IntPtr>, System.Numerics.IMinMaxValue<IntPtr>, System.Numerics.IModulusOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IMultiplicativeIdentity<IntPtr,IntPtr>, System.Numerics.IMultiplyOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.INumber<IntPtr>, System.Numerics.INumberBase<IntPtr>, System.Numerics.IShiftOperators<IntPtr,int,IntPtr>, System.Numerics.ISignedNumber<IntPtr>, System.Numerics.ISubtractionOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IUnaryNegationOperators<IntPtr,IntPtr>, System.Numerics.IUnaryPlusOperators<IntPtr,IntPtr>, System.Runtime.Serialization.ISerializable
public readonly struct IntPtr : IComparable<IntPtr>, IEquatable<IntPtr>, IParsable<IntPtr>, ISpanParsable<IntPtr>, IUtf8SpanParsable<IntPtr>, System.Numerics.IAdditionOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IAdditiveIdentity<IntPtr,IntPtr>, System.Numerics.IBinaryInteger<IntPtr>, System.Numerics.IBinaryNumber<IntPtr>, System.Numerics.IBitwiseOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IComparisonOperators<IntPtr,IntPtr,bool>, System.Numerics.IDecrementOperators<IntPtr>, System.Numerics.IDivisionOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IEqualityOperators<IntPtr,IntPtr,bool>, System.Numerics.IIncrementOperators<IntPtr>, System.Numerics.IMinMaxValue<IntPtr>, System.Numerics.IModulusOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IMultiplicativeIdentity<IntPtr,IntPtr>, System.Numerics.IMultiplyOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.INumber<IntPtr>, System.Numerics.INumberBase<IntPtr>, System.Numerics.IShiftOperators<IntPtr,int,IntPtr>, System.Numerics.ISignedNumber<IntPtr>, System.Numerics.ISubtractionOperators<IntPtr,IntPtr,IntPtr>, System.Numerics.IUnaryNegationOperators<IntPtr,IntPtr>, System.Numerics.IUnaryPlusOperators<IntPtr,IntPtr>, System.Runtime.Serialization.ISerializable
public struct IntPtr : IEquatable<IntPtr>, System.Runtime.Serialization.ISerializable
public readonly struct IntPtr : IEquatable<IntPtr>, System.Runtime.Serialization.ISerializable
[System.Serializable]
public struct IntPtr : System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct IntPtr : System.Runtime.Serialization.ISerializable
public struct IntPtr : System.Runtime.Serialization.ISerializable
type nativeint = struct
type nativeint = struct
    interface IFormattable
    interface ISerializable
type nativeint = struct
    interface ISpanFormattable
    interface IFormattable
    interface ISerializable
type nativeint = struct
    interface IFormattable
    interface IParsable<nativeint>
    interface ISpanFormattable
    interface ISpanParsable<nativeint>
    interface IAdditionOperators<nativeint, nativeint, nativeint>
    interface IAdditiveIdentity<nativeint, nativeint>
    interface IBinaryInteger<nativeint>
    interface IBinaryNumber<nativeint>
    interface IBitwiseOperators<nativeint, nativeint, nativeint>
    interface IComparisonOperators<nativeint, nativeint, bool>
    interface IEqualityOperators<nativeint, nativeint, bool>
    interface IDecrementOperators<nativeint>
    interface IDivisionOperators<nativeint, nativeint, nativeint>
    interface IIncrementOperators<nativeint>
    interface IModulusOperators<nativeint, nativeint, nativeint>
    interface IMultiplicativeIdentity<nativeint, nativeint>
    interface IMultiplyOperators<nativeint, nativeint, nativeint>
    interface INumber<nativeint>
    interface INumberBase<nativeint>
    interface ISubtractionOperators<nativeint, nativeint, nativeint>
    interface IUnaryNegationOperators<nativeint, nativeint>
    interface IUnaryPlusOperators<nativeint, nativeint>
    interface IShiftOperators<nativeint, int, nativeint>
    interface IMinMaxValue<nativeint>
    interface ISignedNumber<nativeint>
    interface ISerializable
type nativeint = struct
    interface IFormattable
    interface IParsable<nativeint>
    interface ISpanFormattable
    interface ISpanParsable<nativeint>
    interface IAdditionOperators<nativeint, nativeint, nativeint>
    interface IAdditiveIdentity<nativeint, nativeint>
    interface IBinaryInteger<nativeint>
    interface IBinaryNumber<nativeint>
    interface IBitwiseOperators<nativeint, nativeint, nativeint>
    interface IComparisonOperators<nativeint, nativeint, bool>
    interface IEqualityOperators<nativeint, nativeint, bool>
    interface IDecrementOperators<nativeint>
    interface IDivisionOperators<nativeint, nativeint, nativeint>
    interface IIncrementOperators<nativeint>
    interface IModulusOperators<nativeint, nativeint, nativeint>
    interface IMultiplicativeIdentity<nativeint, nativeint>
    interface IMultiplyOperators<nativeint, nativeint, nativeint>
    interface INumber<nativeint>
    interface INumberBase<nativeint>
    interface ISubtractionOperators<nativeint, nativeint, nativeint>
    interface IUnaryNegationOperators<nativeint, nativeint>
    interface IUnaryPlusOperators<nativeint, nativeint>
    interface IUtf8SpanFormattable
    interface IUtf8SpanParsable<nativeint>
    interface IShiftOperators<nativeint, int, nativeint>
    interface IMinMaxValue<nativeint>
    interface ISignedNumber<nativeint>
    interface ISerializable
type nativeint = struct
    interface IFormattable
    interface IParsable<nativeint>
    interface ISpanFormattable
    interface ISpanParsable<nativeint>
    interface IUtf8SpanFormattable
    interface IUtf8SpanParsable<nativeint>
    interface IAdditionOperators<nativeint, nativeint, nativeint>
    interface IAdditiveIdentity<nativeint, nativeint>
    interface IBinaryInteger<nativeint>
    interface IBinaryNumber<nativeint>
    interface IBitwiseOperators<nativeint, nativeint, nativeint>
    interface IComparisonOperators<nativeint, nativeint, bool>
    interface IEqualityOperators<nativeint, nativeint, bool>
    interface IDecrementOperators<nativeint>
    interface IDivisionOperators<nativeint, nativeint, nativeint>
    interface IIncrementOperators<nativeint>
    interface IModulusOperators<nativeint, nativeint, nativeint>
    interface IMultiplicativeIdentity<nativeint, nativeint>
    interface IMultiplyOperators<nativeint, nativeint, nativeint>
    interface INumber<nativeint>
    interface INumberBase<nativeint>
    interface ISubtractionOperators<nativeint, nativeint, nativeint>
    interface IUnaryNegationOperators<nativeint, nativeint>
    interface IUnaryPlusOperators<nativeint, nativeint>
    interface IShiftOperators<nativeint, int, nativeint>
    interface IMinMaxValue<nativeint>
    interface ISignedNumber<nativeint>
    interface ISerializable
type nativeint = struct
    interface ISerializable
[<System.Serializable>]
type nativeint = struct
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type nativeint = struct
    interface ISerializable
Public Structure IntPtr
Public Structure IntPtr
Implements IComparable, IComparable(Of IntPtr), IEquatable(Of IntPtr), IFormattable, ISerializable
Public Structure IntPtr
Implements IComparable, IComparable(Of IntPtr), IEquatable(Of IntPtr), ISerializable, ISpanFormattable
Public Structure IntPtr
Implements IAdditionOperators(Of IntPtr, IntPtr, IntPtr), IAdditiveIdentity(Of IntPtr, IntPtr), IBinaryInteger(Of IntPtr), IBinaryNumber(Of IntPtr), IBitwiseOperators(Of IntPtr, IntPtr, IntPtr), IComparable(Of IntPtr), IComparisonOperators(Of IntPtr, IntPtr, Boolean), IDecrementOperators(Of IntPtr), IDivisionOperators(Of IntPtr, IntPtr, IntPtr), IEqualityOperators(Of IntPtr, IntPtr, Boolean), IEquatable(Of IntPtr), IIncrementOperators(Of IntPtr), IMinMaxValue(Of IntPtr), IModulusOperators(Of IntPtr, IntPtr, IntPtr), IMultiplicativeIdentity(Of IntPtr, IntPtr), IMultiplyOperators(Of IntPtr, IntPtr, IntPtr), INumber(Of IntPtr), INumberBase(Of IntPtr), IParsable(Of IntPtr), ISerializable, IShiftOperators(Of IntPtr, Integer, IntPtr), ISignedNumber(Of IntPtr), ISpanParsable(Of IntPtr), ISubtractionOperators(Of IntPtr, IntPtr, IntPtr), IUnaryNegationOperators(Of IntPtr, IntPtr), IUnaryPlusOperators(Of IntPtr, IntPtr)
Public Structure IntPtr
Implements IAdditionOperators(Of IntPtr, IntPtr, IntPtr), IAdditiveIdentity(Of IntPtr, IntPtr), IBinaryInteger(Of IntPtr), IBinaryNumber(Of IntPtr), IBitwiseOperators(Of IntPtr, IntPtr, IntPtr), IComparable(Of IntPtr), IComparisonOperators(Of IntPtr, IntPtr, Boolean), IDecrementOperators(Of IntPtr), IDivisionOperators(Of IntPtr, IntPtr, IntPtr), IEqualityOperators(Of IntPtr, IntPtr, Boolean), IEquatable(Of IntPtr), IIncrementOperators(Of IntPtr), IMinMaxValue(Of IntPtr), IModulusOperators(Of IntPtr, IntPtr, IntPtr), IMultiplicativeIdentity(Of IntPtr, IntPtr), IMultiplyOperators(Of IntPtr, IntPtr, IntPtr), INumber(Of IntPtr), INumberBase(Of IntPtr), IParsable(Of IntPtr), ISerializable, IShiftOperators(Of IntPtr, Integer, IntPtr), ISignedNumber(Of IntPtr), ISpanParsable(Of IntPtr), ISubtractionOperators(Of IntPtr, IntPtr, IntPtr), IUnaryNegationOperators(Of IntPtr, IntPtr), IUnaryPlusOperators(Of IntPtr, IntPtr), IUtf8SpanParsable(Of IntPtr)
Public Structure IntPtr
Implements IEquatable(Of IntPtr), ISerializable
Public Structure IntPtr
Implements ISerializable
繼承
IntPtr
屬性
實作
IComparable IComparable<IntPtr> IEquatable<IntPtr> IFormattable ISerializable ISpanFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<IntPtr> IParsable<TSelf> ISpanParsable<IntPtr> ISpanParsable<TSelf> IAdditionOperators<IntPtr,IntPtr,IntPtr> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<IntPtr,IntPtr> IAdditiveIdentity<TSelf,TSelf> IBinaryInteger<IntPtr> IBinaryNumber<IntPtr> IBinaryNumber<TSelf> IBitwiseOperators<IntPtr,IntPtr,IntPtr> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<IntPtr,IntPtr,Boolean> IComparisonOperators<TSelf,TSelf,Boolean> IDecrementOperators<IntPtr> IDecrementOperators<TSelf> IDivisionOperators<IntPtr,IntPtr,IntPtr> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<IntPtr,IntPtr,Boolean> IEqualityOperators<TSelf,TOther,TResult> IEqualityOperators<TSelf,TSelf,Boolean> IIncrementOperators<IntPtr> IIncrementOperators<TSelf> IMinMaxValue<IntPtr> IModulusOperators<IntPtr,IntPtr,IntPtr> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<IntPtr,IntPtr> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<IntPtr,IntPtr,IntPtr> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<IntPtr> INumber<TSelf> INumberBase<IntPtr> INumberBase<TSelf> IShiftOperators<IntPtr,Int32,IntPtr> IShiftOperators<TSelf,Int32,TSelf> ISignedNumber<IntPtr> ISubtractionOperators<IntPtr,IntPtr,IntPtr> ISubtractionOperators<TSelf,TSelf,TSelf> IUnaryNegationOperators<IntPtr,IntPtr> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<IntPtr,IntPtr> IUnaryPlusOperators<TSelf,TSelf> IUtf8SpanFormattable IUtf8SpanParsable<IntPtr> IUtf8SpanParsable<TSelf>

範例

下列範例會使用 Managed 指標來反轉陣列中的字元。 初始化 String 物件並取得其長度之後,它會執行下列動作:

  1. 呼叫 方法, Marshal.StringToHGlobalAnsi 將 Unicode 字串複製到 Unmanaged 記憶體,做為 ANSI (一位元組) 字元。 方法會傳 IntPtr 回指向 Unmanaged 字串開頭的物件。 Visual Basic 範例會直接使用此指標;在 C++、F# 和 C# 範例中,它會轉換成位元組的指標。

  2. Marshal.AllocHGlobal呼叫 方法,以配置與 Unmanaged 字串佔用相同的位元組數目。 方法會傳 IntPtr 回指向 Unmanaged 記憶體區塊開頭的物件。 Visual Basic 範例會直接使用此指標;在 C++、F# 和 C# 範例中,它會轉換成位元組的指標。

  3. Visual Basic 範例會定義名為 offset 的變數,其等於 ANSI 字串的長度。 用來判斷將 ANSI 字串中下一個字元複製到 Unmanaged 記憶體中的位移。 因為其起始值是字串的長度,所以複製作業會將字元從字串開頭複製到記憶體區塊結尾。

    C#、F# 和 C++ 範例會 ToPointer 呼叫 方法,以取得字串起始位址和非受控記憶體區塊的 Unmanaged 指標,並將一個小於字串長度的字串新增至 ANSI 字串的起始位址。 因為 Unmanaged 字串指標現在指向字串的結尾,所以複製作業會將字元從字串結尾複製到記憶體區塊的開頭。

  4. 使用 迴圈,將每個字元從字串複製到記憶體的 Unmanaged 區塊。

    Visual Basic 範例會 Marshal.ReadByte(IntPtr, Int32) 呼叫 方法,以從 Managed 指標到 ANSI 字串的指定位移讀取位元組 (或一位元組字元) 。 此位移會隨著迴圈的每個反復專案遞增。 然後它會呼叫 Marshal.WriteByte(IntPtr, Int32, Byte) 方法,將位元組寫入記憶體位址,該位址是由 Unmanaged 記憶體區塊的起始位址加上 offset 所定義的記憶體位址。 然後遞減 offset

    C#、F# 和 C++ 範例會執行複製作業,然後將 Unmanaged ANSI 字串中下一個位置的指標遞減,並將指標遞增至 Unmanaged 區塊中的下一個位址。

  5. 所有範例都會呼叫 , Marshal.PtrToStringAnsi 將包含複製 ANSI 字串的 Unmanaged 記憶體區塊轉換成 Managed Unicode String 物件。

  6. 顯示原始和反轉字串之後,所有範例都會呼叫 FreeHGlobal 方法來釋放配置給 Unmanaged ANSI 字串和 Unmanaged 記憶體區塊的記憶體。

using namespace System;
using namespace System::Runtime::InteropServices;

class NotTooSafeStringReverse
{
public:
    static void Main()
    {
        String^ stringA = "I seem to be turned around!";
        int copylen = stringA->Length;

        // Allocate HGlobal memory for source and destination strings
        IntPtr sptr = Marshal::StringToHGlobalAnsi(stringA);
        IntPtr dptr = Marshal::AllocHGlobal(copylen + 1);

        char *src = (char *)sptr.ToPointer();
        char *dst = (char *)dptr.ToPointer();

        if (copylen > 0)
        {
            // set the source pointer to the end of the string
            // to do a reverse copy.
            src += copylen - 1;

            while (copylen-- > 0)
            {
                *dst++ = *src--;
            }
            *dst = 0;
        }
        String^ stringB = Marshal::PtrToStringAnsi(dptr);

        Console::WriteLine("Original:\n{0}\n", stringA);
        Console::WriteLine("Reversed:\n{0}", stringB);

        // Free HGlobal memory
        Marshal::FreeHGlobal(dptr);
        Marshal::FreeHGlobal(sptr);
    }
};

int main()
{
    NotTooSafeStringReverse::Main();
}

// The progam has the following output:
//
// Original:
// I seem to be turned around!
//
// Reversed:
// !dnuora denrut eb ot mees I
using System;
using System.Runtime.InteropServices;

class NotTooSafeStringReverse
{
    static public void Main()
    {
        string stringA = "I seem to be turned around!";
        int copylen = stringA.Length;

        // Allocate HGlobal memory for source and destination strings
        IntPtr sptr = Marshal.StringToHGlobalAnsi(stringA);
        IntPtr dptr = Marshal.AllocHGlobal(copylen + 1);

        // The unsafe section where byte pointers are used.
        unsafe
        {
            byte *src = (byte *)sptr.ToPointer();
            byte *dst = (byte *)dptr.ToPointer();

            if (copylen > 0)
            {
                // set the source pointer to the end of the string
                // to do a reverse copy.
                src += copylen - 1;

                while (copylen-- > 0)
                {
                    *dst++ = *src--;
                }
                *dst = 0;
            }
        }
        string stringB = Marshal.PtrToStringAnsi(dptr);

        Console.WriteLine("Original:\n{0}\n", stringA);
        Console.WriteLine("Reversed:\n{0}", stringB);

        // Free HGlobal memory
        Marshal.FreeHGlobal(dptr);
        Marshal.FreeHGlobal(sptr);
    }
}

// The progam has the following output:
//
// Original:
// I seem to be turned around!
//
// Reversed:
// !dnuora denrut eb ot mees I
#nowarn "9"
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ = 
    let stringA = "I seem to be turned around!"
    let mutable copylen = stringA.Length

    // Allocate HGlobal memory for source and destination strings
    let sptr = Marshal.StringToHGlobalAnsi stringA
    let dptr = Marshal.AllocHGlobal(copylen + 1)

    let mutable src: byte nativeptr = sptr.ToPointer() |> NativePtr.ofVoidPtr
    let mutable dst: byte nativeptr = dptr.ToPointer() |> NativePtr.ofVoidPtr

    if copylen > 0 then
        // set the source pointer to the end of the string
        // to do a reverse copy.
        src <- 
            NativePtr.toNativeInt src + nativeint (copylen - 1) 
            |> NativePtr.ofNativeInt

        while copylen > 0 do
            copylen <- copylen - 1
            NativePtr.read src |> NativePtr.write dst
            dst <- NativePtr.toNativeInt dst + 1n |> NativePtr.ofNativeInt
            src <- NativePtr.toNativeInt src - 1n |> NativePtr.ofNativeInt
        NativePtr.write dst 0uy

    let stringB = Marshal.PtrToStringAnsi dptr

    printfn $"Original:\n{stringA}\n"
    printfn $"Reversed:\n{stringB}"

    // Free HGlobal memory
    Marshal.FreeHGlobal dptr
    Marshal.FreeHGlobal sptr
    0

// The progam has the following output:
//
// Original:
// I seem to be turned around!
//
// Reversed:
// !dnuora denrut eb ot mees I
Imports System.Runtime.InteropServices

Public Module Example
    Public Sub Main()
        Dim stringA As String = "I seem to be turned around!"
        Dim copylen As Integer = stringA.Length

        ' Allocate HGlobal memory for source and destination strings
        Dim sptr As IntPtr = Marshal.StringToHGlobalAnsi(stringA)
        Dim dptr As IntPtr = Marshal.AllocHGlobal(copylen)
        Dim offset As Integer = copylen - 1

         For ctr As Integer = 0 To copylen - 1
            Dim b As Byte = Marshal.ReadByte(sptr, ctr)
            Marshal.WriteByte(dptr, offset, b)
            offset -= 1
         Next

        Dim stringB As String = Marshal.PtrToStringAnsi(dptr)

        Console.WriteLine("Original:{1}{0}{1}", stringA, vbCrLf)
        Console.WriteLine("Reversed:{1}{0}{1}", stringB, vbCrLf)

        ' Free HGlobal memory
        Marshal.FreeHGlobal(dptr)
        Marshal.FreeHGlobal(sptr)
    End Sub
End Module
' The example displays the following output:
'       Original:
'       I seem to be turned around!
'
'       Reversed:
'       !dnuora denrut eb ot mees I

備註

IntPtr 類型設計成整數,其大小與指標相同。 也就是說,此類型的實例預期在 32 位進程中為 32 位,而 64 位進程則為 64 位。

IntPtr 類型可由支援指標的語言使用,並做為參考不支援指標之語言之間資料的常見方法。

IntPtr 物件也可以用來保存控制碼。 例如,的 IntPtr 實例會在 類別中 System.IO.FileStream 廣泛使用,以保存檔案控制代碼。

注意

使用 IntPtr 作為指標或控制碼很容易出錯且不安全。 它只是整數類型,因為大小相同,可用來做為指標和控制碼的交換格式。 在特定的交換需求之外,例如將資料傳遞至不支援指標的語言,應該使用正確型別的指標來表示指標, SafeHandle 而且應該用來表示控制碼。

此類型會實作 ISerializable 。 在 .NET 5 和更新版本中,此類型也會實作 IFormattable 介面。 在 .NET 7 和更新版本中,此類型也會實作 IBinaryInteger<TSelf>IMinMaxValue<TSelf>ISignedNumber<TSelf> 介面。

從 9.0 版開始的 C# 中,您可以使用內 nint 建類型來定義原生大小的整數。 此類型是以內部類型表示, IntPtr 並提供適合整數類型的作業和轉換。 如需詳細資訊,請參閱 nint 和 nuint 類型

從 11 版開始,在以 .NET 7 或更新版本執行時間為目標的 C# 中,是 的別名, nintint 與 的別名 IntPtrInt32 相同。

建構函式

IntPtr(Int32)

使用指定的 32 位帶正負號整數,初始化 的新實例 IntPtr

IntPtr(Int64)

使用指定的 64 位帶正負號整數,初始化 的新實例 IntPtr

IntPtr(Void*)

對未指定的類型使用指定的指標,初始化 IntPtr 的新執行個體。

欄位

Zero

唯讀欄位,表示已初始化為零的帶正負號整數。

屬性

MaxValue

取得 的最大可能值 IntPtr

MinValue

取得 的最小可能值 IntPtr

Size

取得這個執行個體的大小。

方法

Abs(IntPtr)

計算值的絕對值。

Add(IntPtr, Int32)

將位移加入帶正負號的整數。

Clamp(IntPtr, IntPtr, IntPtr)

將值限制為內含最小值和最大值。

CompareTo(IntPtr)

將目前的執行個體與相同類型的另一個物件相比較,並傳回整數,這個整數表示目前的執行個體在排序次序中,位於另一個物件之前、之後或相同位置。

CompareTo(Object)

將目前的執行個體與相同類型的另一個物件相比較,並傳回整數,這個整數表示目前的執行個體在排序次序中,位於另一個物件之前、之後或相同位置。

CopySign(IntPtr, IntPtr)

將值的正負號複製到另一個值的正負號。

CreateChecked<TOther>(TOther)

從值建立目前類型的實例,針對落在目前類型可表示範圍以外的任何值擲回溢位例外狀況。

CreateSaturating<TOther>(TOther)

從值建立目前型別的實例,使落在目前類型可表示範圍以外的任何值飽和。

CreateTruncating<TOther>(TOther)

從值建立目前型別的實例,截斷落在目前類型可表示範圍以外的任何值。

DivRem(IntPtr, IntPtr)

計算兩個值的商數和餘數。

Equals(IntPtr)

指出目前的物件是否等於另一個相同類型的物件。

Equals(Object)

傳回值,該值表示這個執行個體是否和指定的物件相等。

GetHashCode()

傳回這個執行個體的雜湊碼。

IsEvenInteger(IntPtr)

判斷值是否代表偶數整數。

IsNegative(IntPtr)

判斷值是否為負值。

IsOddInteger(IntPtr)

判斷值是否代表奇數整數。

IsPositive(IntPtr)

判斷值是否為正數。

IsPow2(IntPtr)

判斷值是否為 2 的乘冪。

LeadingZeroCount(IntPtr)

計算值中前置零的數目。

Log2(IntPtr)

計算值的 log2。

Max(IntPtr, IntPtr)

比較兩個值與大於的計算。

MaxMagnitude(IntPtr, IntPtr)

比較兩個值與大於的計算。

Min(IntPtr, IntPtr)

比較兩個值與比較較少的計算。

MinMagnitude(IntPtr, IntPtr)

比較兩個值與比較較少的計算。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

將 UTF-8 字元的範圍剖析為值。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

將 UTF-8 字元的範圍剖析為值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將指定樣式和文化特性特定格式之數位字元表示的唯讀範圍,轉換為其帶正負號的原生整數對等專案。

Parse(String)

將數字的字串表示轉換成其對等帶正負號的原生整數。

Parse(String, IFormatProvider)

將指定特定文化特性 (Culture) 格式之數字的字串表示法轉換成其對等帶正負號的原生整數。

Parse(String, NumberStyles)

將指定樣式之數字的字串表示法轉換成其對等帶正負號的原生整數。

Parse(String, NumberStyles, IFormatProvider)

將指定樣式與文化特性 (Culture) 特定格式之數字的字串表示法轉換成其對等帶正負號的原生整數。

PopCount(IntPtr)

計算值中所設定的位數。

RotateLeft(IntPtr, Int32)

將值向左旋轉指定的數量。

RotateRight(IntPtr, Int32)

以指定的數量向右旋轉值。

Sign(IntPtr)

計算值的符號。

Subtract(IntPtr, Int32)

從帶正負號的整數減去位移。

ToInt32()

將這個執行個體的值轉換為 32 位元帶正負號的整數。

ToInt64()

將這個執行個體的值轉換為 64 位元帶正負號的整數。

ToPointer()

將這個執行個體的值轉換為未指定類型的指標。

ToString()

將目前 IntPtr 物件的數值,轉換為其相等的字串表示。

ToString(IFormatProvider)

使用指定的格式和特定文化特性格式資訊,將這個執行個體的數值轉換成它的相等字串表示。

ToString(String)

將目前 IntPtr 物件的數值,轉換為其相等的字串表示。

ToString(String, IFormatProvider)

使用指定的格式,格式化目前執行個體的值。

TrailingZeroCount(IntPtr)

計算值中尾端零的數目。

TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider)

嘗試將目前實例的值格式化為 UTF-8 到提供的位元組範圍。

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)

嘗試將目前實例的值格式化為提供的字元範圍。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, IntPtr)

嘗試將 UTF-8 字元的範圍剖析為值。

TryParse(ReadOnlySpan<Byte>, IntPtr)

嘗試將包含數位字串表示的 UTF-8 字元範圍轉換為其相等帶正負號的整數。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, IntPtr)

嘗試將 UTF-8 字元的範圍剖析為值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, IntPtr)

嘗試將字串剖析成值。

TryParse(ReadOnlySpan<Char>, IntPtr)

將數位之字元表示的唯讀範圍轉換為其帶正負號的原生整數對等專案。 傳回指示轉換是否成功的值。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, IntPtr)

將指定樣式和文化特性特定格式之數位字元表示的唯讀範圍,轉換為其帶正負號的原生整數對等專案。 傳回指示轉換是否成功的值。

TryParse(String, IFormatProvider, IntPtr)

嘗試將字串剖析成值。

TryParse(String, IntPtr)

將數字的字串表示轉換成其對等帶正負號的原生整數。 傳回指示轉換是否成功的值。

TryParse(String, NumberStyles, IFormatProvider, IntPtr)

將指定樣式與文化特性 (Culture) 特定格式之數字的字串表示法轉換成其對等帶正負號的原生整數。 傳回指示轉換是否成功的值。

運算子

Addition(IntPtr, Int32)

將位移加入帶正負號的整數。

Equality(IntPtr, IntPtr)

判斷 IntPtr 的兩個指定執行個體是否相等。

Explicit(Int32 to IntPtr)

將 32 位元帶正負號整數的值轉換為 IntPtr

Explicit(Int64 to IntPtr)

將 64 位元帶正負號整數的值轉換為 IntPtr

Explicit(IntPtr to Int32)

將指定的 IntPtr 值轉換為 32 位元帶正負號的整數。

Explicit(IntPtr to Int64)

將指定的 IntPtr 值轉換為 64 位元帶正負號的整數。

Explicit(IntPtr to Void*)

將指定的 IntPtr 值轉換為未指定的類型指標。

此應用程式開發介面不符合 CLS 標準。

Explicit(Void* to IntPtr)

將指定的指標轉換為 IntPtr 的未指定類型。

此應用程式開發介面不符合 CLS 標準。

Inequality(IntPtr, IntPtr)

判斷 IntPtr 的兩個指定執行個體是否不相等。

Subtraction(IntPtr, Int32)

從帶正負號的整數減去位移。

明確介面實作

IAdditionOperators<IntPtr,IntPtr,IntPtr>.Addition(IntPtr, IntPtr)

將兩個值相加以計算其總和。

IAdditionOperators<IntPtr,IntPtr,IntPtr>.CheckedAddition(IntPtr, IntPtr)

將兩個值相加以計算其總和。

IAdditiveIdentity<IntPtr,IntPtr>.AdditiveIdentity

取得目前型別的加法識別。

IBinaryInteger<IntPtr>.GetByteCount()

取得將寫入為 的 TryWriteLittleEndian(Span<Byte>, Int32) 位元組數目。

IBinaryInteger<IntPtr>.GetShortestBitLength()

取得目前值之最短兩個補數標記法的長度,以位為單位。

IBinaryInteger<IntPtr>.TryReadBigEndian(ReadOnlySpan<Byte>, Boolean, IntPtr)

表示帶正負號的整數,其中位寬度與指標相同。

IBinaryInteger<IntPtr>.TryReadLittleEndian(ReadOnlySpan<Byte>, Boolean, IntPtr)

表示帶正負號的整數,其中位寬度與指標相同。

IBinaryInteger<IntPtr>.TryWriteBigEndian(Span<Byte>, Int32)

嘗試以大到大到大的格式,將目前的值寫入指定的範圍。

IBinaryInteger<IntPtr>.TryWriteLittleEndian(Span<Byte>, Int32)

嘗試以位元組端格式將目前的值寫入指定的範圍。

IBinaryNumber<IntPtr>.AllBitsSet

取得已設定所有位的二進位型別實例。

IBitwiseOperators<IntPtr,IntPtr,IntPtr>.BitwiseAnd(IntPtr, IntPtr)

計算兩個值的位和 。

IBitwiseOperators<IntPtr,IntPtr,IntPtr>.BitwiseOr(IntPtr, IntPtr)

計算兩個值的位或 。

IBitwiseOperators<IntPtr,IntPtr,IntPtr>.ExclusiveOr(IntPtr, IntPtr)

計算兩個值的獨佔或兩個值。

IBitwiseOperators<IntPtr,IntPtr,IntPtr>.OnesComplement(IntPtr)

計算指定值的一補數標記法。

IComparisonOperators<IntPtr,IntPtr,Boolean>.GreaterThan(IntPtr, IntPtr)

比較兩個值,以判斷哪一個值更大。

IComparisonOperators<IntPtr,IntPtr,Boolean>.GreaterThanOrEqual(IntPtr, IntPtr)

比較兩個值,以判斷哪一個值大於或相等。

IComparisonOperators<IntPtr,IntPtr,Boolean>.LessThan(IntPtr, IntPtr)

比較兩個值,以判斷哪一個值較少。

IComparisonOperators<IntPtr,IntPtr,Boolean>.LessThanOrEqual(IntPtr, IntPtr)

比較兩個值,以判斷哪一個值小於或相等。

IDecrementOperators<IntPtr>.CheckedDecrement(IntPtr)

遞減值。

IDecrementOperators<IntPtr>.Decrement(IntPtr)

遞減值。

IDivisionOperators<IntPtr,IntPtr,IntPtr>.Division(IntPtr, IntPtr)

將一個值除以計算其商數。

IEquatable<IntPtr>.Equals(IntPtr)

傳回值,這個值表示這個實例是否等於另一個帶正負號的整數。

IIncrementOperators<IntPtr>.CheckedIncrement(IntPtr)

遞增值。

IIncrementOperators<IntPtr>.Increment(IntPtr)

遞增值。

IMinMaxValue<IntPtr>.MaxValue

取得目前型別的最大值。

IMinMaxValue<IntPtr>.MinValue

取得目前型別的最小值。

IModulusOperators<IntPtr,IntPtr,IntPtr>.Modulus(IntPtr, IntPtr)

將兩個值相除以計算其模數或餘數。

IMultiplicativeIdentity<IntPtr,IntPtr>.MultiplicativeIdentity

取得目前型別的乘法識別。

IMultiplyOperators<IntPtr,IntPtr,IntPtr>.CheckedMultiply(IntPtr, IntPtr)

將兩個值相乘以計算其乘積。

IMultiplyOperators<IntPtr,IntPtr,IntPtr>.Multiply(IntPtr, IntPtr)

將兩個值相乘以計算其乘積。

INumber<IntPtr>.MaxNumber(IntPtr, IntPtr)

比較兩個值與計算,如果輸入為 NaN ,則會傳回另一個值。

INumber<IntPtr>.MinNumber(IntPtr, IntPtr)

比較兩個值與計算,如果輸入 NaN 為 ,則會傳回另一個值。

INumberBase<IntPtr>.IsCanonical(IntPtr)

判斷值是否在其標準標記法中。

INumberBase<IntPtr>.IsComplexNumber(IntPtr)

判斷值是否代表複數。

INumberBase<IntPtr>.IsFinite(IntPtr)

判斷值是否有限。

INumberBase<IntPtr>.IsImaginaryNumber(IntPtr)

判斷值是否代表純虛數。

INumberBase<IntPtr>.IsInfinity(IntPtr)

判斷值是否為無限。

INumberBase<IntPtr>.IsInteger(IntPtr)

判斷值是否代表整數。

INumberBase<IntPtr>.IsNaN(IntPtr)

判斷值是否為 NaN。

INumberBase<IntPtr>.IsNegativeInfinity(IntPtr)

判斷值是否為負無限大。

INumberBase<IntPtr>.IsNormal(IntPtr)

判斷值是否正常。

INumberBase<IntPtr>.IsPositiveInfinity(IntPtr)

判斷值是否為正無限大。

INumberBase<IntPtr>.IsRealNumber(IntPtr)

判斷值是否代表實數。

INumberBase<IntPtr>.IsSubnormal(IntPtr)

判斷值是否為次正常。

INumberBase<IntPtr>.IsZero(IntPtr)

判斷值是否為零。

INumberBase<IntPtr>.MaxMagnitudeNumber(IntPtr, IntPtr)

比較兩個值與具有較大大小的計算,如果輸入為 NaN ,則會傳回另一個值。

INumberBase<IntPtr>.MinMagnitudeNumber(IntPtr, IntPtr)

比較兩個值與計算,其大小較小,如果輸入為 NaN ,則會傳回另一個值。

INumberBase<IntPtr>.One

取得型別的值 1

INumberBase<IntPtr>.Radix

取得類型的基數或基底。

INumberBase<IntPtr>.TryConvertFromChecked<TOther>(TOther, IntPtr)

表示帶正負號的整數,其中位寬度與指標相同。

INumberBase<IntPtr>.TryConvertFromSaturating<TOther>(TOther, IntPtr)

表示帶正負號的整數,其中位寬度與指標相同。

INumberBase<IntPtr>.TryConvertFromTruncating<TOther>(TOther, IntPtr)

表示帶正負號的整數,其中位寬度與指標相同。

INumberBase<IntPtr>.TryConvertToChecked<TOther>(IntPtr, TOther)

嘗試將目前類型的實例轉換成另一個類型,針對落在目前類型可表示範圍以外的任何值擲回溢位例外狀況。

INumberBase<IntPtr>.TryConvertToSaturating<TOther>(IntPtr, TOther)

嘗試將目前類型的實例轉換成另一種類型,使落在目前類型可表示範圍以外的任何值飽和。

INumberBase<IntPtr>.TryConvertToTruncating<TOther>(IntPtr, TOther)

嘗試將目前類型的實例轉換成另一個類型,截斷落在目前類型可表示範圍以外的任何值。

INumberBase<IntPtr>.Zero

取得型別的值 0

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用序列化目前 SerializationInfo 物件所需的資料,填入 IntPtr 物件。

IShiftOperators<IntPtr,Int32,IntPtr>.LeftShift(IntPtr, Int32)

將值左移指定的數量。

IShiftOperators<IntPtr,Int32,IntPtr>.RightShift(IntPtr, Int32)

以指定的數量向右移值。

IShiftOperators<IntPtr,Int32,IntPtr>.UnsignedRightShift(IntPtr, Int32)

以指定的數量向右移值。

ISignedNumber<IntPtr>.NegativeOne

取得型別的值 -1

ISubtractionOperators<IntPtr,IntPtr,IntPtr>.CheckedSubtraction(IntPtr, IntPtr)

減去兩個值來計算其差異。

ISubtractionOperators<IntPtr,IntPtr,IntPtr>.Subtraction(IntPtr, IntPtr)

減去兩個值來計算其差異。

IUnaryNegationOperators<IntPtr,IntPtr>.CheckedUnaryNegation(IntPtr)

計算值的已檢查一元否定。

IUnaryNegationOperators<IntPtr,IntPtr>.UnaryNegation(IntPtr)

計算值的一元負數。

IUnaryPlusOperators<IntPtr,IntPtr>.UnaryPlus(IntPtr)

計算值的一元加號。

適用於

執行緒安全性

此型別具備執行緒安全。

另請參閱