Marshal.PtrToStringAnsi Metodo
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Alloca un String gestito e copia in essa una stringa ANSI (in Windows) o UTF-8 (on Unix) non gestita.
PtrToStringAnsi(IntPtr) |
Copia tutti i caratteri fino al primo carattere Null da una stringa ANSI o UTF-8 non gestita a un Stringgestito e estende ogni carattere a UTF-16. |
PtrToStringAnsi(IntPtr, Int32) |
Alloca un Stringgestito, copia un numero specificato di caratteri da una stringa ANSI o UTF-8 non gestita e estende ogni carattere a UTF-16. |
- 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 Stringgestito e 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.
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 metodo PtrToStringAnsi per creare una stringa gestita da una matrice di char
non gestita.
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 metodi Marshal.StringToCoTaskMemAnsi e Marshal.StringToHGlobalAnsi.
Vedi anche
Si applica a
.NET 9 e altre versioni
Prodotto | Versioni |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Origine:
- Marshal.cs
- Origine:
- Marshal.cs
- Origine:
- Marshal.cs
Alloca un Stringgestito, 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.
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 metodo PtrToStringAnsi per creare una stringa gestita da una matrice dichar
non gestita.
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 metodi Marshal.StringToCoTaskMemAnsi e Marshal.StringToHGlobalAnsi.
Vedi anche
Si applica a
.NET 9 e altre versioni
Prodotto | Versioni |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: