IntPtr.ToPointer 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重要
此 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调用该方法,将 Unicode 字符串作为 ANSI (单字节) 字符复制到非托管内存。 该方法返回一个 IntPtr 对象,该对象指向非托管字符串的开头。 
- Marshal.AllocHGlobal调用该方法以分配与非托管字符串占用的字节数相同的字节数。 该方法返回一个 IntPtr 对象,该对象指向非托管内存块的开头。 
- ToPointer调用该方法以获取指向字符串起始地址和非托管内存块的非托管指针,并将一个小于字符串的长度添加到 ANSI 字符串的起始地址。 由于非托管字符串指针现在指向字符串的末尾,因此复制操作会将字符串末尾的字符复制到内存块的开头。 
- 使用循环将字符串中的每个字符复制到非托管内存块。 每次复制操作后,它会递减指向非托管 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