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 保持するために広範囲に使用されます。

Note

ポインターまたはハンドルとして を使用すると IntPtr 、エラーが発生しやすく、安全ではありません。 これは単なる整数型であり、同じサイズであるため、ポインターとハンドルのインターチェンジ形式として使用できます。 ポインターをサポートしていない言語にデータを渡すなど、特定のインターチェンジ要件以外では、ポインターを表すために正しく型指定されたポインターを使用し、 SafeHandle ハンドルを表すために使用する必要があります。

この型は、 を実装します ISerializable。 .NET 5 以降のバージョンでは、この型は インターフェイスも実装します IFormattable 。 .NET 7 以降のバージョンでは、この型は、IMinMaxValue<TSelf>および ISignedNumber<TSelf> インターフェイスも実装IBinaryInteger<TSelf>します。

バージョン 9.0 以降の C# では、組 nint み込み型を使用してネイティブ サイズの整数を定義できます。 この型は、 型によって IntPtr 内部的に表され、整数型に適した演算と変換を提供します。 詳細については、「 nint 型と nuint 型」を参照してください。

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

コンストラクター

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)

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

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)

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

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>.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)

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

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)

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

適用対象

スレッド セーフ

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

こちらもご覧ください