Marshal.PtrToStringAnsi Méthode

Définition

Alloue un String managé et copie toutes ou partie d’une chaîne ANSI non managée (sur Windows) ou UTF-8 (sur Unix) dans celle-ci.

Surcharges

Nom Description
PtrToStringAnsi(IntPtr)

Copie tous les caractères jusqu’au premier caractère null d’une chaîne ANSI ou UTF-8 non managée dans une chaîne managée String, et élargit chaque caractère à UTF-16.

PtrToStringAnsi(IntPtr, Int32)

Alloue une chaîne ANSI ou UTF-8 non managée String, copie un nombre spécifié de caractères et étend chaque caractère à UTF-16.

PtrToStringAnsi(IntPtr)

Copie tous les caractères jusqu’au premier caractère null d’une chaîne ANSI ou UTF-8 non managée dans une chaîne managée String, et élargit chaque caractère à UTF-16.

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

Paramètres

ptr
IntPtr

nativeint

Adresse du premier caractère de la chaîne non managée.

Retours

Chaîne managée qui contient une copie de la chaîne non managée. Si ptr c’est nullle cas, la méthode retourne une chaîne Null.

Attributs

Exemples

L’exemple suivant utilise la PtrToStringAnsi méthode pour créer une chaîne managée char à partir d’un tableau non managé.

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

void main()
{
    // Create an unmanaged c string.
    const char * myString = "Hello managed world (from the unmanaged world)!";
    
    // Convert the c string to a managed String.
    String ^ myManagedString = Marshal::PtrToStringAnsi((IntPtr) (char *) myString);
    
    // Display the string to the console.
    Console::WriteLine(myManagedString);
}

Remarques

PtrToStringAnsi est utile pour le marshaling personnalisé ou lors du mélange de code managé et non managé. Étant donné que cette méthode crée une copie du contenu de la chaîne non managée, vous devez libérer la chaîne d’origine selon les besoins. Cette méthode fournit les fonctionnalités opposées des méthodes et Marshal.StringToHGlobalAnsi des Marshal.StringToCoTaskMemAnsi méthodes.

Voir aussi

S’applique à

PtrToStringAnsi(IntPtr, Int32)

Alloue une chaîne ANSI ou UTF-8 non managée String, copie un nombre spécifié de caractères et étend chaque caractère à UTF-16.

public:
 static System::String ^ PtrToStringAnsi(IntPtr ptr, int len);
[System.Security.SecurityCritical]
public static string PtrToStringAnsi(IntPtr ptr, int len);
public static string PtrToStringAnsi(IntPtr ptr, int len);
[<System.Security.SecurityCritical>]
static member PtrToStringAnsi : nativeint * int -> string
static member PtrToStringAnsi : nativeint * int -> string
Public Shared Function PtrToStringAnsi (ptr As IntPtr, len As Integer) As String

Paramètres

ptr
IntPtr

nativeint

Adresse du premier caractère de la chaîne non managée.

len
Int32

Nombre d’octets de la chaîne d’entrée à copier.

Retours

Chaîne managée qui contient une copie de la chaîne native si la valeur du ptr paramètre n’est pas null; sinon, cette méthode retourne null.

Attributs

Exceptions

len est inférieur à zéro.

Exemples

L’exemple suivant utilise la PtrToStringAnsi méthode pour créer une chaîne managéechar à partir d’un tableau non managé.

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



void main()
{
    // Create an unmanaged c string.
    const char * myString = "Hello managed world (from the unmanaged world)!";

    // Convert the c string to a managed String.
    String ^ myManagedString = Marshal::PtrToStringAnsi((IntPtr) (char *) myString);

    // Display the string to the console.
    Console::WriteLine(myManagedString);
}

Remarques

PtrToStringAnsi est utile pour le marshaling personnalisé ou lors du mélange de code managé et non managé. Étant donné que cette méthode crée une copie du contenu de la chaîne non managée, vous devez libérer la chaîne d’origine selon les besoins. Cette méthode fournit les fonctionnalités opposées des méthodes et Marshal.StringToHGlobalAnsi des Marshal.StringToCoTaskMemAnsi méthodes.

Voir aussi

S’applique à