IntPtr.ToPointer 方法

定義

重要

此 API 不符合 CLS 規範。

將這個執行個體的值轉換為未指定類型的指標。

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

傳回

Void*

Void 的指標,也就是包含未指定類型資料的記憶體指標。

屬性

範例

下列範例會使用 Managed 指標來反轉陣列中的字元。 初始化 String 物件並取得其長度之後,它會執行下列動作:

  • Marshal.StringToHGlobalAnsi呼叫 方法,將 Unicode 字串複製到 Unmanaged 記憶體,作為 ANSI (一位元組) 字元。 方法會傳回指向 IntPtr Unmanaged 字串開頭的物件。

  • Marshal.AllocHGlobal呼叫 方法,以配置與 Unmanaged 字串佔用相同的位元組數目。 方法會傳 IntPtr 回指向 Unmanaged 記憶體區塊開頭的物件。

  • ToPointer呼叫 方法以取得字串的起始位址和非受控記憶體區塊的 Unmanaged 指標,並將一個小於字串長度的 ANSI 字串起始位址。 因為 Unmanaged 字串指標現在指向字串的結尾,所以複製作業會將字串結尾的字元複製到記憶體區塊的開頭。

  • 使用 迴圈,將字串中的每個字元複製到非受控記憶體區塊。 在每個複製作業之後,它會遞減 Unmanaged ANSI 字串中下一個位置位址的指標,並將指標遞增至 Unmanaged 區塊中的下一個位址。

  • 呼叫 , Marshal.PtrToStringAnsi 將包含所複製 ANSI 字串的 Unmanaged 記憶體區塊轉換成 Managed Unicode String 物件。

  • 顯示原始和反向字串之後,呼叫 Marshal.FreeHGlobal 方法來釋放配置給 Unmanaged ANSI 字串和 Unmanaged 記憶體區塊的記憶體。

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

適用於