Share via


Using COM Interoperability in Visual C# .NET

Using COM Interoperability in Visual C# .NET

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

CDO for Exchange 2000 Server (CDOEX), CDO for Exchange Management (CDOEXM), and CDO for Workflow (CDOWF) provide the fundamental interfaces and Component Object Model (COM) classes that are used to manage most types of items in the Exchange store. The Collaboration Data Objects (CDO) classes are an example of an unmanaged object model and are run outside the control of the common language runtime.

COM components and Microsoft® .NET-connected components are not inherently compatible because their internal architecture is different. Therefore, a COM component cannot be instantiated directly in an application built on the .NET Framework. To get around this issue, the .NET Framework promotes interaction and interoperability with COM components and other types of unmanaged code through the use of wrapper classes and proxy objects. These wrapper classes and proxy objects expose the functionality of the COM components to the .NET Framework.

The Microsoft Visual Studio® .NET Integrated Development Environment (IDE) performs most of the work in COM interoperability behind the scenes when a reference to a COM dynamic-link library (DLL) is created. This generates a .NET proxy component for the COM DLL and places a copy of the COM DLL in the Visual Studio .NET project directory. This proxy component may be referenced from the .NET application and forwards calls from the .NET client through COM services to the COM DLL that it wraps. The proxy component also takes any responses and COM errors from the COM DLL, maps them to data types and exceptions supported by the .NET Framework, and passes them back to the .NET application.

To add a reference to a COM object

  1. Open a new or existing Microsoft Visual C#® project in Visual Studio .NET.

  2. Click the Project menu and select Add Reference.

  3. In the Add Reference window, click the COM tab.

  4. Scroll down the list of components and select the one you want to reference, such as Microsoft CDO For Exchange 2000 Library. Click Select. After the component name appears in the Selected Components window, click OK.

    Note  The COM component must have been registered previously on the server for this to succeed.

    The component will now be listed in the References section of the Solution Explorer window in the Visual Studio .NET IDE.

To use the reference to the COM component in your project, you must first specify the name of the referenced COM object in a using statement at the top of the source file. The referenced COM object may then be used in your application, as in the following example using the IPerson Interface.

using System;

// Microsoft CDO for Exchange 2000 Library
using CDO;

// Microsoft ActiveX Data Objects 2.5 Library
using ADODB;

namespace ExchangeSDK.Snippets.CSharp
{
   public class TestClass
   {
      [STAThread]
      static void Main(string[] args)
      {
          // Variables.
          CDO.Person iPerson;
          string strEmailAddress = "MAILTO:user@fourthcoffee.com";

          try
          {
             // Initialize the CDO.Person object.
             iPerson = new CDO.PersonClass();

             iPerson.DataSource.Open(strEmailAddress,
                                     null,
                                     ADODB.ConnectModeEnum.adModeRead,
                                     ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
                                     ADODB.RecordOpenOptionsEnum.adOpenSource,
                                     String.Empty,
                                     String.Empty
                                    );
           }
           catch(Exception ex)
           {
                Console.WriteLine(ex.Message);
           }
      }
   }
}

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.