Marshal.StringToHGlobalAnsi(String) Método

Definição

Copia o conteúdo de um String gerenciado para a memória não gerenciada, convertendo em formato ANSI durante a cópia.

public:
 static IntPtr StringToHGlobalAnsi(System::String ^ s);
[System.Security.SecurityCritical]
public static IntPtr StringToHGlobalAnsi (string s);
public static IntPtr StringToHGlobalAnsi (string? s);
public static IntPtr StringToHGlobalAnsi (string s);
[<System.Security.SecurityCritical>]
static member StringToHGlobalAnsi : string -> nativeint
static member StringToHGlobalAnsi : string -> nativeint
Public Shared Function StringToHGlobalAnsi (s As String) As IntPtr

Parâmetros

s
String

Uma cadeia de caracteres gerenciada a ser copiada.

Retornos

IntPtr

nativeint

O endereço, na memória não gerenciada, para o qual s foi copiado ou 0 se s for null.

Atributos

Exceções

Memória insuficiente.

O parâmetro s excede o tamanho máximo permitido pelo sistema operacional.

Exemplos

O exemplo a seguir demonstra como converter o conteúdo de uma classe gerenciada String em memória não gerenciada e, em seguida, descartar a memória não gerenciada quando concluído.

using namespace System;
using namespace System::Runtime::InteropServices;

#include <iostream>                                                 // for printf


int main()
{
    // Create a managed string.
    String^ managedString = "Hello unmanaged world (from the managed world).";

    // Marshal the managed string to unmanaged memory.
    char* stringPointer = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();

    printf("stringPointer = %s\n", stringPointer);

    // Always free the unmanaged string.
    Marshal::FreeHGlobal(IntPtr(stringPointer));

    return 0;
}
using System;
using System.Runtime.InteropServices;

class MainFunction
{
    static void Main()
    {
    Console.WriteLine("\nStringToGlobalAnsi\n");

    // Create a managed string.
    String  managedString = "I am a managed String";
    Console.WriteLine("1) managedString = " + managedString );

    // Marshal the managed string to unmanaged memory.
    IntPtr stringPointer = (IntPtr)Marshal.StringToHGlobalAnsi(managedString);
    Console.WriteLine("2) stringPointer = {0}", stringPointer );

    // Get the string back from unmanaged memory
    String RetrievedString = Marshal.PtrToStringAnsi( stringPointer);
    Console.WriteLine("3) Retrieved from unmanaged memory = " + RetrievedString );

    // Always free the unmanaged string.
    Marshal.FreeHGlobal(stringPointer);

    // IntPtr handle value is still the same:
    Console.WriteLine("4) stringPointer = " + stringPointer );

    // However, it contains no data after being freed:
    String RetrievedString2 = Marshal.PtrToStringAnsi( stringPointer);
    Console.WriteLine("5) RetrievedString2 = " + RetrievedString2 );
    }
}

Comentários

StringToHGlobalAnsi é útil para marshaling personalizado ou ao misturar código gerenciado e não gerenciado. Como esse método aloca a memória não gerenciada necessária para uma cadeia de caracteres, sempre libere a memória chamando FreeHGlobal. StringToHGlobalAnsi fornece a funcionalidade oposta de Marshal.PtrToStringAnsi.

Esse método copia caracteres nulos inseridos e inclui um caractere nulo de terminação.

Aplica-se a

Confira também