IntPtr IntPtr IntPtr IntPtr Struct

定義

平台專用的類型,用以代表指標或控點。A platform-specific type that is used to represent a pointer or a handle.

public value class IntPtr : System::Runtime::Serialization::ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct IntPtr : System.Runtime.Serialization.ISerializable
type nativeint = struct
    interface ISerializable
Public Structure IntPtr
Implements ISerializable
繼承
屬性
實作

範例

下列範例會使用 managed 指標來反轉陣列中的字元。The following example uses managed pointers to reverse the characters in an array. 在初始化String物件並取得其長度之後,它會執行下列動作:After it initializes a String object and gets its length, it does the following:

  1. Marshal.StringToHGlobalAnsi呼叫方法,將 Unicode 字串複製到非受控記憶體,做為 ANSI (單位元組)字元。Calls the Marshal.StringToHGlobalAnsi method to copy the Unicode string to unmanaged memory as an ANSI (one-byte) character. 方法會傳回指向IntPtr非受控字串開頭的物件。The method returns an IntPtr object that points to the beginning of the unmanaged string. Visual Basic 範例會直接使用此指標;在C++和C#範例中,它會轉換成位元組的指標。The Visual Basic example uses this pointer directly; in the C++ and C# examples, it is cast to a pointer to a byte.

  2. Marshal.AllocHGlobal呼叫方法,以配置與非受控字串佔用的位元組數目。Calls the Marshal.AllocHGlobal method to allocate the same number of bytes as the unmanaged string occupies. 方法會傳回指向IntPtr非受控記憶體區塊開頭的物件。The method returns an IntPtr object that points to the beginning of the unmanaged block of memory. Visual Basic 範例會直接使用此指標;在C++和C#範例中,它會轉換成位元組的指標。The Visual Basic example uses this pointer directly; in the C++ and C# examples, it is cast to a pointer to a byte.

  3. Visual Basic 範例會定義名offset為的變數,其等於 ANSI 字串的長度。The Visual Basic example defines a variable named offset that is equal to the length of the ANSI string. 它是用來判斷在非受控記憶體中的位移,這是 ANSI 字串中下一個章程的複製目的地。It is used to determine the offset into unmanaged memory to which the next charter in the ANSI string is copied. 由於其起始值為字串的長度,因此複製作業會將字元從字串開頭複製到記憶體區塊的結尾。Because its starting value is the length of the string, the copy operation will copy a character from the start of the string to the end of the memory block.

    C# C++ 和ToPointer範例會呼叫方法,以取得字串的起始位址和未受管理的記憶體區塊的非受控指標,並將小於字串長度的範圍新增至 ANSI 字串的起始位址。The C# and C++ examples call the ToPointer method to get an unmanaged pointer to the starting address of the string and the unmanaged block of memory, and they add one less than the length of the string to the starting address of the ANSI string. 由於非受控字串指標現在會指向字串結尾,因此複製作業會將字元從字串結尾複製到記憶體區塊的開頭。Because the unmanaged string pointer now points to the end of the string, the copy operation will copy a character from the end of the string to the start of the memory block.

  4. 使用迴圈,將字串中的每個字元複製到未受管理的記憶體區塊。Uses a loop to copy each character from the string to the unmanaged block of memory.

    Visual Basic 範例會呼叫Marshal.ReadByte(IntPtr, Int32)方法,以在 ANSI 字串的 managed 指標中,讀取指定位移處的位元組(或一個位元組字元)。The Visual Basic example calls the Marshal.ReadByte(IntPtr, Int32) method to read the byte (or one-byte character) at a specified offset from the managed pointer to the ANSI string. 位移會隨著迴圈的每個反復專案而遞增。The offset is incremented with each iteration of the loop. 然後,它會Marshal.WriteByte(IntPtr, Int32, Byte)呼叫方法,將位元組寫入至記憶體位址,而該位址是由未受管理的記憶體區塊offset之起始位址所定義。It then calls the Marshal.WriteByte(IntPtr, Int32, Byte) method to write the byte to the memory address defined by the starting address of the unmanaged block of memory plus offset. 然後,它offset會遞減。It then decrements offset.

    C#和C++範例會執行複製作業,然後遞減非受控 ANSI 字串中下一個位置之位址的指標,並將指標遞增至非受控區塊中的下一個位址。The C# and C++ examples perform the copy operation, then decrement the pointer to the address of the next location in the unmanaged ANSI string and increment the pointer to the next address in the unmanaged block.

  5. 所有範例都會呼叫Marshal.PtrToStringAnsi ,將包含複製之 ANSI 字串的非受控記憶體區塊轉換成 managed String Unicode 物件。All examples call the Marshal.PtrToStringAnsi to convert the unmanaged memory block containing the copied ANSI string to a managed Unicode String object.

  6. 在顯示原始和反轉字串之後,所有範例都會呼叫FreeHGlobal方法,以釋放配置給非受控 ANSI 字串和非受控記憶體區塊的記憶體。After displaying the original and reversed strings, all examples call the FreeHGlobal method to free the memory allocated for the unmanaged ANSI string and the unmanaged block of memory.

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
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的設計是,其大小為平臺特定的整數。The IntPtr type is designed to be an integer whose size is platform-specific. 也就是說,此類型的實例必須是 32-32 位硬體和作業系統上的位,以及64位硬體和作業系統上的64位。That is, an instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems.

