Mono Class Library: Mono.Unix.UnixMarshal Overview | Members

Mono.Unix.UnixMarshal.PtrToString Method

Allocates a managed string and marshals the unmanaged string into the managed string using the specified encoding.

public static string PtrToString (IntPtr p, System.Text.Encoding encoding)

Parameters

p
A IntPtr containing the address of an unmanaged string in the encoding encoding.
encoding
A System.Text.Encoding specifying which encoding p is in.

Returns

A string containing a copy of the unmanaged string p, or null if p is IntPtr.Zero.

Exceptions

TypeReason
ArgumentNullExceptionencoding is null.

Remarks

The input string p must have a terminating null. The definition of "terminating null" is fluid in this context, since the size of a terminating null depends upon the encoding specified.

A terminating null is a single byte containing the value 0x00 if encoding is one of the following encodings: System.Text.ASCIIEncoding, System.Text.UTF8Encoding, System.Text.UTF7Encoding, or Mono.Unix.UnixEncoding.

A terminating null is the short value 0x0000 if encoding is System.Text.UnicodeEncoding.

Otherwise, the terminating null must consist of a sequence of System.Text.Encoding.GetMaxByteCount(1) bytes having the value 0x00.

Note: This potentially plays havoc with multi-byte encodings, as many multi-byte encodings would only use a single byte for the terminating null, and not the maximum possible number of bytes used for encoding any character to be used for the terminating null.

Any buffer returned by UnixMarshal.StringToHeap will satisfy this requirement.

Requirements

Namespace: Mono.Unix
Assembly: Mono.Posix (in Mono.Posix.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0