Marshal.StringToHGlobalAuto(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 le cas échéant.

public:
 static IntPtr StringToHGlobalAuto(System::String ^ s);
public static IntPtr StringToHGlobalAuto (string? s);
public static IntPtr StringToHGlobalAuto (string s);
[System.Security.SecurityCritical]
public static IntPtr StringToHGlobalAuto (string s);
static member StringToHGlobalAuto : string -> nativeint
[<System.Security.SecurityCritical>]
static member StringToHGlobalAuto : string -> nativeint
Public Shared Function StringToHGlobalAuto (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ù la chaîne a été copiée, ou 0 si s est null.

Attributs

Exceptions

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

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;

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::StringToHGlobalAuto(managedString).ToPointer();

    // Pass the string to an unmanaged API.

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

    return 0;
}

Remarques

StringToHGlobalAuto est utile pour le marshaling personnalisé ou pour une utilisation 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. Cette méthode fournit les fonctionnalités opposées de Marshal.PtrToStringAuto.

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

S’applique à

Voir aussi