Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Example COM Component

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;
} CUSTOM_DATE;
[
   object,
   uuid("AE23C15E-920D-4ffa-B662-7E77E231E951"),
   helpstring("ICustomDate Interface"),
   pointer_default(unique)
]
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);
};
[
   uuid("58DD1FFC-9754-4b87-88DF-4D58DCF59B66"),
   version(1.0),
   helpstring("ATLTransmitAs 1.0 Type Library")
]
library ATLTransmitAsLib
{
   importlib("stdole2.tlb");
   [
      uuid("77F81807-FF09-4080-9726-3944E8682CD3"),
      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

Show:
© 2015 Microsoft