Marshal.PtrToStringUni Method

Definition

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

Overloads

PtrToStringUni(IntPtr)

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

PtrToStringUni(IntPtr, Int32)

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

PtrToStringUni(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 Unicode string into it.

[System.Security.SecurityCritical]
public static string PtrToStringUni (IntPtr ptr);
public static string? PtrToStringUni (IntPtr ptr);
public static string PtrToStringUni (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

PtrToStringUni 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.StringToCoTaskMemUni and Marshal.StringToHGlobalUni methods.

This API reflects the Windows definition of Unicode, which is a UTF-16 2-byte encoding. On many non-Windows platforms, the wchar_t data-type is 4-bytes, not 2-bytes. Consult your compiler to confirm if wchar_t can be used or char16_t should be used instead.

See also

Applies to

.NET 9 și alte versiuni
Produs Versiuni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

PtrToStringUni(IntPtr, Int32)

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

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

[System.Security.SecurityCritical]
public static string PtrToStringUni (IntPtr ptr, int len);
public static string PtrToStringUni (IntPtr ptr, int len);

Parameters

ptr
IntPtr

The address of the first character of the unmanaged string.

len
Int32

The number of Unicode characters 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

PtrToStringUni 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.StringToCoTaskMemUni and Marshal.StringToHGlobalUni methods.

This API reflects the Windows definition of Unicode, which is a UTF-16 2-byte encoding. On many non-Windows platforms, the wchar_t data-type is 4-bytes, not 2-bytes. Consult your compiler to confirm if wchar_t can be used or char16_t should be used instead.

See also

Applies to

.NET 9 și alte versiuni
Produs Versiuni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0