System.Runtime.InteropServices.Marshal.PtrToStringAnsi Method

Allocates a managed string, copies a specified number of characters from an unmanaged ANSI string into it, and widens each ANSI character to Unicode.

Syntax

public static string PtrToStringAnsi (IntPtr ptr, int len)

Parameters

ptr
The address of the first character of the unmanaged string.
len
The byte count of the input string to copy.

Returns

A managed string that holds a copy of the native ANSI string if the value of the ptr parameter is not null; otherwise, this method returns null.

Remarks

Marshal.PtrToStringAnsi(IntPtr) 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(string) and Marshal.StringToHGlobalAnsi(string) methods.

Requirements

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