Using the MarshalAsAttribute Attribute
You can use the MarshalAsAttribute attribute to customize marshaling behavior. For example, you can perform the following tasks:
Marshal arrays and strings in a structure.
Provide needed flexibility to support common COM interop scenarios.
Provide consistency in marshaling types through the platform invoke and vtable marshaler as compared to the IDispatch marshaler.
The following table describes marshaling support for the values from the UnmanagedType enumeration.
Unmanaged Type | Types valid on | Supported in structures |
---|---|---|
BStr, TBStr |
||
LPWStr, LPStr, LPTStr |
String |
Yes |
IUnknown |
Yes |
|
IDispatch |
Object |
Yes |
ByValTStr |
String (in structures only) |
Yes |
ByValArray |
Any array |
Yes |
SafeArray |
Any array |
Yes |
VBByRefStr |
String |
|
Bool |
integer types |
|
I1, I2, I4 |
Integer types of appropriate length |
|
U1, U2, U4 |
Unsigned integer types of appropriate length |
|
I8 |
N/A |
|
U8 |
N/A |
|
R4 |
4-byte real numbers |
|
R8 |
8-byte real numbers |
|
Struct |
Object |
|
Interface |
Object |
|
SysInt, SysUInt |
Integer types of appropriate sign and length |
|
AnsiBStr |
N/A |
|
VariantBool |
Boolean |
|
FunctionPtr |
N/A |
|
AsAny |
N/A |
|
LPArray |
N/A |
|
LPStruct |
N/A |
|
Error |
4 byte integers |
|
Currency |