Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++

Understanding Classic COM Interoperability With .NET Applications

Rate me:
Please Sign up or sign in to vote.
4.96/5 (207 votes)
24 Jul 2001 1.2M   11.9K   715  
Discusses how existing COM components can be used from managed code.
using System;
using System.Runtime.InteropServices;
using System.Reflection;
using AIRLINEINFORMATIONLib;


public class CAirlineClient
{

	public static void Main()
	{

		///////////////////////////////////////////////
		/// EARLY BINDING EXAMPLE
		///////////////////////////////////////////////

		String strAirline = "Air Scooby IC 5678";
		String strFoodJunkieAirline = "Air Jughead TX 1234";
		try
	   	{
			AirlineInfo objAirlineInfo;
			
			// Create a new AirlineInfo Object
			objAirlineInfo = new AirlineInfo();	

			// Display the output after calling the GetAirileTiming method
			System.Console.WriteLine("Details for Airline {0} --> {1}",
				strAirline,objAirlineInfo.GetAirlineTiming(strAirline)); 

			// System.Console.WriteLine("Details for Airline {0} --> {1}",
			// strFoodJunkieAirline,objAirlineInfo.GetAirlineTiming(strFoodJunkieAirline)); 

	        }//try 
		catch(COMException e)
		{
			System.Console.WriteLine("Oops- We encountered an error for Airline {0}. The Error message is : {1}. The  Error code is {2}",
				strFoodJunkieAirline ,e.Message,e.ErrorCode);			  	

		}// catch 

		///////////////////////////////////////////////
		/// QUERY INTERFACE/ RT type Checking
		///////////////////////////////////////////////

	
		try	
		{
			AirlineInfo objAirlineInfo;
			IAirportFacilitiesInfo objFacilitiesInfo;
			
			// Create a new AirlineInfo object	
			objAirlineInfo = new AirlineInfo();
		
			// Invoke the GetAirlineTiming method	
			String strDetails = objAirlineInfo.GetAirlineTiming(strAirline);	

			// QI for the IAirportFacilitiesInfo interface		
			objFacilitiesInfo = (IAirportFacilitiesInfo)objAirlineInfo;
			
			//Invoke a method on the IAirportFacilitiesInfo interface
			System.Console.WriteLine("{0}",objFacilitiesInfo.GetInternetCafeLocations());

        	} //try
		catch(InvalidCastException eCast)
		{	
			System.Console.WriteLine("We got an InvalidCast Exception - Message is {0}",eCast.Message);   

		}//catch

		///////////////////////////////////////////////
		/// LATE BINDING
		///////////////////////////////////////////////


		try
		{

			object objAirlineLateBound;
			Type objTypeAirline;
		      	object[] arrayInputParams= { "Air Scooby IC 5678" };
			objTypeAirline = Type.GetTypeFromProgID
                               ("AirlineInformation.AirlineInfo");
			objAirlineLateBound = Activator.CreateInstance(objTypeAirline);
			String str = (String)objTypeAirline.InvokeMember("GetAirlineTiming",
									BindingFlags.Default |	
					                                BindingFlags.InvokeMethod,
					                                null,
									objAirlineLateBound,
									arrayInputParams);

			System.Console.WriteLine("{0}",str);

			String strTime = (String)objTypeAirline.InvokeMember("LocalTimeAtOrlando", 
								BindingFlags.Default | 										BindingFlags.GetProperty, 
								null, 
								objAirlineLateBound,
								new object [] {});


		      Console.WriteLine ("Hi there !. The Local Time at Orlando,Florida is: {0}", strTime);
        

		}//try late binding
		catch(COMException e)
		{
	
			System.Console.WriteLine("Oops- We encountered an error for Airline {0}. The Error message is : 					{1}. The Error code is {2}",strFoodJunkieAirline ,e.Message,e.ErrorCode);			  	

	       }//catch


	}// Main

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions