Click here to Skip to main content
Click here to Skip to main content

Understanding Classic COM Interoperability With .NET Applications

By , 24 Jul 2001
 
cominteropwithdotnetdemo.zip
COMInteropWithDOTNET
COMInteropWithDOTNETDemo
AirlineClient.exe
AirlineInformation.dll
AirlineMetadata.dll
AirlineNotify.exe
AirlineNotifyClient.exe
AirlineNotifyMetadata.dll
ControlTower.exe
DelegateTest.exe
Directional.dll
FlightClient.exe
HelloDotNet.dll
HelloWorldDelegate.exe
IceCreamMenuClient.exe
IceCreamMenuMetadata.dll
IceCreamParlor.dll
MyFlyer.dll
MyFlyerMetadata.dll
NETThreadDemoClient.exe
SevenDwarfs.exe
Temperature.dll
VBIceCreamProject.dll
VBTestClient.exe
VBWeatherStation.exe
WeatherNotify.dll
cominteropwithdotnetsource.zip
COMInteropWithDOTNETSource
ClassicCOM
AirlineInformation
AirlineInfo.rgs
AirlineInformation.aps
AirlineInformation.def
AirlineInformation.dsp
AirlineInformation.dsw
AirlineInformation.opt
AirlineInformation.tlb
AirlineInformationps.def
AirlineInformationps.mk
AirlineNotify
AirlineArrivalPager.rgs
AirlineNotify.aps
AirlineNotify.dsp
AirlineNotify.dsw
AirlineNotify.opt
AirlineNotify.rgs
AirlineNotify.suo
AirlineNotify.tlb
AirlineNotifyps.def
AirlineNotifyps.mk
IceCreamParlor
IceCreamMenu.rgs
IceCreamParlor.aps
IceCreamParlor.def
IceCreamParlor.dsp
IceCreamParlor.dsw
IceCreamParlor.opt
IceCreamParlor.tlb
IceCreamParlorps.def
IceCreamParlorps.mk
MyFlyer
Flyer.rgs
MyFlyer.aps
MyFlyer.def
MyFlyer.dsp
MyFlyer.dsw
MyFlyer.opt
MyFlyer.tlb
MyFlyerps.def
MyFlyerps.mk
NETThreadDemoClient
HelloDotNet.tlb
NETThreadDemoClient.dsp
NETThreadDemoClient.dsw
NETThreadDemoClient.opt
VBAirlineControlTower
ControlTower.vbp
ControlTower.vbw
MainForm.frm
VBIceCreamParlor
IceCreamMenu.cls
stdole.dll
VBIceCreamProject.exp
VBIceCreamProject.lib
VBIceCreamProject.vbp
VBIceCreamProject.vbw
VBTestClient
FormMain.frm
VBTestClient.vbp
VBTestClient.vbw
VBWeatherStation
VBWeatherStation.vbp
VBWeatherStation.vbw
VBWeatherStationForm.frm
DOTNET-CSharpFiles
AirlineInformation.tlb
AirlineNotify.tlb
IceCreamParlor.tlb
MyFlyer.tlb
MyKeyPair.snk
cominterop_demo.zip
ComInteropDemo
AirlineCSharpClient
AirlineInformation
AirlineInfo.rgs
AirlineInformation.aps
AirlineInformation.def
AirlineInformation.dsp
AirlineInformation.dsw
AirlineInformation.opt
AirlineInformation.tlb
AirlineInformationps.def
AirlineInformationps.mk
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 use 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

About the Author

Aravind C
India India
Member
No Biography provided

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