IntPtr.ToPointer Méthode

Définition

Important

Cette API n’est pas conforme CLS.

Convertit la valeur de cette instance en pointeur vers un type non spécifié.

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

Retours

Void*

Pointeur vers Void ; c'est-à-dire, un pointeur vers une mémoire contenant des données de type non spécifié.

Attributs

Exemples

L’exemple suivant utilise des pointeurs managés pour inverser les caractères d’un tableau. Une fois qu’il initialise un String objet et obtient sa longueur, il effectue les opérations suivantes :

  • Appelle la Marshal.StringToHGlobalAnsi méthode pour copier la chaîne Unicode en mémoire non managée en caractères ANSI (un octet). La méthode retourne un IntPtr objet qui pointe vers le début de la chaîne non managée.

  • Appelle la Marshal.AllocHGlobal méthode pour allouer le même nombre d’octets que la chaîne non managée occupe. La méthode retourne un IntPtr objet qui pointe vers le début du bloc de mémoire non managé.

  • Appelle la ToPointer méthode pour obtenir un pointeur non managé vers l’adresse de départ de la chaîne et le bloc de mémoire non managé, et ajoute une valeur inférieure à la longueur de la chaîne à l’adresse de départ de la chaîne ANSI. Étant donné que le pointeur de chaîne non managé pointe maintenant vers la fin de la chaîne, l’opération de copie copie copiera un caractère de la fin de la chaîne au début du bloc de mémoire.

  • Utilise une boucle pour copier chaque caractère de la chaîne vers le bloc de mémoire non managé. Après chaque opération de copie, il décrémente le pointeur vers l’adresse de l’emplacement suivant dans la chaîne ANSI non managée et incrémente le pointeur à l’adresse suivante dans le bloc non managé.

  • Appelle le Marshal.PtrToStringAnsi bloc de mémoire non managé contenant la chaîne ANSI copiée en objet Unicode String managé.

  • Après avoir affiché les chaînes d’origine et inversées, appelle la Marshal.FreeHGlobal méthode pour libérer la mémoire allouée pour la chaîne ANSI non managée et le bloc de mémoire non managé.

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

S’applique à