This documentation is archived and is not being maintained.

Example COM Component

Visual Studio .NET 2003

This section shows how to construct a non-trivial COM component. The component could be part of legacy code that you want to expose to .NET Framework clients.

Create an ATL project called ATLTransmitAs. Add an IDL file named ATLTransmitAs.idl and paste the following code into it.

// ATLTransmitAs.idl : IDL source for ATLTransmitAs

// This file will be processed by the MIDL tool to produce
// the type library (ATLTransmitAs.tlb) and marshalling code.

import "oaidl.idl";
import "ocidl.idl";

typedef long DATEXMIT;
typedef [transmit_as(DATEXMIT)] struct CUSTOM_DATE
   short day;
   short month;
   short year;
   helpstring("ICustomDate Interface"),
interface ICustomDate : IUnknown
   [helpstring("method GetDate")] 
   HRESULT GetDate([out,ref] CUSTOM_DATE ** pDate);
   [helpstring("method PutDate")] 
   HRESULT PutDate([in,ref] CUSTOM_DATE * pDate);
   [helpstring("method GetDates")] 
   HRESULT GetDates([out]short *n,[out,size_is(,*n)]CUSTOM_DATE **dates);
   helpstring("ATLTransmitAs 1.0 Type Library")
library ATLTransmitAsLib
      helpstring("CustomDate Class")
   coclass CustomDate
      [default] interface ICustomDate;

The code demonstrates the following concepts:

  • The CUSTOM_DATE structure is marshaled as long because of the transmit_as attribute.
  • The GetDate method allocates a CUSTOM_DATE structure and returns its pointer in a caller-provided location.
  • The GetDates method returns a conformant, varying array, in which the component allocates the array and informs the caller of the allocated size.

Go to the next step | Go to the previous step

See Also

Managed Extensions for C++ and COM Interoperability Tutorial