次の方法で共有


IntPtr 構造体

定義

ビット幅がポインターと同じ符号付き整数を表します。

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
継承
IntPtr
属性
実装
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 オブジェクトを初期化し、その長さを取得すると、次の処理が行われます。

  1. Marshal.StringToHGlobalAnsi メソッドを呼び出して、Unicode 文字列を ANSI (1 バイト) 文字としてアンマネージ メモリにコピーします。 このメソッドは、アンマネージ文字列の先頭を指す IntPtr オブジェクトを返します。 Visual Basic の例では、このポインターを直接使用します。C++、F# および C# の例では、バイトへのポインターにキャストされます。

  2. Marshal.AllocHGlobal メソッドを呼び出して、アンマネージ文字列が占有するのと同じバイト数を割り当てます。 このメソッドは、アンマネージ メモリ ブロックの先頭を指す IntPtr オブジェクトを返します。 Visual Basic の例では、このポインターを直接使用します。C++、F# および C# の例では、バイトへのポインターにキャストされます。

  3. Visual Basic の例では、ANSI 文字列の長さと同じ offset という名前の変数を定義します。 これは、ANSI 文字列内の次の文字がコピーされるアンマネージド メモリへのオフセットを決定するために使用されます。 開始値は文字列の長さであるため、コピー操作では文字列の先頭からメモリ ブロックの末尾に文字がコピーされます。

    C#、F#、C++ の例では、ToPointer メソッドを呼び出して、文字列の開始アドレスとアンマネージ メモリ ブロックへのアンマネージ ポインターを取得し、文字列の長さより 1 つ小さい値を ANSI 文字列の開始アドレスに追加します。 アンマネージ文字列ポインターが文字列の末尾を指すようになったため、コピー操作では文字列の末尾からメモリ ブロックの先頭に文字がコピーされます。

  4. ループを使用して、文字列からアンマネージ メモリ ブロックに各文字をコピーします。

    Visual Basic の例では、Marshal.ReadByte(IntPtr, Int32) メソッドを呼び出して、マネージド ポインターから ANSI 文字列への指定されたオフセットにあるバイト (または 1 バイト文字) を読み取ります。 オフセットは、ループの反復ごとにインクリメントされます。 次に、Marshal.WriteByte(IntPtr, Int32, Byte) メソッドを呼び出して、アンマネージ メモリ ブロックの開始アドレスと offsetで定義されたメモリ アドレスにバイトを書き込みます。 次に、offsetデクリメントします。

    C#、F#、および C++ の例では、コピー操作を実行し、アンマネージド ANSI 文字列内の次の場所のアドレスへのポインターをデクリメントし、アンマネージ ブロック内の次のアドレスへのポインターをインクリメントします。

  5. すべての例では、Marshal.PtrToStringAnsi を呼び出して、コピーした ANSI 文字列を含むアンマネージ メモリ ブロックをマネージド Unicode String オブジェクトに変換します。

  6. 元の文字列と反転された文字列を表示した後、すべての例では、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 型を参照してください。

バージョン 11 以降の C# では、.NET 7 以降のランタイムを対象とする場合、nintInt32のエイリアスであるのと同じ方法で IntPtr のエイリアス int

コンストラクター

IntPtr(Int32)

指定した 32 ビット符号付き整数を使用して、IntPtr の新しいインスタンスを初期化します。

IntPtr(Int64)

指定した 64 ビット符号付き整数を使用して、IntPtr の新しいインスタンスを初期化します。

IntPtr(Void*)

指定されていない型への指定したポインターを使用して、IntPtr の新しいインスタンスを初期化します。

フィールド

Zero

0 に初期化された符号付き整数を表す読み取り専用フィールド。

プロパティ

MaxValue

IntPtrの最大値を取得します。

MinValue

IntPtrの最小値を取得します。

Size

このインスタンスのサイズを取得します。

メソッド

Abs(IntPtr)

値の絶対を計算します。

Add(IntPtr, Int32)

符号付き整数にオフセットを追加します。

