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 가져오면 다음을 수행합니다.

  • 유니코드 문자열을 Marshal.StringToHGlobalAnsi 관리되지 않는 메모리에 ANSI(1 바이트) 문자로 복사하는 메서드를 호출합니다. 메서드는 관리되지 않는 문자열의 시작을 가리키는 개체를 반환 IntPtr 합니다.

  • 메서드를 Marshal.AllocHGlobal 호출하여 관리되지 않는 문자열이 차지하는 것과 동일한 바이트 수를 할당합니다. 메서드는 관리되지 않는 메모리 블록의 시작을 가리키는 개체를 반환 IntPtr 합니다.

  • 메서드를 ToPointer 호출하여 문자열의 시작 주소와 관리되지 않는 메모리 블록에 대한 관리되지 않는 포인터를 가져와서 ANSI 문자열의 시작 주소에 문자열 길이보다 작은 포인터를 추가합니다. 이제 관리되지 않는 문자열 포인터가 문자열의 끝을 가리키므로 복사 작업은 문자열의 끝에서 메모리 블록의 시작까지 문자를 복사합니다.

  • 루프를 사용하여 문자열의 각 문자를 관리되지 않는 메모리 블록으로 복사합니다. 각 복사 작업 후에는 관리되지 않는 ANSI 문자열의 다음 위치 주소에 대한 포인터를 감소시키고 관리되지 않는 블록의 다음 주소로 포인터를 증분합니다.

  • 복사된 Marshal.PtrToStringAnsi ANSI 문자열을 포함하는 관리되지 않는 메모리 블록을 관리형 유니코드 String 개체로 변환하려면 호출합니다.

  • 원래 및 역방향 문자열을 표시한 후 메서드를 호출 Marshal.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

적용 대상