Click here to Skip to main content
15,896,154 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.3M   11.9K   715  
Discusses how existing COM components can be used from managed code.
using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Diagnostics;

// Outgoing Event Interface which the Sink implements. We'll
// use a dispinterface here so that it remains friendly to
// scripting clients 
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITornadoWatchEvents 
{
    void OnTornadoWarning(int nWindSpeed);

}/* end interface ITornadoWatchEvents */

// Explicit incoming interface containing the methods
// being exposed to COM aware clients
public interface IWeatherNotify {

    int WindSpeed { get; set; }

}/* end interface IWeatherNotify */

// Delegate representing the OnTornadoWarning method of the outgoing event interface
public delegate void TornadoWarningDelegate(int nWindSpeed);


[ 
  ComSourceInterfaces("ITornadoWatchEvents"),
  ClassInterface(ClassInterfaceType.None)
]
public class WeatherNotify : IWeatherNotify
{
	// Indicates Windspeed in Miles Per Hour 
	private int m_nWindSpeed = 20;

	// Define an event associated with the TornadoWarningDelegate 
	public event TornadoWarningDelegate OnTornadoWarning;
	
	// Constructor
	public WeatherNotify() {
	}

	public int WindSpeed
	{
	      get {

	         // Return the current Wind speed 
                       return m_nWindSpeed;
                    }
	
	      set {

	          // Set the WindSpeed to the new value
                        m_nWindSpeed = value;
			
	          // Check if the Wind Speed warrants an event notification 
                        if(value >= 300) {

 		  try  {

		       // Check if the delegate instance for the event exists
		       if(null != OnTornadoWarning) {
				
		             // Twister on the loose. Run for cover !!!. 
		             // Fire the event to all the unmanaged sink handlers 
		             // that have registered for this event.
		            OnTornadoWarning(m_nWindSpeed);
			
		            OnTornadoWarning.Target.GetType().InvokeMember( "OnTornadoWarning",  BindingFlags.InvokeMethod, 
								      null,  OnTornadoWarning.Target, 
								      new object [] {m_nWindSpeed} );


		          } /* end if */

                               }/* end try */
		   catch(Exception ex) {

		       Trace.WriteLine(ex.Message);
                               }/* end catch */
				
	            }/* end if */

	      }/* end set */

         }/* End WindSpeed Property Accessors */

}/* end class WeatherNotify */

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