Clamp(IntPtr, IntPtr, IntPtr)

値を包括的な最小値と最大値にクランプします。

CompareTo(IntPtr)

現在のインスタンスを同じ型の別のオブジェクトと比較し、現在のインスタンスが他のオブジェクトと並べ替え順序で同じ位置にあるかどうかを示す整数を返します。

CompareTo(Object)

現在のインスタンスを同じ型の別のオブジェクトと比較し、現在のインスタンスが他のオブジェクトと並べ替え順序で同じ位置にあるかどうかを示す整数を返します。

CopySign(IntPtr, IntPtr)

値の符号を別の値の符号にコピーします。

CreateChecked<TOther>(TOther)

現在の型のインスタンスを値から作成し、現在の型の表現可能範囲外の値に対してオーバーフロー例外をスローします。

CreateSaturating<TOther>(TOther)

現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外の値を飽和させます。

CreateTruncating<TOther>(TOther)

現在の型のインスタンスを値から作成し、現在の型の表現可能範囲外の値を切り捨てます。

DivRem(IntPtr, IntPtr)

2 つの値の商と剰余を計算します。

Equals(IntPtr)

現在のオブジェクトが同じ型の別のオブジェクトと等しいかどうかを示します。

Equals(Object)

このインスタンスが指定したオブジェクトと等しいかどうかを示す値を返します。

GetHashCode()

このインスタンスのハッシュ コードを返します。

IsEvenInteger(IntPtr)

値が偶数の整数を表すかどうかを判断します。

IsNegative(IntPtr)

値が負の値かどうかを判断します。

IsOddInteger(IntPtr)

値が奇数の整数を表すかどうかを判断します。

IsPositive(IntPtr)

値が正かどうかを判断します。

IsPow2(IntPtr)

値が 2 の累乗であるかどうかを判断します。

LeadingZeroCount(IntPtr)

値の先頭のゼロの数を計算します。

Log2(IntPtr)

値の log2 を計算します。

Max(IntPtr, IntPtr)

2 つの値を、より大きいコンピューティングと比較します。

MaxMagnitude(IntPtr, IntPtr)

2 つの値を、より大きいコンピューティングと比較します。

Min(IntPtr, IntPtr)

2 つの値を比較して計算します。計算の方が少なくなります。

MinMagnitude(IntPtr, IntPtr)

2 つの値を比較して計算します。計算の方が少なくなります。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

UTF-8 文字のスパンを値に解析します。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

UTF-8 文字のスパンを値に解析します。

Parse(ReadOnlySpan<Char>, IFormatProvider)

文字のスパンを値に解析します。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャ固有の形式の数値の文字表現の読み取り専用スパンを、等価の符号付きネイティブ整数に変換します。

Parse(String)

数値の文字列形式を等価の符号付きネイティブ整数に変換します。

Parse(String, IFormatProvider)

指定したカルチャ固有の形式の数値の文字列形式を、等価の符号付きネイティブ整数に変換します。

Parse(String, NumberStyles)

指定したスタイルの数値の文字列形式を、等価の符号付きネイティブ整数に変換します。

Parse(String, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の符号付きネイティブ整数に変換します。

PopCount(IntPtr)

値に設定されているビット数を計算します。

RotateLeft(IntPtr, Int32)

指定した量の値を左に回転します。

RotateRight(IntPtr, Int32)

指定した量の値を右に回転します。

Sign(IntPtr)

値の符号を計算します。

Subtract(IntPtr, Int32)

符号付き整数からオフセットを減算します。

ToInt32()

このインスタンスの値を 32 ビット符号付き整数に変換します。

ToInt64()

このインスタンスの値を 64 ビット符号付き整数に変換します。

ToPointer()

このインスタンスの値を、指定されていない型へのポインターに変換します。

ToString()

現在の IntPtr オブジェクトの数値を等価の文字列形式に変換します。

ToString(IFormatProvider)

指定した書式とカルチャ固有の書式情報を使用して、このインスタンスの数値を等価の文字列形式に変換します。

ToString(String)

現在の IntPtr オブジェクトの数値を等価の文字列形式に変換します。

ToString(String, IFormatProvider)

指定した形式を使用して、現在のインスタンスの値を書式設定します。

TrailingZeroCount(IntPtr)

値の末尾のゼロの数を計算します。

TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider)

