IntPtr.ToPointer メソッド

定義

重要

この API は CLS 準拠ではありません。

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

public:
 void* ToPointer();
[System.CLSCompliant(false)]
public void* ToPointer ();
[<System.CLSCompliant(false)>]
member this.ToPointer : unit -> nativeptr<unit>

戻り値

Void*

Void のポインター。つまり指定されていない型のデータが格納されているメモリのポインター。

属性

次の例では、マネージド ポインターを使用して配列内の文字を反転します。 オブジェクトを String 初期化し、その長さを取得すると、次の処理が行われます。

  • Unicode 文字列を Marshal.StringToHGlobalAnsi ANSI (1 バイト) 文字としてアンマネージド メモリにコピーするメソッドを呼び出します。 このメソッドは、 IntPtr アンマネージ文字列の先頭を指すオブジェクトを返します。

  • アンマネージ文字列が Marshal.AllocHGlobal 占めるのと同じバイト数を割り当てるメソッドを呼び出します。 このメソッドは、 IntPtr アンマネージ メモリ ブロックの先頭を指すオブジェクトを返します。

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

  • ループを使用して、文字列からアンマネージ メモリ ブロックに各文字をコピーします。 各コピー操作の後、アンマネージ ANSI 文字列内の次の場所のアドレスへのポインターをデクリメントし、アンマネージ ブロック内の次のアドレスへのポインターをインクリメントします。

  • コピーした Marshal.PtrToStringAnsi ANSI 文字列を含むアンマネージ メモリ ブロックをマネージド Unicode String オブジェクトに変換する呼び出し。

  • 元の文字列と逆の文字列を表示した後、アンマネージ ANSI 文字列とアンマネージ メモリ ブロックに割り当てられたメモリを解放するメソッドを呼び出 Marshal.FreeHGlobal します。

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

適用対象