MarshalAs Attribute


MarshalAs Attribute

Class

System.Runtime.InteropServices.MarshalAsAttribute

Applies To

Field, Parameter, ReturnValue

Constructor

     New(unmanagedType) 


unmanagedType (required; UnmanagedType enumeration)

Indicates the unmanaged COM data type to which the data is to be converted. One of the UnmanagedType enumeration members (see below).

Properties


Value (UnmanagedType enumeration)

The unmanaged COM data type that the managed .NET data is to be marshaled as. Defined through the constructor's unmanagedType parameter. One of the UnmanagedType enumeration members (see below).

Fields


ArraySubType (UnmanagedType enumeration)

The subtype of an array of type ByValArray or LPArray. It is used when an array contains strings. This allows the runtime to correctly marshal a string array to COM. One of the UnmanagedType enumeration members (see below).


MarshalCookie (String)

An undefined field that can be used to pass user-defined data to a custom marshaler. The value of the MarshalCookie field as passed to the custom marshaler's GetInstance method.


MarshalType (String)

The fully qualified name of a custom marshaler. It is required if the Value property is set to CustomMarshaler.


MarshalTypeRef (Type)

Implements the MarshalType value as a Type rather than as a string.


SafeArraySubType (VarEnum enumeration)

The data type of a SafeArray. One of the System.Runtime.InteropServices.VarEnum enumeration values:

Value

Description

VT_ARRAY

A SAFEARRAY pointer

VT_BLOB

A length-prefixed collection of bytes

VT_BLOB_OBJECT

A VT_BLOB containing an object

VT_BOOL

A Boolean value

VT_BSTR

A string of type BSTR

VT_BYREF

A value passed by reference

VT_CARRAY

A C-style array

VT_CF

Clipboard format

VT_CLSID

A class identifier (CLSID)

VT_CY

A currency value

VT_DATE

A date or time value

VT_DECIMAL

A decimal value

VT_DISPATCH

An IDispatch pointer

VT_EMPTY

No specified value

VT_ERROR

An SCODE value

VT_FILETIME

A FILETIME value

VT_HRESULT

An HRESULT value

VT_I1

A signed 8-bit integer

VT_I2

A signed 16-bit integer

VT_I4

A signed 32-bit integer

VT_I8

A signed 64-bit integer

VT_INT

An integer value

VT_LPSTR

A null-terminated string

VT_LPWSTR

A null-terminated Unicode string

VT_NULL

A null reference (Nothing)

VT_PTR

A pointer

VT_R4

A floating point value

VT_R8

A double value

VT_RECORD

A user-defined type

VT_SAFEARRAY

A SAFEARRAY

VT_STORAGE

A named storage entry

VT_STORED_OBJECT

Storage containing an object

VT_STREAM

A named stream

VT_STREAMED_OBJECT

A stream containing an object

VT_UI1

An unsigned 8-bit integer

VT_UI2

An unsigned 16-bit integer

VT_UI4

An unsigned 32-bit integer

VT_UI8

An unsigned 64-bit integer

VT_UINT

An unsigned integer

VT_UNKNOWN

An IUnknown pointer

VT_USERDEFINED

A user-defined type

VT_VARIANT

A VARIANT far pointer

VT_VECTOR

A simple counted array

VT_VOID

A C-style void



SafeArrayUserDefinedSubType (Type)

The user-defined type of the SAFEARRAY. This field is used only when the value of the SafeArraySubType field is VT_UNKNOWN, VT_DISPATCH, or VT_RECORD.


SizeConst (Integer)

The number of elements in a fixed-length array.


SizeParamIndex (Short)

Indicates which zero-based parameter contains a count of array elements.

Description

The <MarshalAs> attribute defines the correct type conversion between managed and unmanaged data. Unmanaged types are defined by the following System.Runtime.InteropServices.UnmanagedType enumeration values.

Value

Description

AnsiBStr

An ANSI BSTR (a character string whose first byte indicates the string length).

AsAny

Dynamic type determination at runtime.

Bool

4-byte Boolean (true <> 0, False = 0).

BStr

A Unicode BSTR (a character string with first two bytes that indicate the string length).

ByValArray

An array passed by value. An array that is a field in a structure must have this attribute. The SizeConst field must be set to the number of array elements, and the ArraySubType field can optionally be set to the unmanaged data type of the array.

ByValTStr

An inline fixed-length character array within a structure. The character type is determined by the CharSet argument of the containing structure's <StructLayout> attribute.

Currency

A COM Currency data type. Used with the .NET Decimal data type.

CustomMarshaler

A custom marshaler class. The class is defined by the MarshalType or MarshelTypeRef field. Additional information can be passed to the custom marshaler by the MarshalCookie field.

Error

An hrESULT value. The native .NET type should be a 4-byte signed or unsigned integer.

FunctionPtr

A function pointer.

I1

A 1-byte signed integer.

I2

A 2-byte signed integer.

I4

A 4-byte signed integer.

I8

An 8-byte signed integer.

IDispatch

A COM IDispatch pointer.

Interface

A COM interface pointer.

IUnknown

A COM IUnknown pointer.

LPArray

A C-style array. Its length is indicated by the SizeConst and SizeParamIndex fields. Optionally, the ArraySubType field can indicate the unmanaged type of string elements within the array.

LPStr

An ANSI (single-byte) character string.

LPStruct

A pointer to a structure.

LPTStr

A platform-dependent character string (ANSI on Windows 9x, Unicode on Windows NT/2000/XP). LPTStr is supported only for platform invoke and not for COM interop.

LPWStr

A Unicode (double-byte) character string.

R4

A 4-byte floating point number.

R8

An 8-byte floating point number.

SafeArray

A SafeArray (a self-describing array that includes information on its type, dimension, and bounds).

Struct

A C-style structure used to marshal .NET formatted classes and value types.

SysInt

A platform-dependent integer (4 bytes on 32-bit Windows, 8 bytes on 64-bit Windows).

SysUInt

The hardware's natural-sized unsigned integer.

TBStr

A length-prefixed, platform-dependent character string (ANSI in Windows 9x, Unicode on Windows NT/2000/XP).

U1

A 1-byte unsigned integer.

U2

A 2-byte unsigned integer.

U4

A 4-byte unsigned integer.

U8

An 8-byte unsigned integer.

VariantBool

A 2-byte OLE-defined Boolean value (true = -1, False = 0).

VBByRefStr

Allows Visual Basic to change a string in unmanaged code and reflect the change in managed code.


See Also

COMClass Attribute




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net