現在のインスタンスの値を UTF-8 として指定されたバイトスパンに書式設定しようとします。

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)

現在のインスタンスの値を指定された文字スパンに書式設定しようとします。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, IntPtr)

UTF-8 文字のスパンを値に解析しようとします。

TryParse(ReadOnlySpan<Byte>, IntPtr)

数値の文字列形式を含む UTF-8 文字スパンを、等価の符号付き整数に変換しようとします。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, IntPtr)

UTF-8 文字のスパンを値に解析しようとします。

TryParse(ReadOnlySpan<Char>, IFormatProvider, IntPtr)

文字列を値に解析しようとします。

TryParse(ReadOnlySpan<Char>, IntPtr)

数値の文字表現の読み取り専用スパンを、等価の符号付きネイティブ整数に変換します。 戻り値は、変換が成功したかどうかを示します。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, IntPtr)

指定したスタイルおよびカルチャ固有の形式の数値の文字表現の読み取り専用スパンを、等価の符号付きネイティブ整数に変換します。 戻り値は、変換が成功したかどうかを示します。

TryParse(String, IFormatProvider, IntPtr)

文字列を値に解析しようとします。

TryParse(String, IntPtr)

数値の文字列形式を等価の符号付きネイティブ整数に変換します。 戻り値は、変換が成功したかどうかを示します。

TryParse(String, NumberStyles, IFormatProvider, IntPtr)

指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の符号付きネイティブ整数に変換します。 戻り値は、変換が成功したかどうかを示します。

演算子

Addition(IntPtr, Int32)

符号付き整数にオフセットを追加します。

Equality(IntPtr, IntPtr)

IntPtr の指定された 2 つのインスタンスが等しいかどうかを判断します。

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 の指定された 2 つのインスタンスが等しくないかどうかを判断します。

Subtraction(IntPtr, Int32)

符号付き整数からオフセットを減算します。

明示的なインターフェイスの実装

IAdditionOperators<IntPtr,IntPtr,IntPtr>.Addition(IntPtr, IntPtr)

合計を計算するために 2 つの値を加算します。

IAdditionOperators<IntPtr,IntPtr,IntPtr>.CheckedAddition(IntPtr, IntPtr)

合計を計算するために 2 つの値を加算します。

IAdditiveIdentity<IntPtr,IntPtr>.AdditiveIdentity

現在の型の加法 ID を取得します。

IBinaryInteger<IntPtr>.GetByteCount()

TryWriteLittleEndian(Span<Byte>, Int32)の一部として書き込まれるバイト数を取得します。

IBinaryInteger<IntPtr>.GetShortestBitLength()

現在の値の最短 2 の補数表現の長さをビット単位で取得します。

IBinaryInteger<IntPtr>.TryReadBigEndian(ReadOnlySpan<Byte>, Boolean, IntPtr)

ビット幅がポインターと同じ符号付き整数を表します。

IBinaryInteger<IntPtr>.TryReadLittleEndian(ReadOnlySpan<Byte>, Boolean, IntPtr)

ビット幅がポインターと同じ符号付き整数を表します。

IBinaryInteger<IntPtr>.TryWriteBigEndian(Span<Byte>, Int32)

ビッグ エンディアン形式の現在の値を特定のスパンに書き込もうとします。

IBinaryInteger<IntPtr>.TryWriteLittleEndian(Span<Byte>, Int32)

現在の値をリトル エンディアン形式で特定のスパンに書き込もうとします。

IBinaryNumber<IntPtr>.AllBitsSet

すべてのビットが設定されているバイナリ型のインスタンスを取得します。

IBitwiseOperators<IntPtr,IntPtr,IntPtr>.BitwiseAnd(IntPtr, IntPtr)

2 つの値のビットごとの計算を行います。

