語言

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 字串以 ANSI(單位元組)字元複製到非管理記憶體的方法。 該方法回傳一個 IntPtr 指向未管理字串起始點的物件。

  • 呼叫 Marshal.AllocHGlobal 方法以分配與未管理字串所佔位元組相同的位元組數。 該方法回傳一個 IntPtr 指向未管理記憶體區塊起始點的物件。

  • 呼叫 ToPointer 方法取得指向字串起始位址及未管理記憶體區塊的非管理指標,並將 ANSI 字串起始位址加一個比字串長度少一個的指標。 由於 Unmanaged 字串指標現在指向字串的結尾,因此複製作業會將字元從字串結尾複製到記憶體區塊的開頭。

  • 使用 迴圈,將每個字元從字串複製到記憶體的 Unmanaged 區塊。 每次複製操作後,會將指標遞減至非管理 ANSI 字串中下一個位置的位址,並將指標遞增至非管理區塊的下一個位址。

  • 呼叫 將 Marshal.PtrToStringAnsi 包含複製 ANSI 字串的未管理記憶體區塊轉換為受管理的 Unicode 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

適用於