For easy COM interoperability (COM interop) reference, the following sections present tables of .NET type conversions, variants for .NET types, COM attributes, and Marshal class methods.
Table 10-1 shows .NET type conversions for Interface Definition Language (IDL) types and Visual Basic 6 (VB6) types.
|
.NET Type |
IDL Type |
VB6 Type |
|---|---|---|
|
System.SByte |
char |
N/A |
|
System.Int16 |
short |
Integer |
|
System.Int32 |
long |
Long |
|
with UnmanagedType.Error |
HRESULT |
Long |
|
System.Int64 |
int64 |
N/A |
|
System.IntPtr |
long |
Long |
|
System.Char |
||
|
default parameters |
unsigned short |
N/A |
|
default for struct fields (ANSI) |
unsigned char |
Byte |
|
default for struct fields (Unicode) |
unsigned short |
N/A |
|
with UnmanagedType.U1 |
unsigned char |
Byte |
|
with UnmanagedType.U2 |
unsigned. short |
N/A |
|
System.UInt16 |
unsigned short |
N/A |
|
System.UInt32 |
unsigned long |
N/A |
|
with UnmanagedType.Error |
HRESULT |
Long |
|
System.UInt64 |
uint64 |
N/A |
|
System.UIntPtr |
unsigned long |
N/A |
|
System.Single |
single |
Single |
|
System.Double |
double |
Double |
|
System.Boolean |
||
|
default for parameters |
VARIANT_BOOL |
Boolean |
|
default for struct fields |
long |
Long |
|
with UnmanagedType.Bool |
long |
Long |
|
with UnmanagedType.I1 |
unsigned char |
Byte |
|
with UnmanagedType.U1 |
unsigned char |
Byte |
|
with UnmanagedType.VariantBool |
VARIANT_BOOL |
Boolean |
|
System.DateTime |
DATE |
Date |
|
System.Guid |
GUID |
N/A |
|
System.Decimal |
||
|
default |
DECIMAL |
N/A |
|
with UnmanagedType.Currency |
CURRENCY |
Currency |
|
System.String |
||
|
default for parameters |
BSTR |
String |
|
default for struct fields |
LPSTR |
String |
|
with UnmanagedType.LPStr |
LPSTR |
String |
|
with UnmanagedType.LPWStr |
LPWSTR |
String |
|
with UnmanagedType.CustomMarshaler |
long |
Long |
|
System.Text.StringBuilder |
LPSTR |
String |
|
with UnmanagedType.LPStr |
LPSTR |
String |
|
with UnmanagedType.LPWStr |
LPWSTR |
String |
|
with UnmanagedType.CustomerMarshaler |
IUnknown* |
Unknown |
|
System.Object |
VARIANT |
Variant |
|
default parameters |
IUnknown* |
Unknown |
|
default for struct fields |
IUnknown* |
Unknown |
|
with UnmanagedType.IUnknown |
IUnknown* |
Unknown |
|
with UnmanagedType.IDispatch |
IDispatch* |
Object |
|
with UnmanagedType.Struct |
VARIANT |
Variant |
|
with UnmanagedType.CustomerMarshaler |
IUnknown* |
Unknown |
|
with UnmanagedType.Interface |
IUnknown* |
Unknown |
|
AnInterface |
AnInterface* |
SomeClass or Interface |
|
with UnmanagedType.CustomMarshaler |
IUnknown* |
Unknown |
|
SomeClass |
DefaultInterface* |
SomeClass |
|
with UnmanagedType.CustomerMarshaler |
IUnknown* |
Unknown |
|
SomeFormattedClass |
SomeStruct* |
ByRef SomeUDT |
|
with UnmanagedType.CustomMarshaler |
IUnknown* |
Unknown |
|
SomeDelegatedClass |
||
|
default parameters |
DefaultInterface* |
SomeDelegatedClass |
|
default for struct fields |
int |
Long |
|
with UnmanagedType.FunctionPtr |
int |
Long |
|
with UnmanagedType.CustomMarshaler |
IUnknown* |
Unknown |
|
SomeValueType |
SomeStruct |
SomeUDT |
|
SomeEnum |
||
|
with 32-bit underlying type |
SomeEnum |
SomeEnum |
|
with any other underlying type |
underlying type |
underlying type |
|
array of some type |
||
|
default parameters |
SAFEARRAY(Type) |
SomeType () |
|
with UnmanagedType.ByValArray |
SomeType [size] |
SomeType (0 to −1) |
|
with UnmanagedType.LPArray |
(parameters only) |
SomeType* |
|
SomeType |
||
|
with UnmanagedType.SafeArray |
SAFEARRAY(Type) |
SomeType () |
|
with UnmanagedType.CustomMarshaler |
long |
Long |
Table 10-2 lists the .NET types and their corresponding variant types. When calling methods that have optional variant types, you must use Type.Missing, because C# does not support optional/default parameters.
|
.NET Type |
Variant Type |
|---|---|
|
null |
VT_EMPTY |
|
System.DBNull |
VT_NULL |
|
System.Runtime.InteropServices.CurrencyWrapper |
VT_CY |
|
System.Runtime.InteropServices.UnknownWrapper |
VT_UNKNOWN |
|
System.Runtime.InteropServices.DispatchWrapper |
VT_DISPATCH |
|
System.Runtime.InteropServices.ErrorWrapper |
VT_ERROR |
|
System.Reflection.Missing |
VT_ERROR |
|
System.String |
VT_BSTR |
|
System.Decimal |
VT_DECIMAL |
|
System.Boolean |
VT_BOOL |
|
System.Char |
VT_U2 |
|
System.Byte |
VT_U1 |
|
System.SByte |
VT_I1 |
|
System.Int16 |
VT_I2 |
|
System.Int32 |
VT_I4 |
|
System.Int64 |
VT_I8 |
|
System.IntPtr |
VT_INT |
|
System.UInt16 |
VT_U2 |
|
System.UInt32 |
VT_U4 |
|
System.UInt64 |
VT_U8 |
|
System.UIntPtr |
VT_UINT |
|
System.Single |
VT_R4 |
|
System.Double |
VT_R8 |
|
System.DateTime |
VT_DATE |
|
Array |
VT_ARRAY |
|
System.Object or other .NET class |
VT_DISPATCH |
Table 10-3 lists COM attributes with their descriptions.
|
Attribute |
Description |
|---|---|
|
AutomationProxy |
Sets if an object should be custom marshaled or marshaled using the automation marshaler |
|
ClassInterface |
Sets the type of interface generated for a class |
|
CoClass |
Identifies the original CLSID for an object |
|
ComImport |
Indicates that a class or interface was imported to managed code |
|
ComRegisterFunction |
Sets a method to be called when an assembly is registered for use with COM |
|
ComSourceInterfaces |
Identifies the source interface for events |
|
ComUnregisterFunction |
Sets a method to be called when an assembly is unregistered for COM |
|
ComVisible |
Sets or determines if an object is visible to COM |
|
DispId |
Sets the dispatch ID for a method or property |
|
FieldOffset |
Sets the physical position of a field within a class or struct |
|
Guid |
Sets a GUID on a class or an interface |
|
IDispatchImp |
Sets which IDispatch implementation is used by managed code when exposing dual interfaces or dispinterfaces |
|
LCIDConversion |
Sets an unmanaged method signature LCID parameter |
|
MarshalAs |
Sets how data is marshaled to COM |
|
Optional |
Sets a parameter to be optional |
|
Out |
Sets a parameter as optional |
|
PreserveSig |
Suppresses HRESULT or retval signature when making COM calls |
|
ProgId |
Sets the program ID of a class |
|
StructLayout |
Sets or controls the physical layout of structs |
The Marshal class is used to interact with unmanaged code. Table 10-4 lists the Marshal class methods.
|
Method |
Description |
|---|---|
|
AddRef |
Adds a reference to an interface |
|
AllocCoTaskMem |
Allocates an unmanaged memory chunk |
|
BindToMoniker |
Get an interface moniker from a specified moniker |
|
CreateWrapperOfType |
Wraps a COM type with a designated type |
|
Destroy Structure |
Frees substructures of an unmanaged clock |
|
FreeCoTaskMem |
Frees an unmanaged memory chunk |
|
GetActiveObject |
Obtains an instance from the running object table (ROT) |
|
GetComInterfaceForObject |
Returns an IUnknown pointer for an object |
|
GetFunctionPointerForDelegate |
Used to pass a function pointer for a managed object to unmanaged code |
|
IsComObject |
Determines if an object is a native COM object |
|
OffsetOf |
Returns the field offset of an unmanaged object |
|
PtrToStringXXXX (Ansi, Bstr, or Uni) |
Given a pointer to an unmanaged string, returns a string object |
|
PtrToStructure |
Given a pointer to an unmanaged struct, returns a managed struct |
|
QueryInterface |
Returns a COM interface |
|
Release |
Releases a reference to an interface |
|
StructureToPtr |
Given a managed struct, returns an unmanaged copy |