Marshal.StringToHGlobalAuto(String) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Copie le contenu d’une mémoire managée en mémoire non managée String , en convertissant au format ANSI si nécessaire.
public:
static IntPtr StringToHGlobalAuto(System::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
nativeint
Adresse, en mémoire non managée, à l’emplacement où la chaîne a été copiée, ou 0 si s c’est nullle cas.
- Attributs
Exceptions
La mémoire est insuffisante.
Exemples
L’exemple suivant montre comment convertir le contenu d’une classe de String managée 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 du mélange 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.