IntPtr類型可用於支援指標的語言,以及在執行和不支援指標的語言之間參考資料的常見方法。The IntPtr type can be used by languages that support pointers, and as a common means of referring to data between languages that do and do not support pointers.

IntPtr物件也可以用來保存控制碼。IntPtr objects can also be used to hold handles. 例如,的IntPtr實例會廣泛用於System.IO.FileStream類別中,以保存檔案控制代碼。For example, instances of IntPtr are used extensively in the System.IO.FileStream class to hold file handles.

類型符合 CLS 規範, UIntPtr而類型不是。 IntPtrThe IntPtr type is CLS-compliant, while the UIntPtr type is not. 通用語言執行時間只會使用類型。IntPtrOnly the IntPtr type is used in the common language runtime. 類型主要是用來維護類型的IntPtr架構對稱。 UIntPtrThe UIntPtr type is provided mostly to maintain architectural symmetry with the IntPtr type.

此型別代表 ISerializable 介面。This type implements the ISerializable interface.

建構函式

IntPtr(Int32) IntPtr(Int32) IntPtr(Int32) IntPtr(Int32)

使用指定的 32 位元指標或控點,初始化 IntPtr 的新執行個體。Initializes a new instance of IntPtr using the specified 32-bit pointer or handle.

IntPtr(Int64) IntPtr(Int64) IntPtr(Int64) IntPtr(Int64)

使用指定的 64 位元指標,初始化 IntPtr 的新執行個體。Initializes a new instance of IntPtr using the specified 64-bit pointer.

IntPtr(Void*) IntPtr(Void*) IntPtr(Void*) IntPtr(Void*)

對未指定的類型使用指定的指標,初始化 IntPtr 的新執行個體。Initializes a new instance of IntPtr using the specified pointer to an unspecified type.

欄位

Zero Zero Zero Zero

表示已經初始化為零的指標或控點之唯讀欄位。A read-only field that represents a pointer or handle that has been initialized to zero.

屬性

Size Size Size Size

取得這個執行個體的大小。Gets the size of this instance.

方法

Add(IntPtr, Int32) Add(IntPtr, Int32) Add(IntPtr, Int32) Add(IntPtr, Int32)

將指標的值加上位移。Adds an offset to the value of a pointer.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,指出這個執行個體 (Instance) 是否和指定的物件相等。Returns a value indicating whether this instance is equal to a specified object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

Subtract(IntPtr, Int32) Subtract(IntPtr, Int32) Subtract(IntPtr, Int32) Subtract(IntPtr, Int32)

將指標值減去移位。Subtracts an offset from the value of a pointer.

ToInt32() ToInt32() ToInt32() ToInt32()

將這個執行個體的值轉換為 32 位元帶正負號的整數。Converts the value of this instance to a 32-bit signed integer.

ToInt64() ToInt64() ToInt64() ToInt64()

