IntPtr 结构
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个带符号整数,其中位宽度与指针相同。
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 : 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 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 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 IEquatable(Of IntPtr), ISerializable
Public Structure IntPtr
Implements ISerializable
- 继承
- 属性
- 实现
-
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>
示例
以下示例使用托管指针来反转数组中的字符。 初始化 String 对象并获取其长度后,它将执行以下操作:
Marshal.StringToHGlobalAnsi调用 方法将 Unicode 字符串作为 ANSI (一字节) 字符复制到非托管内存。 方法返回一个 IntPtr 对象,该对象指向非托管字符串的开头。 Visual Basic 示例直接使用此指针;在 C++、F# 和 C# 示例中,它被强制转换为指向字节的指针。
Marshal.AllocHGlobal调用 方法以分配与非托管字符串占用的字节数相同的字节数。 方法返回一个 IntPtr 对象,该对象指向非托管内存块的开头。 Visual Basic 示例直接使用此指针;在 C++、F# 和 C# 示例中,它被强制转换为指向字节的指针。
Visual Basic 示例定义一个名为
offset
的变量,该变量等于 ANSI 字符串的长度。 它用于确定将 ANSI 字符串中的下一个字符复制到的非托管内存中的偏移量。 由于其起始值是字符串的长度,因此复制操作会将字符串开头的字符复制到内存块的末尾。C#、F# 和 C++ 示例调用 ToPointer 方法以获取指向字符串的起始地址和非托管内存块的非托管指针,并将一个小于字符串长度的指针添加到 ANSI 字符串的起始地址。 由于非托管字符串指针现在指向字符串的末尾,因此复制操作会将字符从字符串的末尾复制到内存块的开头。
使用循环将字符串中的每个字符复制到非托管内存块。
Visual Basic 示例调用 Marshal.ReadByte(IntPtr, Int32) 方法,以从指向 ANSI 字符串的托管指针的指定偏移量读取字节 (或单字节字符) 。 偏移量随循环的每次迭代而递增。 然后, Marshal.WriteByte(IntPtr, Int32, Byte) 它调用 方法,将字节写入由非托管内存块的起始地址和
offset
定义的内存地址。 然后,它会递offset
减 。C#、F# 和 C++ 示例执行复制操作,然后递减指向非托管 ANSI 字符串中下一个位置的地址的指针,并将指针递增到非托管块中的下一个地址。
所有示例都调用 Marshal.PtrToStringAnsi ,将包含复制的 ANSI 字符串的非托管内存块转换为托管的 Unicode String 对象。
在显示原始字符串和反向字符串后,所有示例都会调用 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 |
获取此实例的大小。 |
方法
运算符
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) |
从有符号整数中减去偏移量。 |
显式接口实现
IBinaryInteger<IntPtr>.GetByteCount() |
获取将作为 的一部分写入的 TryWriteLittleEndian(Span<Byte>, Int32)字节数。 |
IBinaryInteger<IntPtr>.GetShortestBitLength() |
获取当前值的最短两个补补表示形式的长度(以位为单位)。 |
IBinaryInteger<IntPtr>.TryWriteBigEndian(Span<Byte>, Int32) |
尝试以 big-endian 格式将当前值写入给定范围。 |
IBinaryInteger<IntPtr>.TryWriteLittleEndian(Span<Byte>, Int32) |
尝试以 little-endian 格式将当前值写入给定范围。 |
IEquatable<IntPtr>.Equals(IntPtr) |
返回一个值,该值指示此实例是否等于另一个有符号整数。 |
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
使用序列化当前的 SerializationInfo 对象所需的所有数据填充 IntPtr 对象。 |
适用于
线程安全性
此类型是线程安全的。