Marshal.PtrToStringAnsi Metodo

Definizione

Alloca un String gestito e copia in essa tutte o parte di una stringa ANSI non gestita (in Windows) o UTF-8 (on Unix).

Overload

Nome Descrizione
PtrToStringAnsi(IntPtr)

Copia tutti i caratteri fino al primo carattere Null da una stringa ANSI o UTF-8 non gestita a un oggetto gestito Stringe estende ogni carattere a UTF-16.

PtrToStringAnsi(IntPtr, Int32)

Alloca un oggetto gestito String, copia un numero specificato di caratteri da una stringa ANSI o UTF-8 non gestita e estende ogni carattere a UTF-16.

PtrToStringAnsi(IntPtr)

Origine:
Marshal.cs
Origine:
Marshal.cs
Origine:
Marshal.cs
Origine:
Marshal.cs
Origine:
Marshal.cs

Copia tutti i caratteri fino al primo carattere Null da una stringa ANSI o UTF-8 non gestita a un oggetto gestito Stringe estende ogni carattere a 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

Parametri

ptr
IntPtr

nativeint

Indirizzo del primo carattere della stringa non gestita.

Valori restituiti

Stringa gestita che contiene una copia della stringa non gestita. Se ptr è null, il metodo restituisce una stringa Null.

Attributi

Esempio

Nell'esempio seguente viene usato il PtrToStringAnsi metodo per creare una stringa gestita da una matrice non gestita 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);
}

Commenti

PtrToStringAnsi è utile per il marshalling personalizzato o quando si combina codice gestito e non gestito. Poiché questo metodo crea una copia del contenuto della stringa non gestita, è necessario liberare la stringa originale in base alle esigenze. Questo metodo fornisce la funzionalità opposta dei Marshal.StringToCoTaskMemAnsi metodi e Marshal.StringToHGlobalAnsi .

Vedi anche

Si applica a

PtrToStringAnsi(IntPtr, Int32)

Origine:
Marshal.cs
Origine:
Marshal.cs
Origine:
Marshal.cs
Origine:
Marshal.cs
Origine:
Marshal.cs

Alloca un oggetto gestito String, copia un numero specificato di caratteri da una stringa ANSI o UTF-8 non gestita e estende ogni carattere a 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

Parametri

ptr
IntPtr

nativeint

Indirizzo del primo carattere della stringa non gestita.

len
Int32

Numero di byte della stringa di input da copiare.

Valori restituiti

Stringa gestita che contiene una copia della stringa nativa se il valore del ptr parametro non nullè . In caso contrario, questo metodo restituisce null.

Attributi

Eccezioni

len è minore di zero.

Esempio

Nell'esempio seguente viene usato il PtrToStringAnsi metodo per creare una stringa gestita da una matrice non gestitachar .

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);
}

Commenti

PtrToStringAnsi è utile per il marshalling personalizzato o quando si combina codice gestito e non gestito. Poiché questo metodo crea una copia del contenuto della stringa non gestita, è necessario liberare la stringa originale in base alle esigenze. Questo metodo fornisce la funzionalità opposta dei Marshal.StringToCoTaskMemAnsi metodi e Marshal.StringToHGlobalAnsi .

Vedi anche

Si applica a