C# Source Code: Interop: Using a CCW to consume .NET component events from Visual Basic (using WithEvents)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Interop: Using a CCW to consume .NET component events from Visual Basic (using WithEvents)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, September 22, 2005
Hits:
2016
Category:
COM/Interop
Article:
The .NET Framework provides a delegate-based event system to connect an event sender (source) to an event receiver (sink). When the sink is a COM client, the source must include additional elements to simulate connection points. With these modifications, a COM client can register its event sink interface in the traditional way by calling the IConnectionPoint::Advise method. (Visual Basic hides connection point details, so you do not have to call these methods directly.) Below is an example C# class that defines and raises events to be consumed by a VB client. Example VB client code is listed at the bottom of this post. [C#] using System; using System.Runtime.InteropServices; [assembly: Guid("900310DE-C376-4e0a-8BC9-1C8CC7B284D0")] namespace VBusers.Interop.Examples { [ComVisible(false)] public delegate void MessageHandler(string message); [ComVisible(false)] public delegate void ConnectionHandler(); #region IMessageEvents Interface // Define an event sink interface to be // implemented in the COM client. Note, the COM client // must completely impliment all the methods in this interface. // Can use guidgen.exe to generator the GUID. [GuidAttribute("04F46C72-AECC-410f-A9C2-0D27598D093F") ] [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)] public interface IMessageEvents { ///
/// Called when a message is received. ///
///
The message. [DispId(1)] void MessageReceived(string message); ///
/// Called when a connection is received. ///
[DispId(2)] void Connected(); } #endregion #region InstantMessageDemo Class // Note, do not use ClassInterfaceType.AutoDual for production // applications. Instead use ClassInterfaceType.None and impliment an // interface containing to expose the object to COM clients, or just late // bind using ClassInterfaceType.AutoDispatch. [ComSourceInterfaces(typeof(IMessageEvents))] // Connect the event sink interface to a class [ClassInterface(ClassInterfaceType.AutoDual)] [GuidAttribute("99707BE3-E6B1-459b-BEA8-7F96A6CBA3D7")] public class InstantMessageDemo { public event MessageHandler MessageReceived; public event ConnectionHandler Connected; ///
/// Sends a message using the
MessageReceived
event. ///
///
The message to send. public void SendMessage(string message) { // Will never return null from COM implimentation, even if the event has not been defined if ( MessageReceived != null ) { try { MessageReceived( message ); } catch{} // Note, COM side will throw exception if this event is not defined } } ///
/// Opens a connection using the
Connected
event. ///
public void OpenConnection() { // Will never return null from COM implimentation, even if the event has not been defined if ( Connected != null ) { try { Connected(); } catch{} // Note, COM side will throw exception if this event is not defined } } } #endregion } [Visual Basic] ' This Visual Basic 6.0 client creates an instance of the InstantMessageDemo class ' and implements the event sink interface. The WithEvents directive ' registers the sink interface pointer with the source. Note, that you MUST ' subscribe to ALL the events published by the .NET class to prevent an ' exception being thrown on the .NET side. If you don't wish to consume an event ' simple put a comment in the code (to stop the compiler removing the stub). [Class Code] Public WithEvents oMessageDemo As InstantMessageDemo Private Sub Class_Initialize() Set oMessageDemo = New InstantMessageDemo End Sub Private Sub oMessageDemo_MessageReceived(ByVal message) MsgBox "Received message " & message End Sub Private Sub oMessageDemo_Connected() MsgBox "Connected" End Sub
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet