Marshal.PtrToStringAuto Method

Definition

Allocates a managed String and copies all or part of an unmanaged string into it.

Overloads

PtrToStringAuto(IntPtr)

Allocates a managed String and copies all characters up to the first null character from a string stored in unmanaged memory into it.

PtrToStringAuto(IntPtr, Int32)

Allocates a managed String and copies the specified number of characters from a string stored in unmanaged memory into it.

PtrToStringAuto(IntPtr)

Allocates a managed String and copies all characters up to the first null character from a string stored in unmanaged memory into it.

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

Parameters

ptr
IntPtr

For Unicode platforms, the address of the first Unicode character.

-or-

For ANSI platforms, the address of the first ANSI character.

Returns

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

Attributes

Remarks

If the current platform is Unicode, each ANSI character is widened to a Unicode character and this method calls PtrToStringUni. Otherwise, this method calls PtrToStringAnsi.

PtrToStringAuto 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. PtrToStringAuto provides the opposite functionality of the Marshal.StringToCoTaskMemAuto and Marshal.StringToHGlobalAuto methods.

See also

PtrToStringAuto(IntPtr, Int32)

Allocates a managed String and copies the specified number of characters from a string stored in unmanaged memory into it.

public:
 static System::String ^ PtrToStringAuto(IntPtr ptr, int len);
public static string PtrToStringAuto (IntPtr ptr, int len);
[System.Security.SecurityCritical]
public static string PtrToStringAuto (IntPtr ptr, int len);
static member PtrToStringAuto : nativeint * int -> string
Public Shared Function PtrToStringAuto (ptr As IntPtr, len As Integer) As String

Parameters

ptr
IntPtr

For Unicode platforms, the address of the first Unicode character.

-or-

For ANSI platforms, the address of the first ANSI character.

len
Int32

The number of characters to copy.

Returns

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

Attributes

Exceptions

len is less than zero.

Remarks

On Unicode platforms, this method calls PtrToStringUni; on ANSI platforms, it calls PtrToStringAnsi. No transformations are done before these methods are called.

PtrToStringAuto 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. PtrToStringAuto provides the opposite functionality of Marshal.StringToCoTaskMemAuto and Marshal.StringToHGlobalAuto.

See also

Applies to