IntPtr.ToPointer Method

Definition

Important

This API is not CLS-compliant.

将此实例的值转换为指向未指定类型的指针。Converts the value of this instance to a pointer to an unspecified type.

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

Returns

Void*

指向 Void 的指针,即是说,该指针所指向的内存包含有未指定类型的数据。A pointer to Void; that is, a pointer to memory containing data of an unspecified type.

Attributes

Examples

下面的示例使用托管指针来反转数组中的字符。The following example uses managed pointers to reverse the characters in an array. 初始化 String 对象并获取其长度后,它将执行以下操作:After it initializes a String object and gets its length, it does the following:

  • 调用 Marshal.StringToHGlobalAnsi 方法将 Unicode 字符串作为 ANSI (单字节)字符复制到非托管内存。Calls the Marshal.StringToHGlobalAnsi method to copy the Unicode string to unmanaged memory as ANSI (one-byte) characters. 方法返回指向非托管字符串开头的 IntPtr 对象。The method returns an IntPtr object that points to the beginning of the unmanaged string.

  • 调用 Marshal.AllocHGlobal 方法,以便分配与非托管字符串占用的字节数相同的字节数。Calls the Marshal.AllocHGlobal method to allocate the same number of bytes as the unmanaged string occupies. 方法返回一个 IntPtr 对象,该对象指向非托管内存块的开头。The method returns an IntPtr object that points to the beginning of the unmanaged block of memory.

  • 调用 ToPointer 方法,以获取指向字符串的起始地址和非托管内存块的非托管指针,并将该字符串的长度小于该字符串的长度。Calls the ToPointer method to get an unmanaged pointer to the starting address of the string and the unmanaged block of memory, and adds one less than the length of the string to the starting address of the ANSI string. 由于非托管字符串指针现在指向字符串的末尾,因此复制操作会将字符串末尾的字符复制到内存块的开头。Because the unmanaged string pointer now points to the end of the string, the copy operation will copy a character from the end of the string to the start of the memory block.

  • 使用循环将字符串中的每个字符复制到非托管内存块。Uses a loop to copy each character from the string to the unmanaged block of memory. 在每个复制操作完成后,它会将指针递减到非托管 ANSI 字符串中的下一个位置的地址,并使指针递增到非托管块中的下一个地址。After each copy operation, it decrements the pointer to the address of the next location in the unmanaged ANSI string and increments the pointer to the next address in the unmanaged block.

  • 调用 Marshal.PtrToStringAnsi 将包含复制的 ANSI 字符串的非托管内存块转换为托管 Unicode String 对象。Calls the Marshal.PtrToStringAnsi to convert the unmanaged memory block containing the copied ANSI string to a managed Unicode String object.

  • 在显示原始和反向字符串后,调用 Marshal.FreeHGlobal 方法,以释放为非托管 ANSI 字符串和非托管内存块分配的内存。After displaying the original and reversed strings, calls the Marshal.FreeHGlobal method to free the memory allocated for the unmanaged ANSI string and the unmanaged block of memory.

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

Applies to