The System.Runtime.InteropServices namespace provides a collection of classes useful for accessing COM objects, and native APIs from .NET. The types in this namespace fall into the following areas of functionality: attributes, exceptions, managed definitions of COM types, wrappers, type converters, and the Marshal class.
Many attributes are provided as a means of controlling marshaling behavior, such as how a structure should be arranged, or how a String should be represented. Of the most important attributes are , which is used to define methods that access native system APIs, and , which indicates how data should be marshaled between managed and unmanaged memory. Some attributes have corresponding enumerations that specify valid values for the attribute. They are listed below their associated attribute and are prefixed with the "-" character. The attributes provided by are:
AutomationProxyAttribute
ClassInterfaceAttribute
-ClassInterfaceType
ComAliasNameAttribute
ComConvertionLossAttribute
ComImportAttribute
ComRegisterFunctionAttribute
ComSourceInterfacesAttribute
ComUnregisterFunctionAttribute
ComVisibleAttribute
DispIdAttribute
DllImportAttribute
-CallingConvention
-CharSet
FieldOffsetAttribute
GuidAttribute
IDispatchImplAttribute
-IDispatchImplType
ImportedFromTypeLibAttribute
InAttribute
InterfaceTypeAttribute
-ComInterfaceType
LCIDConvertionAttribute
MarshalAsAttribute
-VarEnum
-UnmanagedType
OptionalAttribute
OutAttribute
PreserveSigAttribute
PrimaryInteropAssemblyAttribute
ProdIdAttribute
StructLayoutAttribute
-CharSet
-LayoutKind
TypeLibFuncAttribute
-TypeLibFuncFlags
TypeLibTypeAttribute
-TypeLibTypeFlags
TypeLibVarAttribute
-TypeLibVarFlags
The exception classes provided by indicate exceptional circumstances such as errors while performing certain operations, and serve as a way to bridge COM HRESULTs to .NET exceptions. The exceptions provided by are:
COMException
ExternalException
InvalidComObjectException
InvalidOleVariantTypeException
MarshalDirectiveException
SafeArrayRankMismatchException
SafeArrayTypeMismatchException
SEHException
It is occasionally necessary to use existing COM types from managed code. To do so, a managed definition of the COM type must be available for the managed user to bind to. The namespace provides managed definitions of a handful of useful interfaces. For detailed information on the types, refer to the existing documentation in the MSDN library. The COM types provided by are:
BINDPTR
BIND_OPTS
CALLCONV
CONNECTDATA
DESCKIND
DISPPARAMS
ELEMDESC
ELEMDESC.DESCUNION
EXCEPINFO
FILETIME
FUNCDESC
FUNCFLAGS
FUNCKIND
IDLDESC
IDLFLAG
IMPLTYPEFLAGS
INVOKEKIND
LIBFLAGS
PARAMDESC
PARAMFLAG
STATSTG
SYSKIND
TYPEATTR
TYPEDESC
TYPEFLAGS
TYPEKIND
TYPELIBATTR
UCOMIBindCtx
UCOMIConnectionPoint
UCOMIConnectionPointContainer
UCOMIEnumConnectionPoints
UCOMIEnumConnections
UCOMIEnumMoniker
UCOMIEnumStream
UCOMIEnumVARIANT
UCOMIMoniker
UCOMIPersistFile
UCOMIRunningObjectTable
UCOMIStream
VARDESC
VARDESC.DESCUNION
VARFLAGS
When using COM Variants, it is sometimes necessary to wrap a COM type in a managed class in order to control the type of the Variant. Several wrappers have been designed to deal with these situations. The wrapper classes provided by are:
CurrencyWrapper
DispatchWrapper
ErrorWrapper
UnknownWrapper
Although tools are provided in the .NET Framework SDK for registering types and converting between type libraries and assemblies, it is also possible to do the same conversion and registration tasks with the classes and interfaces found in the I namespace. The conversion and registration classes provided by are:
AssemblyRegistrationFlags
ExporterEventKind
ImporterEventKind
IRegistrationServices
ITypeLibConverter
ITypeLibExporterNotifySink
ITypeLibImporterNotifySink
RegistrationServices
TypeLibConverter
TypeLibExporterFlags
TypeLibImporterFlags
The class is the largest class in, and most important part of, . It is used primarily for unmanaged memory operations and building custom marshalers, a bridge between managed and unmanaged code.
Class | Description |
---|---|
class COMException | The exception thrown when an unrecognized HRESULT is returned from a COM method call. |
class DllImportAttribute | Indicates that the attributed method is implemented as an export from an unmanaged DLL. |
class ExternalException | The base exception type for all COM interop exceptions and structured exception handling (SEH) exceptions. |
class FieldOffsetAttribute | Indicates the physical position of fields within the unmanaged representation of a class or structure. |
class InAttribute | Indicates that data should be marshaled from the caller to the callee. |
class InvalidOleVariantTypeException | The exception thrown by the marshaler when it encounters an argument of a variant type that can not be marshaled to managed code. |
class Marshal | Provides a collection of methods pertaining to allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types. |
class MarshalAsAttribute | Indicates how the data should be marshaled between managed and unmanaged code. |
class MarshalDirectiveException | The exception that is thrown by the marshaler when it encounters a MarshalAsAttribute it does not support. |
class OptionalAttribute | Indicates that a parameter is optional. |
class OutAttribute | Indicates that data should be marshaled from callee back to caller. |
class PreserveSigAttribute | Indicates that the HRESULT or retval signature transformation that takes place during COM interop calls should be suppressed. |
class RuntimeEnvironment | Provides a collection of methods that return information about the common language runtime environment. |
class SEHException | Represents Structured Exception Handler (SEH) errors. |
class StructLayoutAttribute | The class allows the user to control the physical layout of the data fields of a class or structure. |
Interface | Description |
---|---|
interface ICustomMarshaler | Designed to provide custom wrappers for handling method calls. |
Structure | Description |
---|---|
structure ArrayWithOffset | Encapsulates an array and an offset within the specified array. |
structure GCHandle | Provides a means for accessing a managed object from unmanaged memory. |
structure HandleRef | Wraps a managed object holding a handle to a resource that is passed to unmanaged code using platform invoke. |
Enumeration | Description |
---|---|
enumeration CallingConvention | Specifies the calling convention required to call methods implemented in unmanaged code. |
enumeration CharSet | Dictates which character set marshaled strings should use. |
enumeration GCHandleType | Represents the types of handles the GCHandle class can allocate. |
enumeration LayoutKind | Controls the layout of an object when exported to unmanaged code. |
enumeration UnmanagedType | Identifies how parameters or fields should be marshaled to unmanaged code. |
enumeration VarEnum | Indicates how the array elements should be marshaled when an array is marshaled from managed to unmanaged code as a System.Runtime.InteropServices.UnmanagedType.SafeArray (not supported on the shared source CLI) . |