M
Michael Tissington
I'm confused by documentation and examples on using Delegate to create
Events for use with COM
In some situation I see a parameter list of (sender as Object, e as
EventArgs) and other times I see no parameters, or a list of parameters that
are event dependent.
Below is my .NET code to create a class which raises some events.
From C++ (using MFC 6.0) I create an instance of the object and
IConnectionPointContainer which seems to work.
When the .NET object raises the event I get an exception of type
System.NullReferenceException: Object reference not set to an instance of an
object.
at MapiForm._FormEvents.Shutdown()
at MapiForm.MAPIForm.frm_ShutDownForm() in D:\My
Documents\SQLView\Forms\MAPIForm\MAPIForm.vb:line 63
at MapiForm.OutlookForm.txtShutdown_Click(Object sender, EventArgs e) in
D:\My Documents\SQLView\Forms\MAPIForm\OutlookForm.vb:line 113
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
*******************
Any ideas how I need to setup events so that I can call them from MFC 6.0
???
Thanks!
******************
----------- snip -------------
Imports System
Imports System.Runtime.InteropServices
<ComVisible(False)> Public Delegate Sub NewMessageEventHandler()
<ComVisible(False)> Public Delegate Sub ShutdownEventHandler()
<ComVisible(False)> Public Delegate Sub PrintEventHandler()
<ComVisible(False)> Public Delegate Sub SavedEventHandler()
<GuidAttribute("93D44286-4BA4-4cc7-8971-0C7831EA0354"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)> _
Public Interface _Form
<DispId(&H1001)> Sub ShowForm(ByVal sCN As String, ByVal sGUID As
String)
<DispId(&H1002)> Sub ShutDownForm(ByVal SaveOptions As UInt32)
<DispId(&H1003)> Sub DoVerb(ByVal Verb As Int32)
End Interface
<GuidAttribute("06670E52-A3C2-480b-8D7A-7CE3DD1D03A3"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)> _
Public Interface _FormEvents
<DispId(&HF001)> Sub NewMessage()
<DispId(&HF002)> Sub Shutdown()
<DispId(&HF003)> Sub Print(ByVal PageNumber As UInt32, ByVal Status As
UInt32)
<DispId(&HF004)> Sub Saved()
End Interface
' This is your COM object, you must implement _Form and _FormEvents
' For each object that you create you will need to assign a unique GUID
' and give the class a unique ProgID
<GuidAttribute("9d8a13bf-df52-3084-954b-95e85887111d"),
ProgIdAttribute("SQLView.Form"), ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(GetType(_FormEvents))> _
Public Class MAPIForm
Implements _Form
Public Event NewMessage As NewMessageEventHandler
Public Event ShutDown As ShutdownEventHandler
Public Event Print As PrintEventHandler
Public Event Saved As SavedEventHandler
Private WithEvents frm As New OutlookForm
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
Public Sub ShowForm(ByVal sCN As String, ByVal sGUID As String)
Implements _Form.ShowForm
' This is called by Outlook when a form needs to be displayed
frm.txtCON.Text = sCN
frm.txtGUID.Text = sGUID
frm.Show()
End Sub
Public Sub ShutDownForm(ByVal SaveOptions As UInt32) Implements
_Form.ShutDownForm
' The is called by Outlook when it wants to close the form
End Sub
Public Sub DoVerb(ByVal Verb As Int32) Implements _Form.DoVerb
' The is called by Outlook when a verb (eg Print) needs to be done
' Verbs are defined in the cfg file
End Sub
Private Sub frm_ShutDownForm() Handles frm.ShutDownForm
RaiseEvent ShutDown()
End Sub
End Class
Events for use with COM
In some situation I see a parameter list of (sender as Object, e as
EventArgs) and other times I see no parameters, or a list of parameters that
are event dependent.
Below is my .NET code to create a class which raises some events.
From C++ (using MFC 6.0) I create an instance of the object and
IConnectionPointContainer which seems to work.
When the .NET object raises the event I get an exception of type
System.NullReferenceException: Object reference not set to an instance of an
object.
at MapiForm._FormEvents.Shutdown()
at MapiForm.MAPIForm.frm_ShutDownForm() in D:\My
Documents\SQLView\Forms\MAPIForm\MAPIForm.vb:line 63
at MapiForm.OutlookForm.txtShutdown_Click(Object sender, EventArgs e) in
D:\My Documents\SQLView\Forms\MAPIForm\OutlookForm.vb:line 113
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
*******************
Any ideas how I need to setup events so that I can call them from MFC 6.0
???
Thanks!
******************
----------- snip -------------
Imports System
Imports System.Runtime.InteropServices
<ComVisible(False)> Public Delegate Sub NewMessageEventHandler()
<ComVisible(False)> Public Delegate Sub ShutdownEventHandler()
<ComVisible(False)> Public Delegate Sub PrintEventHandler()
<ComVisible(False)> Public Delegate Sub SavedEventHandler()
<GuidAttribute("93D44286-4BA4-4cc7-8971-0C7831EA0354"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)> _
Public Interface _Form
<DispId(&H1001)> Sub ShowForm(ByVal sCN As String, ByVal sGUID As
String)
<DispId(&H1002)> Sub ShutDownForm(ByVal SaveOptions As UInt32)
<DispId(&H1003)> Sub DoVerb(ByVal Verb As Int32)
End Interface
<GuidAttribute("06670E52-A3C2-480b-8D7A-7CE3DD1D03A3"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)> _
Public Interface _FormEvents
<DispId(&HF001)> Sub NewMessage()
<DispId(&HF002)> Sub Shutdown()
<DispId(&HF003)> Sub Print(ByVal PageNumber As UInt32, ByVal Status As
UInt32)
<DispId(&HF004)> Sub Saved()
End Interface
' This is your COM object, you must implement _Form and _FormEvents
' For each object that you create you will need to assign a unique GUID
' and give the class a unique ProgID
<GuidAttribute("9d8a13bf-df52-3084-954b-95e85887111d"),
ProgIdAttribute("SQLView.Form"), ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(GetType(_FormEvents))> _
Public Class MAPIForm
Implements _Form
Public Event NewMessage As NewMessageEventHandler
Public Event ShutDown As ShutdownEventHandler
Public Event Print As PrintEventHandler
Public Event Saved As SavedEventHandler
Private WithEvents frm As New OutlookForm
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
Public Sub ShowForm(ByVal sCN As String, ByVal sGUID As String)
Implements _Form.ShowForm
' This is called by Outlook when a form needs to be displayed
frm.txtCON.Text = sCN
frm.txtGUID.Text = sGUID
frm.Show()
End Sub
Public Sub ShutDownForm(ByVal SaveOptions As UInt32) Implements
_Form.ShutDownForm
' The is called by Outlook when it wants to close the form
End Sub
Public Sub DoVerb(ByVal Verb As Int32) Implements _Form.DoVerb
' The is called by Outlook when a verb (eg Print) needs to be done
' Verbs are defined in the cfg file
End Sub
Private Sub frm_ShutDownForm() Handles frm.ShutDownForm
RaiseEvent ShutDown()
End Sub
End Class