Método Marshal.PtrToStringAnsi (IntPtr, Int32)
Aloca um String gerenciado, copia um número especificado de caracteres de uma cadeia de caracteres ANSI não gerenciada para ele e amplia cada caractere ANSI para Unicode.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (em mscorlib.dll)
Sintaxe
[SecurityCriticalAttribute]
public static string PtrToStringAnsi(
IntPtr ptr,
int len
)
public:
[SecurityCriticalAttribute]
static String^ PtrToStringAnsi(
IntPtr ptr,
int len
)
[<SecurityCriticalAttribute>]
static member PtrToStringAnsi :
ptr:nativeint *
len:int -> string
<SecurityCriticalAttribute>
Public Shared Function PtrToStringAnsi (
ptr As IntPtr,
len As Integer
) As String
Parâmetros
ptr
Type: System.IntPtrO endereço do primeiro caractere da cadeia de caracteres não gerenciada.
len
Type: System.Int32O número de bytes da cadeia de caracteres de entrada a ser copiado.
Valor Retornado
Type: System.String
Uma cadeia de caracteres gerenciada que mantém uma cópia da cadeia de caracteres ANSI nativa se o valor do parâmetro ptr não for null; caso contrário, esse método retornará null.
Exceções
Exception | Condition |
---|---|
ArgumentException | len é menor que zero. |
Comentários
PtrToStringAnsi is useful for custom marshaling or when mixing managed and unmanaged code. Because this method creates a copy of the unmanaged string's contents, you must free the original string as appropriate. This method provides the opposite functionality of the Marshal.StringToCoTaskMemAnsi and Marshal.StringToHGlobalAnsi methods.
Exemplos
The following example uses the M:System.Runtime.InteropServices.Marshal.PtrToStringAnsi(System.IntPtr,System.Int32) method to create a managed string from an unmanagedchar array.
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);
}
Segurança
requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Informações de Versão
Plataforma Universal do Windows
Disponível desde 8
.NET Framework
Disponível desde 1.1
Biblioteca de Classes Portátil
Com suporte no: plataformas portáteis do .NET
Silverlight
Disponível desde 2.0
Windows Phone Silverlight
Disponível desde 8.0
Windows Phone
Disponível desde 8.1
Confira Também
StringToCoTaskMemAnsi
StringToHGlobalAnsi
PtrToStringAnsi Sobrecarga
Classe Marshal
Namespace System.Runtime.InteropServices
Retornar ao início