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>
範例
下列範例會使用Managed指標來反轉數位列中的字元。 初始化 String 物件並取得其長度之後,它會執行下列動作:
呼叫 Marshal.StringToHGlobalAnsi 方法,將 Unicode 字串複製到 Unmanaged 記憶體做為 ANSI (一位元組) 字元。 方法會傳回指向 Unmanaged 字串開頭的 IntPtr 物件。 Visual Basic 範例會直接使用此指標;在 C++、F# 和 C# 範例中,它會轉換成位元組的指標。
呼叫 Marshal.AllocHGlobal 方法,以配置與 Unmanaged 字串佔用的相同位元組數目。 方法會傳回指向 Unmanaged 記憶體區塊開頭的 IntPtr 物件。 Visual Basic 範例會直接使用此指標;在 C++、F# 和 C# 範例中,它會轉換成位元組的指標。
Visual Basic 範例會定義名為
offset
的變數,其長度等於 ANSI 字串的長度。 它用來判斷將 ANSI 字串中下一個字元複製到 Unmanaged 記憶體中的位移。 因為其起始值是字串的長度,因此複製作業會將字元從字串開頭複製到記憶體區塊的結尾。C#、F# 和 C++ 範例會呼叫 ToPointer 方法,以取得字串起始位址和記憶體中 Unmanaged 區塊的 Unmanaged 指標,並將一個小於字元串長度的字元串新增至 ANSI 字符串的起始位址。 由於 Unmanaged 字串指標現在指向字串的結尾,因此複製作業會將字元從字串結尾複製到記憶體區塊的開頭。
使用 迴圈,將每個字元從字串複製到記憶體的 Unmanaged 區塊。
Visual Basic 範例會呼叫 Marshal.ReadByte(IntPtr, Int32) 方法,以從 Managed 指標到 ANSI 字串的指定位移讀取位元組(或一位元組字元)。 位移會隨著迴圈的每個反覆專案而遞增。 然後,它會呼叫 Marshal.WriteByte(IntPtr, Int32, Byte) 方法,將位元組寫入非受控記憶體區塊起始位址所定義的記憶體位址,加上
offset
。 然後,它會遞減offset
。C#、F# 和 C++ 範例會執行複製作業,然後將 Unmanaged ANSI 字串中下一個位置的位址指標遞減,並將指標遞增至 Unmanaged 區塊中的下一個位址。
所有範例都會呼叫 Marshal.PtrToStringAnsi,將包含已複製 ANSI 字串的 Unmanaged 記憶體區塊轉換成 Managed Unicode String 物件。
顯示原始和反轉字串之後,所有範例都會呼叫 FreeHGlobal 方法來釋放配置給 Unmanaged ANSI 字串和 Unmanaged 記憶體區塊的記憶體。
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 類型表示,並提供適用於整數類型的作業和轉換。 如需詳細資訊,請參閱
從第 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) |
從帶正負號的整數減去位移。 |
明確介面實作
適用於
執行緒安全性
此類型是安全線程。