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
繼承
屬性
實作

範例

下列範例會使用受管理的指標,若要反轉陣列中的字元。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.StringToHGlobalAnsiUnicode 字串複製到 unmanaged 記憶體為 ANSI (一個位元組) 字元的方法。Calls the Marshal.StringToHGlobalAnsi method to copy the Unicode string to unmanaged memory as an ANSI (one-byte) character. 此方法會傳回IntPtr指向的 unmanaged 字串的開頭的物件。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配置 unmanaged 的字串佔用相同的位元組數的方法。Calls the Marshal.AllocHGlobal method to allocate the same number of bytes as the unmanaged string occupies. 此方法會傳回IntPtr指向的 unmanaged 記憶體區塊開頭的物件。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. 它用來判斷其位移到 unmanaged 記憶體中複製到其中的 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方法來取得的 unmanaged 的指標的起始位址的字串和 unmanaged 的記憶體區塊的它們可以加入一個的 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. 因為 unmanaged 的字串指標現在會指向字串結尾,複製作業會將複製的字元從字串結尾的記憶體區塊的開始。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. 您可以使用迴圈,每個字元複製到 unmanaged 記憶體區塊的字串。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)unmanaged 加上的記憶體區塊的開始位址所定義的方法,將位元組寫入的記憶體位址offsetIt 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. 然後遞減offsetIt then decrements offset.

    C# 和 c + + 範例執行複製作業,則遞減 unmanaged 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要轉換的 unmanaged 的記憶體區塊,包含複製的 ANSI 字串,為受管理的 unicodeString物件。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方法來釋放 unmanaged 的 ANSI 字串和 unmanaged 的記憶體區塊配置的記憶體。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
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.

IntPtr型別是符合 CLS 規範,而UIntPtr類型不是。The IntPtr type is CLS-compliant, while the UIntPtr type is not. 只有IntPtr類型會在通用語言執行平台。Only the IntPtr type is used in the common language runtime. UIntPtr大部分都是為了維護架構對稱提供的型別IntPtr型別。The 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.

另請參閱