ComImportAttribute Class
Assembly: mscorlib (in mscorlib.dll)
'Declaration <ComVisibleAttribute(True)> _ <AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Interface, Inherited:=False)> _ Public NotInheritable Class ComImportAttribute Inherits Attribute 'Usage Dim instance As ComImportAttribute
/** @attribute ComVisibleAttribute(true) */ /** @attribute AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Interface, Inherited=false) */ public final class ComImportAttribute extends Attribute
ComVisibleAttribute(true) AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Interface, Inherited=false) public final class ComImportAttribute extends Attribute
Not applicable.
You can apply this attribute to classes or interfaces, although the Type Library Importer (Tlbimp.exe) typically applies it for you when it imports a type library.
ComImportAttribute is a pseudo-custom attribute that indicates that a type has been defined in a previously published type library. The common language runtime treats these types differently when activating, exporting, coercing, and so on.
Note: |
|---|
| All base COM types that are inherited by a managed object must aggregate the Free Threaded Marshaller (FTM). COM types that do not aggregate the FTM can't be inherited by managed objects. |
The following example demonstrates how to apply the ComImportAttribute to a managed interface declaration. You apply this attribute only when generating an interop assembly manually in source code, and want to simulate the metadata produced by Tlbimp.exe.
Imports System Imports System.Runtime.InteropServices Module MyModule ' If you do not have a type library for an interface ' you can redeclare it using ComImportAttribute. ' This is how the interface would look in an idl file. '[ 'object, 'uuid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26"), 'dual, helpstring("IMyStorage Interface"), 'pointer_default(unique) '] 'interface IMyStorage : IDispatch '{ ' [id(1)] ' HRESULT GetItem([in] BSTR bstrName, [out, retval] IDispatch ** ppItem); ' [id(2)] ' HRESULT GetItems([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT)* pItems); ' [id(3)] ' HRESULT GetItemDescriptions([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT) ** ppItems); ' [id(4), propget] ' HRESULT get_IsEmpty([out, retval] BOOL * pfEmpty); '}; ' This is the managed declaration. <ComImport(), Guid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26")> _ Public Interface IMyStorage <DispId(1)> _ Function GetItem(<InAttribute(), MarshalAs(UnmanagedType.BStr)> ByVal bstrName As String) _ As <MarshalAs(UnmanagedType.Interface)> Object <DispId(2)> _ Function GetItems(<InAttribute(), MarshalAs(UnmanagedType.BStr)> ByVal bstrLocation As String, _ <OutAttribute(), MarshalAs(UnmanagedType.SafeArray)> ByVal Items() As Object) <DispId(3)> _ Function GetItemDescriptions(<InAttribute()> ByVal bstrLocation As String, _ <InAttribute(), OutAttribute(), MarshalAs(UnmanagedType.SafeArray)> ByRef varDescriptions() As Object) <DispId(4)> _ ReadOnly Property IsEmpty(<MarshalAs(UnmanagedType.VariantBool)> ByVal bEmpty As Boolean) End Interface End Module
import System.*;
import System.Runtime.InteropServices.*;
// If you do not have a type library for an interface
// you can redeclare it using ComImportAttribute.
// This is how the interface would look in an idl file.
// [
// object,
// uuid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26"),
// dual, helpstring("IMyStorage Interface"),
// pointer_default(unique)
// ]
// interface IMyStorage : IDispatch
// {
// [id(1)]
// HRESULT GetItem([in] BSTR bstrName, [out, retval] IDispatch ** ppItem);
// [id(2)]
// HRESULT GetItems([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT)* pItems);
// [id(3)]
// HRESULT GetItemDescriptions([in] BSTR bstrLocation, [out] SAFEARRAY(VARIANT) ** ppItems);
// [id(4), propget]
// HRESULT get_IsEmpty([out, retval] BOOL * pfEmpty);
// };
// This is the managed declaration.
/** @attribute ComImport()
*/
/** @attribute Guid("73EB4AF8-BE9C-4b49-B3A4-24F4FF657B26")
*/
public interface IMyStorage
{
/** @attribute DispId(1)
*/
Object GetItem(
/** @attribute In()
@attribute MarshalAs(UnmanagedType.BStr)
*/String bstrName);
/** @attribute DispId(2)
*/
void GetItems(
/** @attribute In()
@attribute MarshalAs(UnmanagedType.BStr)
*/String bstrLocation,
/** @attribute Out()
@attribute MarshalAs(UnmanagedType.SafeArray,
SafeArraySubType = VarEnum.VT_VARIANT)
*/Object Items[]);
/** @attribute DispId(3)
*/
void GetItemDescriptions(
/** @attribute In()
*/String bstrLocation,
/** @attribute In()
@attribute Out()
@attribute MarshalAs(UnmanagedType.SafeArray)
*/Object varDescriptions[]);
/** @attribute DispId(4)
*/
/** @return MarshalAs(UnmanagedType.VariantBool)
*/
boolean get_IsEmpty();
} //IMyStorage
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: