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>

示例

以下示例使用托管指针来反转数组中的字符。 初始化 对象 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 字符串中的下一个字符复制到的非托管内存中的偏移量。 由于其起始值为字符串的长度,因此复制操作会将字符从字符串的开头复制到内存块的末尾。

    C#、F# 和 C++ 示例调用 ToPointer 方法以获取指向字符串的起始地址和非托管内存块的非托管指针,并将小于字符串长度的指针添加到 ANSI 字符串的起始地址。 由于非托管字符串指针现在指向字符串的末尾,因此复制操作会将字符串末尾的字符复制到内存块的开头。

  4. 使用循环将字符串中的每个字符复制到非托管内存块。

    Visual Basic 示例调用 Marshal.ReadByte(IntPtr, Int32) 方法,以从指向 ANSI 字符串的托管指针的指定偏移量读取字节 (或单字节字符) 。 偏移量随循环的每次迭代而递增。 然后, Marshal.WriteByte(IntPtr, Int32, Byte) 它调用 方法,将字节写入由非托管内存块的起始地址和 offset定义的内存地址。 然后,它会递 offset减 。

    C#、F# 和 C++ 示例执行复制操作,然后递减指向非托管 ANSI 字符串中下一个位置的地址的指针,并将指针递增到非托管块中的下一个地址。

  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 对象还可用于保存句柄。 例如, 的 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 类型

在 C# 中,从版本 11 开始,以 .NET 7 或更高版本运行时为目标时, nint 是 的 IntPtr 别名,其别名与 int 的别名 Int32相同。

构造函数

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)

将指定区域性特定格式的数字的字符串表示形式转换为它的等效有符号本机整数。

Parse(String, NumberStyles)

将指定样式的数字的字符串表示形式转换为它的等效有符号本机整数。

Parse(String, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效有符号本机整数。

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)

将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效有符号本机整数。 一个指示转换是否成功的返回值。

运算符

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)

从有符号整数中减去偏移量。

显式接口实现

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)

尝试以 little-endian 格式将当前值写入给定范围。

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)

计算值的一元加号。

适用于

线程安全性

此类型是线程安全的。

另请参阅