System.Runtime.InteropServices.Marshal.StringToBSTR Method

Allocates a BSTR and copies the contents of a managed string into it.

Syntax

public static IntPtr StringToBSTR (string s)

Parameters

s
The managed string to be copied.

Returns

An unmanaged pointer to the BSTR, or 0 if s is null.

Remarks

Marshal.StringToBSTR(string) is useful for custom marshaling or when mixing managed and unmanaged code. Because this method allocates the unmanaged memory required for a string, always free the BSTR when finished by calling Marshal.FreeBSTR(IntPtr). This method provides the opposite functionality of Marshal.PtrToStringBSTR(IntPtr).

Requirements

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