Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Understanding Classic COM Interoperability With .NET Applications

, 24 Jul 2001
Discusses how existing COM components can be used from managed code.
cominteropwithdotnetdemo.zip
COMInteropWithDOTNET
COMInteropWithDOTNETDemo
AirlineNotifyMetadata.dll
AirlineMetadata.dll
SevenDwarfs.exe
IceCreamMenuClient.exe
HelloWorldDelegate.exe
FlightClient.exe
DelegateTest.exe
AirlineNotifyClient.exe
WeatherNotify.dll
AirlineInformation.dll
AirlineNotify.exe
IceCreamParlor.dll
MyFlyer.dll
NETThreadDemoClient.exe
ControlTower.exe
VBIceCreamProject.dll
VBTestClient.exe
VBWeatherStation.exe
AirlineClient.exe
Temperature.dll
MyFlyerMetadata.dll
IceCreamMenuMetadata.dll
HelloDotNet.dll
Directional.dll
cominteropwithdotnetsource.zip
COMInteropWithDOTNETSource
DOTNET-CSharpFiles
MyKeyPair.snk
AirlineInformation.tlb
IceCreamParlor.tlb
AirlineNotify.tlb
MyFlyer.tlb
ClassicCOM
AirlineNotify
AirlineNotifyps.def
AirlineNotifyps.mk
AirlineNotify.rgs
AirlineNotify.dsw
AirlineArrivalPager.rgs
AirlineNotify.aps
AirlineNotify.dsp
AirlineNotify.suo
AirlineNotify.tlb
AirlineNotify.opt
AirlineInformation
AirlineInfo.rgs
AirlineInformation.aps
AirlineInformation.def
AirlineInformationps.def
AirlineInformationps.mk
AirlineInformation.dsp
AirlineInformation.dsw
AirlineInformation.tlb
AirlineInformation.opt
VBIceCreamParlor
IceCreamMenu.cls
stdole.dll
VBIceCreamProject.vbw
VBIceCreamProject.lib
VBIceCreamProject.exp
VBIceCreamProject.vbp
VBAirlineControlTower
MainForm.frm
ControlTower.vbw
ControlTower.vbp
IceCreamParlor
IceCreamMenu.rgs
IceCreamParlor.def
IceCreamParlor.aps
IceCreamParlorps.def
IceCreamParlorps.mk
IceCreamParlor.dsp
IceCreamParlor.dsw
IceCreamParlor.tlb
IceCreamParlor.opt
MyFlyer
MyFlyer.dsp
MyFlyer.def
MyFlyerps.def
MyFlyerps.mk
MyFlyer.dsw
Flyer.rgs
MyFlyer.aps
MyFlyer.tlb
MyFlyer.opt
VBTestClient
VBTestClient.vbp
FormMain.frm
VBTestClient.vbw
VBWeatherStation
VBWeatherStation.vbw
VBWeatherStationForm.frm
VBWeatherStation.vbp
NETThreadDemoClient
NETThreadDemoClient.opt
NETThreadDemoClient.dsp
NETThreadDemoClient.dsw
HelloDotNet.tlb
cominterop_demo.zip
ComInteropDemo
AirlineCSharpClient
AirlineInformation
AirlineInformation.opt
AirlineInfo.rgs
AirlineInformation.aps
AirlineInformation.tlb
AirlineInformation.def
AirlineInformationps.def
AirlineInformationps.mk
AirlineInformation.dsp
AirlineInformation.dsw
using System;
using System.Runtime.InteropServices;
using System.Reflection;
using AirlineMetadata;


