This documentation is archived and is not being maintained.

Using the Component from an Unmanaged C++ Client

Visual Studio .NET 2003

The following code shows a simple unmanaged C++ client that uses the example COM component CUSTOM_DATE. For example, you could put this main code in an unmanaged Win32 console application called ATLTransmitAsClient:

// ATLTransmitAsClient.cpp : Defines the entry point for 
// the console application.

#include "stdafx.h"
#include "..\ATLTransmitAs\ATLTransmitAs.h"
#include <iostream>
#include <comdef.h>
_COM_SMARTPTR_TYPEDEF(ICustomDate, IID_ICustomDate);

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
   ::CoInitialize(NULL);
   {
      ICustomDatePtr pCD;
      pCD.CreateInstance(CLSID_CustomDate, NULL, CLSCTX_INPROC_SERVER);
      if ( pCD != NULL )
      {
         CUSTOM_DATE *pDate;
         short n;

         pCD->GetDate(&pDate);
         pDate->day++;
         pCD->PutDate(pDate);
         CoTaskMemFree(pDate);

         pCD->GetDates(&n,&pDate);
         for(int i=0;i<n;i++)
         {
            cout << pDate[i].day << '-' 
                 << pDate[i].month << '-' 
                 << pDate[i].year << endl;
         }
         CoTaskMemFree(pDate);
      }
   }
   ::CoUninitialize();
   return 0;
}

Compare the unmanaged code with the managed code in the next step.

Go to the next step | Go to the previous step

See Also

Managed Extensions for C++ and COM Interoperability Tutorial

Show: