System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi Method

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

Syntax

public static IntPtr StringToHGlobalAnsi (string s)

Parameters

s
A managed string to be copied.

Returns

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

Remarks

Marshal.StringToHGlobalAnsi(string) 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 Marshal.FreeHGlobal(IntPtr). Marshal.StringToHGlobalAnsi(string) provides the opposite functionality of Marshal.PtrToStringAnsi(IntPtr).

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

Requirements

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0