將這個執行個體的值轉換為 64 位元帶正負號的整數。Converts the value of this instance to a 64-bit signed integer.

ToPointer() ToPointer() ToPointer() ToPointer()

將這個執行個體的值轉換為未指定類型的指標。Converts the value of this instance to a pointer to an unspecified type.

ToString(String) ToString(String) ToString(String) ToString(String)

將目前 IntPtr 物件的數值,轉換為其相等的字串表示。Converts the numeric value of the current IntPtr object to its equivalent string representation.

ToString() ToString() ToString() ToString()

將目前 IntPtr 物件的數值,轉換為其相等的字串表示。Converts the numeric value of the current IntPtr object to its equivalent string representation.

運算子

Addition(IntPtr, Int32) Addition(IntPtr, Int32) Addition(IntPtr, Int32) Addition(IntPtr, Int32)

將指標的值加上位移。Adds an offset to the value of a pointer.

Equality(IntPtr, IntPtr) Equality(IntPtr, IntPtr) Equality(IntPtr, IntPtr) Equality(IntPtr, IntPtr)

判斷 IntPtr 的兩個指定執行個體是否相等。Determines whether two specified instances of IntPtr are equal.

Explicit(Void* to IntPtr) Explicit(Void* to IntPtr) Explicit(Void* to IntPtr) Explicit(Void* to IntPtr)

將指定的指標轉換為 IntPtr 的未指定類型。Converts the specified pointer to an unspecified type to an IntPtr.

此應用程式開發介面不符合 CLS 標準。This API is not CLS-compliant.

Explicit(IntPtr to Int64) Explicit(IntPtr to Int64) Explicit(IntPtr to Int64) Explicit(IntPtr to Int64)

將指定的 IntPtr 值轉換為 64 位元帶正負號的整數。Converts the value of the specified IntPtr to a 64-bit signed integer.

Explicit(IntPtr to Void*) Explicit(IntPtr to Void*) Explicit(IntPtr to Void*) Explicit(IntPtr to Void*)

將指定的 IntPtr 值轉換為未指定的類型指標。Converts the value of the specified IntPtr to a pointer to an unspecified type.

此應用程式開發介面不符合 CLS 標準。This API is not CLS-compliant.

Explicit(Int64 to IntPtr) Explicit(Int64 to IntPtr) Explicit(Int64 to IntPtr) Explicit(Int64 to IntPtr)

將 64 位元帶正負號整數的值轉換為 IntPtrConverts the value of a 64-bit signed integer to an IntPtr.

Explicit(Int32 to IntPtr) Explicit(Int32 to IntPtr) Explicit(Int32 to IntPtr) Explicit(Int32 to IntPtr)

將 32 位元帶正負號整數的值轉換為 IntPtrConverts the value of a 32-bit signed integer to an IntPtr.

Explicit(IntPtr to Int32) Explicit(IntPtr to Int32) Explicit(IntPtr to Int32) Explicit(IntPtr to Int32)

將指定的 IntPtr 值轉換為 32 位元帶正負號的整數。Converts the value of the specified IntPtr to a 32-bit signed integer.

Inequality(IntPtr, IntPtr) Inequality(IntPtr, IntPtr) Inequality(IntPtr, IntPtr) Inequality(IntPtr, IntPtr)

判斷 IntPtr 的兩個指定執行個體是否不相等。Determines whether two specified instances of IntPtr are not equal.

Subtraction(IntPtr, Int32) Subtraction(IntPtr, Int32) Subtraction(IntPtr, Int32) Subtraction(IntPtr, Int32)

將指標值減去移位。Subtracts an offset from the value of a pointer.

明確介面實作

IEquatable<IntPtr>.Equals(IntPtr) IEquatable<IntPtr>.Equals(IntPtr) IEquatable<IntPtr>.Equals(IntPtr) IEquatable<IntPtr>.Equals(IntPtr)
ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用序列化目前 SerializationInfo 物件所需的資料,填入 IntPtr 物件。Populates a SerializationInfo object with the data needed to serialize the current IntPtr object.

適用於

執行緒安全性

此型別具備執行緒安全。This type is thread safe.

另請參閱