Marshal.PtrToStringAnsi Metodo

Definizione

Alloca una classe String gestita e copia tutta o una parte di una stringa non gestita ANSI (in Windows) o UTF-8 (in Unix).

Overload

PtrToStringAnsi(IntPtr)

Copia tutti i caratteri fino al primo carattere Null da una stringa non gestita ANSI o UTF-8 a una classe String gestita e trasforma ogni carattere in UTF-16.

PtrToStringAnsi(IntPtr, Int32)

Alloca una classe String gestita e vi copia un numero specifico di caratteri da una stringa non gestita ANSI o UTF-8 e trasforma ogni carattere in UTF-16.

PtrToStringAnsi(IntPtr)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Copia tutti i caratteri fino al primo carattere Null da una stringa non gestita ANSI o UTF-8 a una classe String gestita e trasforma ogni carattere in 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

L'indirizzo del primo carattere della stringa non gestita.

Restituisce

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)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Alloca una classe String gestita e vi copia un numero specifico di caratteri da una stringa non gestita ANSI o UTF-8 e trasforma ogni carattere in 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

L'indirizzo del primo carattere della stringa non gestita.

len
Int32

Conteggio dei byte della stringa di input da copiare.

Restituisce

Stringa gestita che contiene una copia della stringa nativa se il valore del parametro ptr 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