Marshal.PtrToStringUTF8 Method

Definition

Overloads

PtrToStringUTF8(IntPtr)

Allocates a managed String and copies all characters up to the first null character from an unmanaged UTF-8 string into it.

PtrToStringUTF8(IntPtr, Int32)

Allocates a managed String and copies a specified number of bytes from an unmanaged UTF8 string into it.

PtrToStringUTF8(IntPtr)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Allocates a managed String and copies all characters up to the first null character from an unmanaged UTF-8 string into it.

C#
public static string? PtrToStringUTF8(IntPtr ptr);
C#
[System.Security.SecurityCritical]
public static string PtrToStringUTF8(IntPtr ptr);
C#
public static string PtrToStringUTF8(IntPtr ptr);

Parameters

ptr
IntPtr

The address of the first character of the unmanaged string.

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

PtrToStringUTF8 is useful for custom marshaling or for use 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.StringToCoTaskMemUTF8 methods.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1

PtrToStringUTF8(IntPtr, Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Allocates a managed String and copies a specified number of bytes from an unmanaged UTF8 string into it.

C#
public static string PtrToStringUTF8(IntPtr ptr, int byteLen);
C#
[System.Security.SecurityCritical]
public static string PtrToStringUTF8(IntPtr ptr, int byteLen);

Parameters

ptr
IntPtr

The address of the first character of the unmanaged string.

byteLen
Int32

The number of bytes to copy.

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

PtrToStringUTF8 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.StringToCoTaskMemUTF8 methods.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1