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 对象还可用于保存句柄。 例如,System.IO.FileStream 类中广泛使用 IntPtr 实例来保存文件句柄。

注意

使用 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# 中,nintIntPtr 的别名,intInt32的别名。

构造函数

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)

尝试将当前值(以小端格式)写入给定范围。

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>.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)

使用序列化当前 IntPtr 对象所需的数据填充 SerializationInfo 对象。

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)

计算一元加值。

适用于

线程安全性

此类型是线程安全的。

另请参阅