Bewerken

Delen via


Marshal.StringToHGlobalAnsi(String) Method

Definition

Copies the contents of a managed String into unmanaged memory, converting into ANSI format as it copies.

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

Parameters

s
String

A managed string to be copied.

Returns

IntPtr

nativeint

The address, in unmanaged memory, to where s was copied, or 0 if s is null.

Attributes

Exceptions

There is insufficient memory available.

The s parameter exceeds the maximum length allowed by the operating system.

Examples

The following example demonstrates how to convert the contents of a managed String class to unmanaged memory and then dispose of the unmanaged memory when done.

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;
using System.Threading;

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, the data may be cleared after the memory is freed, depending on whether the memory allocated to stringPointer
        // has been reclaimed or not. Uncommenting the following line (Thread.Sleep(1000)) increases the likelihood of the memory being reclaimed.
        // Thread.Sleep(1000);
        String RetrievedString2 = Marshal.PtrToStringAnsi(stringPointer);
        Console.WriteLine("5) RetrievedString2 = " + RetrievedString2);
    }
}

Remarks

StringToHGlobalAnsi is useful for custom marshaling or when mixing managed and unmanaged code. Because this method allocates the unmanaged memory required for a string, always free the memory by calling FreeHGlobal. StringToHGlobalAnsi provides the opposite functionality of Marshal.PtrToStringAnsi.

This method copies embedded null characters, and includes a terminating null character.

Applies to

See also