interface IJunkInterface 
{

}

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)); 
			
			// This will throw us the 
			System.Console.WriteLine("Details for Airline {0} --> {1}",
				strFoodJunkieAirline,objAirlineInfo.GetAirlineTiming(strFoodJunkieAirline)); 

	    	}
		catch(COMException ex1COM)
		{
			System.Console.WriteLine("Oops an error occured !. Error Code is : {0}. Error message is : {1}",
					ex1COM.ErrorCode,ex1COM.Message);			  	
		}
		catch(Exception ex1)
		{
			System.Console.WriteLine("Oops !. An exception occured : {0} ",
				ex1.Message);			  	
		}

		
		/*
		 * QueryInterface Example
		 */
		try	
		{
			AirlineInfo objAirlineInfo = null;
			IAirportFacilitiesInfo objFacilitiesInfo = null;
			
			// Create a new AirlineInfo object	
			objAirlineInfo = new AirlineInfo();
		
			// Invoke the GetAirlineTiming method	
			String strDetails = objAirlineInfo.GetAirlineTiming(strAirline);	
			
				
    			// Check to see if the AirlineInfo object supports the 
			// IAirportFacilitiesInfo interface using C#'s 'is' operator

			// The class is always going to be of that type
			/*
			if(objAirlineInfo is IAirportFacilitiesInfo) 
			{
			*/			
				// Perform a cast to get the QI done
				objFacilitiesInfo = (IAirportFacilitiesInfo)objAirlineInfo;

        				// There's always more than one way to skin a cat
				// You could even perform the cast using C#'s 'as' operator
        				objFacilitiesInfo = objAirlineInfo as IAirportFacilitiesInfo;

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

			/*
			}
			*/
			
			// Let's check against an arbitrary interface type
			if(objAirlineInfo is IJunkInterface) 
			{
				System.Console.WriteLine("We should never get here ");
			}
			else
			{
				System.Console.WriteLine("I'm sorry I don't implement the IJunkInterface interface ");
			}

			// And now let's ask for some trouble and have the
			// interop throw us an invalid cast exception.
			IJunkInterface objJunk = null;
			objJunk = (IJunkInterface)objAirlineInfo;


		}/* end try */
		catch(InvalidCastException eCast)
		{	
			System.Console.WriteLine("Here comes trouble ... Error Message : {0}",eCast.Message);   

		}/* end catch */

		/*
		 * Late Binding Example
		 */
		try
		{

			object objAirlineLateBound;
			Type objTypeAirline;

			// Create an object array containing the input params
		    	object[] arrayInputParams= { "Air Scooby IC 5678" };
			
			//Get the type information from the progid
			objTypeAirline = Type.GetTypeFromProgID("AirlineInformation.AirlineInfo");
			
			// Here's how you use the COM CLSID to get the associated .NET System.Type
			// objTypeAirline = 	Type.GetTypeFromCLSID(new Guid("{F29EAEEE-D445-403B-B89E-C8C502B115D8}"));

			// Create an instance of the object
			objAirlineLateBound = Activator.CreateInstance(objTypeAirline);

			// Invoke the 'GetAirlineTiming' method
			String str = (String)objTypeAirline.InvokeMember("GetAirlineTiming",
					BindingFlags.Default | BindingFlags.InvokeMethod,
					null, objAirlineLateBound,
					  arrayInputParams);

			System.Console.WriteLine("Late Bound Call - Air Scooby Arrives at {0}",str);

			// Get the value of the 'LocalTimeAtOrlando' property
			String strTime = (String)objTypeAirline.InvokeMember("LocalTimeAtOrlando", 
					BindingFlags.Default | BindingFlags.GetProperty, 
					null, objAirlineLateBound,
					new object [] {});

		             Console.WriteLine ("Late Bound Call - Local Time at Orlando,Florida is: {0}", strTime);
        

		}/* end try */
		catch(COMException e)
		{
			System.Console.WriteLine("Error Code : {0} , Error Message : {1}",e.ErrorCode,e.Message);			  	

	              }/* end catch */


        }/* end  Main */

}/* end class */

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

Share

About the Author

Aravind C

India India
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 25 Jul 2001
Article Copyright 2001 by Aravind C
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid