Marshal.StringToHGlobalAnsi(String) Méthode

Définition

Copie le contenu d’un objet String managé dans la mémoire non managée, avec conversion au format ANSI pendant la copie.

public:
 static IntPtr StringToHGlobalAnsi(System::String ^ s);
[System.Security.SecurityCritical]
public static IntPtr StringToHGlobalAnsi (string s);
public static IntPtr StringToHGlobalAnsi (string? s);
public static IntPtr StringToHGlobalAnsi (string s);
[<System.Security.SecurityCritical>]
static member StringToHGlobalAnsi : string -> nativeint
static member StringToHGlobalAnsi : string -> nativeint
Public Shared Function StringToHGlobalAnsi (s As String) As IntPtr

Paramètres

s
String

Chaîne managée à copier.

Retours

IntPtr

nativeint

Adresse, dans la mémoire non managée, où s a été copié, ou 0 si s est null.

Attributs

Exceptions

Il n’y a pas suffisamment de mémoire disponible.

Le paramètre s dépasse la longueur maximale autorisée par le système d'exploitation.

Exemples

L’exemple suivant montre comment convertir le contenu d’une classe managée String en mémoire non managée, puis supprimer la mémoire non managée lorsque vous avez terminé.

using namespace System;
using namespace System::Runtime::InteropServices;

#include <iostream>                                                 // for printf


int main()
{
    // Create a managed string.
    String^ managedString = "Hello unmanaged world (from the managed world).";

    // Marshal the managed string to unmanaged memory.
    char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();

    printf("stringPointer = %s\n", stringPointer);

    // Always free the unmanaged string.
    Marshal::FreeHGlobal(IntPtr(stringPointer));

    return 0;
}
using System;
using System.Runtime.InteropServices;

class MainFunction
{
    static void Main()
    {
    Console.WriteLine("\nStringToGlobalAnsi\n");

    // Create a managed string.
    String  managedString = "I am a managed String";
    Console.WriteLine("1) managedString = " + managedString );

    // Marshal the managed string to unmanaged memory.
    IntPtr stringPointer = (IntPtr)Marshal.StringToHGlobalAnsi(managedString);
    Console.WriteLine("2) stringPointer = {0}", stringPointer );

    // Get the string back from unmanaged memory
    String RetrievedString = Marshal.PtrToStringAnsi( stringPointer);
    Console.WriteLine("3) Retrieved from unmanaged memory = " + RetrievedString );

    // Always free the unmanaged string.
    Marshal.FreeHGlobal(stringPointer);

    // IntPtr handle value is still the same:
    Console.WriteLine("4) stringPointer = " + stringPointer );

    // However, it contains no data after being freed:
    String RetrievedString2 = Marshal.PtrToStringAnsi( stringPointer);
    Console.WriteLine("5) RetrievedString2 = " + RetrievedString2 );
    }
}

Remarques

StringToHGlobalAnsi est utile pour le marshaling personnalisé ou lors de la combinaison de code managé et non managé. Étant donné que cette méthode alloue la mémoire non managée requise pour une chaîne, libérez toujours la mémoire en appelant FreeHGlobal. StringToHGlobalAnsi fournit les fonctionnalités opposées de Marshal.PtrToStringAnsi.

Cette méthode copie les caractères null incorporés et inclut un caractère null de fin.

S’applique à

Voir aussi