IntPtr 結構

定義

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

public value class IntPtr
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 : 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 : IEquatable<IntPtr>, System::Runtime::Serialization::ISerializable
public value class IntPtr : System::Runtime::Serialization::ISerializable
public struct IntPtr
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 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 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 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 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 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 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 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 IEquatable(Of IntPtr), ISerializable
Public Structure IntPtr
Implements ISerializable
繼承
IntPtr
屬性
實作
IComparable IComparable<IntPtr> IComparable<TSelf> IEquatable<IntPtr> IEquatable<TSelf> IFormattable IParsable<IntPtr> IParsable<TSelf> ISpanFormattable ISpanParsable<IntPtr> ISpanParsable<TSelf> IUtf8SpanFormattable IUtf8SpanParsable<IntPtr> IUtf8SpanParsable<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> ISerializable

範例

下列範例會使用Managed指標來反轉數位列中的字元。 初始化物件 String 並取得長度後,會執行以下操作:

  1. 呼叫 Marshal.StringToHGlobalAnsi 將 Unicode 字串以 ANSI(單位元組)字元複製到非管理記憶體的方法。 該方法回傳一個 IntPtr 指向未管理字串起始點的物件。 Visual Basic 範例會直接使用此指標;在 C++、F# 和 C# 範例中,它會轉換成位元組的指標。

  2. 呼叫 Marshal.AllocHGlobal 方法以分配與未管理字串所佔位元組相同的位元組數。 該方法回傳一個 IntPtr 指向未管理記憶體區塊起始點的物件。 Visual Basic 範例會直接使用此指標;在 C++、F# 和 C# 範例中,它會轉換成位元組的指標。

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

    C#、F# 和 C++ 範例 ToPointer 呼叫該方法取得字串起始位址與記憶體區塊的非管理指標,並對 ANSI 字串的起始位址加一個字串長度。 由於 Unmanaged 字串指標現在指向字串的結尾,因此複製作業會將字元從字串結尾複製到記憶體區塊的開頭。

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

    Visual Basic 範例呼叫 Marshal.ReadByte(IntPtr, Int32) 方法,從管理指標到 ANSI 字串的指定偏移量讀取位元組(或一位元組字元)。 位移會隨著迴圈的每個反覆專案而遞增。 接著它呼叫 Marshal.WriteByte(IntPtr, Int32, Byte) 該位元組寫入由未管理記憶體區塊 offset起始位址加 所定義的記憶體位址。 接著會 offset遞減。

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

  5. 所有範例都呼叫將 Marshal.PtrToStringAnsi 包含複製 ANSI 字串的未管理記憶體區塊轉換為受管理的 Unicode String 物件。

  6. 在顯示原始字串與反向字串後,所有範例都會呼叫該 FreeHGlobal 方法以釋放分配給非管理 ANSI 字串及非管理記憶體區塊的記憶體。

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 物品也可以用來握把手。 例如,在類別中大量使用 System.IO.FileStreamIntPtr實例來保存檔案的句柄。

注意

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

此類型實 ISerializable作 。 在 .NET 5 及以後版本中,此類型也實作了 IFormattable 介面。 在 .NET 7 及以後版本中,此類型也實作了 IBinaryInteger<TSelf>IMinMaxValue<TSelf> 以及 ISignedNumber<TSelf> 介面。

在 C# 從 9.0 版本開始,你可以使用內建 nint 型別來定義原生大小的整數。 此型別由 IntPtr 內部型別表示,並提供適合整數型別的操作與轉換。 更多資訊請參見 nint 和 nuint 類型

在 C# 從版本 11 開始,針對 .NET 7 或更新執行時,nintIntPtr 的別名,就像 intInt32 的別名一樣。

建構函式

名稱 Description
IntPtr(Int32)

初始化一個使用指定 32 位元有符號整數的新實例 IntPtr

IntPtr(Int64)

初始化使用指定 64 位元有號整數的新實例 IntPtr

IntPtr(Void*)

初始化使用指定指標指向未指定型態的新實例 IntPtr

欄位

名稱 Description
Zero

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

屬性

名稱 Description
MaxValue

得到最大可能的值。IntPtr

MinValue

得到最小的值。IntPtr

Size

取得這個實例的大小。

方法

名稱 Description
Abs(IntPtr)

計算值的絕對值。

Add(IntPtr, Int32)

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

BigMul(IntPtr, IntPtr, IntPtr)

產生兩個無符號原生整數的完整乘積。

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)

判斷值是否為兩個乘冪。

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, IFormatProvider)

將指定之特定文化特性格式之數位的字串表示轉換為其相等的帶正負號原生整數。

Parse(String, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式之數位的字串表示,轉換為其相等的帶正負號原生整數。

Parse(String, NumberStyles)

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

Parse(String)

將數位的字串表示轉換為其相等的帶正負號原生整數。

PopCount(IntPtr)

計算值中所設定的位數。

RotateLeft(IntPtr, Int32)

以指定的數量旋轉值。

RotateRight(IntPtr, Int32)

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

Sign(IntPtr)

計算值的符號。

Subtract(IntPtr, Int32)

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

ToInt32()

將這個實例的值轉換為32位帶正負號的整數。

ToInt64()

將這個實例的值轉換為64位帶正負號的整數。

ToPointer()

將這個實例的值轉換為未指定類型的指標。

ToString()

將當前 IntPtr 物件的數值轉換為其等效的字串表示。

ToString(IFormatProvider)

使用指定的格式和文化特性特定格式資訊,將這個實例的數值轉換為其相等的字串表示。

ToString(String, IFormatProvider)

使用指定的格式,格式化目前實例的值。

ToString(String)

將當前 IntPtr 物件的數值轉換為其等效的字串表示。

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)

將指定樣式和特定文化特性格式之數位的字串表示,轉換為其相等的帶正負號原生整數。 傳回值表示轉換是否成功。

操作員

名稱 Description
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 值轉換為未指定型別的指標。

此 API 不符合 CLS 標準。

Explicit(Void* to IntPtr)

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

此 API 不符合 CLS 標準。

Inequality(IntPtr, IntPtr)

判斷兩個指定的 實 IntPtr 例是否不相等。

Subtraction(IntPtr, Int32)

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

明確介面實作

名稱 Description
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)

嘗試以 big-endian 格式將目前的值寫入指定的範圍。

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)

計算指定值的 ones-complement 表示法。

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>.MultiplyAddEstimate(IntPtr, IntPtr, IntPtr)

計算估計值為(left * right) + 。 addend

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)

計算值的一元加號。

適用於

執行緒安全性

此類型是安全線程。

另請參閱