Marshal.PtrToStringAnsi Méthode

Définition

Alloue un objet String managé et y copie la totalité ou une partie d’une chaîne ANSI (sur Windows) ou UTF-8 (sur UNIX) non managée.

Surcharges

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 un objet String managé et élargit chaque caractère au format UTF-16.

PtrToStringAnsi(IntPtr, Int32)

Alloue un objet String managé, y copie un nombre spécifié de caractères d'une chaîne ANSI ou UTF-8 non managée et élargit chaque caractère au format 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 un objet String managé et élargit chaque caractère au format UTF-16.

public:
 static System::String ^ PtrToStringAnsi(IntPtr ptr);
[System.Security.SecurityCritical]
public static string PtrToStringAnsi (IntPtr ptr);
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

String

Chaîne managée qui contient une copie de la chaîne non managée. Si ptr est null, la méthode retourne une chaîne vide.

Attributs

Exemples

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

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 de la combinaison 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 le cas échéant. 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 un objet String managé, y copie un nombre spécifié de caractères d'une chaîne ANSI ou UTF-8 non managée et élargit chaque caractère au format 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

String

Chaîne managée qui contient une copie de la chaîne native si la valeur du paramètre ptr 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ée à partir d’un tableau non managéchar .

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 de la combinaison 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 le cas échéant. Cette méthode fournit les fonctionnalités opposées des méthodes et Marshal.StringToHGlobalAnsi des Marshal.StringToCoTaskMemAnsi méthodes.

Voir aussi

S’applique à