System.Runtime.InteropServices.Marshal.PtrToStringBSTR Method

Allocates a managed string and copies a BSTR string stored in unmanaged memory into it.

Syntax

public static string PtrToStringBSTR (IntPtr ptr)

Parameters

ptr
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.

Remarks

Call this method only on strings that were allocated with the unmanaged SysAllocString and SysAllocStringLen functions.

Marshal.PtrToStringBSTR(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.StringToBSTR(string) method.

Requirements

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