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 : 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
- 继承
- 属性
- 实现
-
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 对象并获取其长度后,它将执行以下操作:
调用 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 对象还可用于保存句柄。 例如,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# 中,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) |
从带符号整数中减去偏移量。 |
显式接口实现
适用于
线程安全性
此类型是线程安全的。