IBitwiseOperators<IntPtr,IntPtr,IntPtr>.BitwiseOr(IntPtr, IntPtr)

ビットごとの値または 2 つの値を計算します。

IBitwiseOperators<IntPtr,IntPtr,IntPtr>.ExclusiveOr(IntPtr, IntPtr)

2 つの値の排他的または排他的な値を計算します。

IBitwiseOperators<IntPtr,IntPtr,IntPtr>.OnesComplement(IntPtr)

指定された値の 1 対補数表現を計算します。

IComparisonOperators<IntPtr,IntPtr,Boolean>.GreaterThan(IntPtr, IntPtr)

2 つの値を比較して、どちらが大きいかを判断します。

IComparisonOperators<IntPtr,IntPtr,Boolean>.GreaterThanOrEqual(IntPtr, IntPtr)

2 つの値を比較して、どちらが大きいか等しいかを判断します。

IComparisonOperators<IntPtr,IntPtr,Boolean>.LessThan(IntPtr, IntPtr)

2 つの値を比較して、どちらが小さいかを判断します。

IComparisonOperators<IntPtr,IntPtr,Boolean>.LessThanOrEqual(IntPtr, IntPtr)

2 つの値を比較して、次の値以下を決定します。

IDecrementOperators<IntPtr>.CheckedDecrement(IntPtr)

値をデクリメントします。

IDecrementOperators<IntPtr>.Decrement(IntPtr)

値をデクリメントします。

IDivisionOperators<IntPtr,IntPtr,IntPtr>.Division(IntPtr, IntPtr)

1 つの値を別の値で除算して商を計算します。

IEquatable<IntPtr>.Equals(IntPtr)

このインスタンスが別の符号付き整数と等しいかどうかを示す値を返します。

IIncrementOperators<IntPtr>.CheckedIncrement(IntPtr)

値をインクリメントします。

IIncrementOperators<IntPtr>.Increment(IntPtr)

値をインクリメントします。

IMinMaxValue<IntPtr>.MaxValue

現在の型の最大値を取得します。

IMinMaxValue<IntPtr>.MinValue

現在の型の最小値を取得します。

IModulusOperators<IntPtr,IntPtr,IntPtr>.Modulus(IntPtr, IntPtr)

2 つの値を除算して、剰余または剰余を計算します。

IMultiplicativeIdentity<IntPtr,IntPtr>.MultiplicativeIdentity

現在の型の乗法 ID を取得します。

IMultiplyOperators<IntPtr,IntPtr,IntPtr>.CheckedMultiply(IntPtr, IntPtr)

2 つの値を乗算して積を計算します。

IMultiplyOperators<IntPtr,IntPtr,IntPtr>.Multiply(IntPtr, IntPtr)

2 つの値を乗算して積を計算します。

INumber<IntPtr>.MaxNumber(IntPtr, IntPtr)

2 つの値を比較して、より大きい値を計算し、入力が NaN場合はもう一方の値を返します。

INumber<IntPtr>.MinNumber(IntPtr, IntPtr)

2 つの値を比較して計算します。小さい方が計算され、入力が NaN場合はもう一方の値が返されます。

INumberBase<IntPtr>.IsCanonical(IntPtr)

値が正規表現内にあるかどうかを判断します。

INumberBase<IntPtr>.IsComplexNumber(IntPtr)

値が複素数を表すかどうかを判断します。

INumberBase<IntPtr>.IsFinite(IntPtr)

値が有限かどうかを判断します。

INumberBase<IntPtr>.IsImaginaryNumber(IntPtr)

値が純粋な虚数を表すかどうかを判断します。

INumberBase<IntPtr>.IsInfinity(IntPtr)

値が無限かどうかを判断します。

INumberBase<IntPtr>.IsInteger(IntPtr)

値が整数を表すかどうかを判断します。

INumberBase<IntPtr>.IsNaN(IntPtr)

値が NaN かどうかを判断します。

INumberBase<IntPtr>.IsNegativeInfinity(IntPtr)

値が負の無限大かどうかを判断します。

INumberBase<IntPtr>.IsNormal(IntPtr)

値が正常かどうかを判断します。

INumberBase<IntPtr>.IsPositiveInfinity(IntPtr)

値が正の無限大かどうかを判断します。

INumberBase<IntPtr>.IsRealNumber(IntPtr)

値が実数を表すかどうかを判断します。

INumberBase<IntPtr>.IsSubnormal(IntPtr)

値が非正規かどうかを判断します。

INumberBase<IntPtr>.IsZero(IntPtr)

値が 0 かどうかを判断します。

INumberBase<IntPtr>.MaxMagnitudeNumber(IntPtr, IntPtr)

2 つの値を比較して、大きさが大きい計算を行い、入力が NaN場合はもう一方の値を返します。

INumberBase<IntPtr>.MinMagnitudeNumber(IntPtr, IntPtr)

2 つの値を比較して、大きさが小さい計算を行い、入力が NaN場合はもう一方の値を返します。

INumberBase<IntPtr>.MultiplyAddEstimate(IntPtr, IntPtr, IntPtr)

(left * right) + addendの推定値を計算します。

INumberBase<IntPtr>.One

型の 1 値を取得します。

INumberBase<IntPtr>.Radix

型の基数 (底) を取得します。

INumberBase<IntPtr>.TryConvertFromChecked<TOther>(TOther, IntPtr)

ビット幅がポインターと同じ符号付き整数を表します。

INumberBase<IntPtr>.TryConvertFromSaturating<TOther>(TOther, IntPtr)

ビット幅がポインターと同じ符号付き整数を表します。

INumberBase<IntPtr>.TryConvertFromTruncating<TOther>(TOther, IntPtr)

ビット幅がポインターと同じ符号付き整数を表します。

INumberBase<IntPtr>.TryConvertToChecked<TOther>(IntPtr, TOther)

現在の型のインスタンスを別の型に変換しようと試み、現在の型の表現可能な範囲外の値に対してオーバーフロー例外をスローします。

INumberBase<IntPtr>.TryConvertToSaturating<TOther>(IntPtr, TOther)

現在の型のインスタンスを別の型に変換しようとします。現在の型の表現可能な範囲外の値が飽和状態になります。

INumberBase<IntPtr>.TryConvertToTruncating<TOther>(IntPtr, TOther)

現在の型のインスタンスを別の型に変換し、現在の型の表現可能な範囲外の値を切り捨てようとします。

INumberBase<IntPtr>.Zero

型の 0 値を取得します。

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

現在の IntPtr オブジェクトをシリアル化するために必要なデータを SerializationInfo オブジェクトに設定します。

IShiftOperators<IntPtr,Int32,IntPtr>.LeftShift(IntPtr, Int32)

指定した量の値を左にシフトします。

IShiftOperators<IntPtr,Int32,IntPtr>.RightShift(IntPtr, Int32)

指定した量で値を右にシフトします。

IShiftOperators<IntPtr,Int32,IntPtr>.UnsignedRightShift(IntPtr, Int32)

指定した量で値を右にシフトします。

ISignedNumber<IntPtr>.NegativeOne

型の -1 値を取得します。

ISubtractionOperators<IntPtr,IntPtr,IntPtr>.CheckedSubtraction(IntPtr, IntPtr)

2 つの値を減算して差を計算します。

ISubtractionOperators<IntPtr,IntPtr,IntPtr>.Subtraction(IntPtr, IntPtr)

2 つの値を減算して差を計算します。

IUnaryNegationOperators<IntPtr,IntPtr>.CheckedUnaryNegation(IntPtr)

値のチェックされた単項否定を計算します。

IUnaryNegationOperators<IntPtr,IntPtr>.UnaryNegation(IntPtr)

値の単項否定を計算します。

IUnaryPlusOperators<IntPtr,IntPtr>.UnaryPlus(IntPtr)

値の単項プラスを計算します。

適用対象

スレッド セーフ

この型はスレッド セーフです。

こちらもご覧ください