Writing COM interface to COM add-in

Add-in Express™ Support Service
That's what is more important than anything else

Writing COM interface to COM add-in
 
mani g




Posts: 66
Joined: 2009-06-09
Hello,

I have an ADX Outlook COM addin, and would like to create a COM interface to this addin for outside applications to use.

That is, I'd like to do what's discussed here: http://msdn.microsoft.com/en-us/library/aa645738%28VS.71%29.aspx

I was planning on creating a new module to handle all the COM server stuff, and generate a separate DLL and TLB file for that. However, I have a feeling this is not the ideal solution, since the add-in is already providing a COM interface.

So, any suggestions regarding the cleanest way to accomplish this?

Thank you,
Mani
Posted 14 Dec, 2009 15:50:21 Top
Andrei Smolin


Add-in Express team


Posts: 14094
Joined: 2006-05-11
Hello Mani,

You can access your COM add-in from an external aplication via the following code path:

OutlookApp.COMAddins.Item(strMyComAddinProgId).Object.MyPublicPropertyOrMethod.

1. strMyComAddinProgId - see the ProgId attribute of your add-in module.
2. MyPublicPropertyOrMethod is called via late binding (see System.Type.InvokeMember in MSDN)

Recently, I found that PropertySet doesn't work when used in the second parameter of InvokeMember (see below); there is no exception, the code just doesn't do anything. The reason is unknown at the moment. The workaround is to call a method with a parameter insted.

I've tested the following code:

MyAddin356:
===
Private _value As String = ""
Private Const notSet As String = "not set"

Public Property MyProperty() As String
Get
If String.IsNullOrEmpty(_value) Then
Return notSet
Else
Return _value
End If
End Get
Set(ByVal value As String)
System.Windows.Forms.MessageBox.Show("MyProperty Set has been called", "MyAddin356")
_value = value
End Set
End Property

Public Sub MySub()
System.Windows.Forms.MessageBox.Show("MySub has been called", "MyAddin356")
End Sub

Public Sub MySub2(ByVal aParameter As String)
_value = aParameter
System.Windows.Forms.MessageBox.Show("MySub2 has been called; _value is '" + _value + "'", "MyAddin356")
End Sub

Public Function MyFunction(ByVal aParameter As String) As String
_value = aParameter
Return _value
End Function
===

MyAddin357:
===
Private myOtherAddin As String = "MyAddin356.AddinModule"

Private Function GetAddinObject(ByVal progID As String) As Object
Dim Addin As Office.COMAddIn = OutlookApp.COMAddIns.Item(progID)
If Addin IsNot Nothing Then
Dim AddinObj As Object = Addin.Object
If Addin.Connect Then
Return AddinObj
End If
End If
Return Nothing
End Function

Private Sub btnPropertyGet_Click(ByVal sender As System.Object) Handles btnPropertyGet.Click
Dim AddinObj As Object = GetAddinObject(myOtherAddin)
If AddinObj IsNot Nothing Then
Try
Dim returnValue As Object = _
AddinObj.GetType().InvokeMember("MyProperty", Reflection.BindingFlags.GetProperty, Nothing, AddinObj, Nothing)
System.Windows.Forms.MessageBox.Show("MyProperty returned " + returnValue.ToString(), "MyAddin357")
Catch ex As Exception
System.Windows.Forms.MessageBox.Show("btnPropertyGet_Click. Error:" + ex.Message, "MyAddin357")
End Try
End If
End Sub

Private Sub btnPropertySet_Click(ByVal sender As System.Object) Handles btnPropertySet.Click
Dim AddinObj As Object = GetAddinObject(myOtherAddin)
If AddinObj IsNot Nothing Then
Dim prop As PropertyInfo = AddinObj.GetType().GetProperty("MyProperty", BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.GetField Or BindingFlags.Static)
Try
Dim value As String = AdxCommandBarEdit1.Text
Dim result As Object = _
AddinObj.GetType().InvokeMember("MyProperty", BindingFlags.DeclaredOnly Or BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.SetProperty Or BindingFlags.Static, Nothing, AddinObj, New Object() {"dsdfsd"})
Catch ex As Exception
System.Windows.Forms.MessageBox.Show("btnPropertySet_Click. Error:" + ex.Message, "MyAddin357")
End Try
End If
End Sub

Private Sub btnCallSub_Click(ByVal sender As System.Object) Handles btnCallSub.Click
Dim AddinObj As Object = GetAddinObject(myOtherAddin)
If AddinObj IsNot Nothing Then
Try
AddinObj.GetType().InvokeMember("MySub", Reflection.BindingFlags.InvokeMethod, Nothing, AddinObj, Nothing)
Catch ex As Exception
System.Windows.Forms.MessageBox.Show("btnCallSub_Click. Error:" + ex.Message, "MyAddin357")
End Try
End If
End Sub

Private Sub btnCallSubWithParameter_Click(ByVal sender As System.Object) Handles btnCallSubWithParameter.Click
Dim AddinObj As Object = GetAddinObject(myOtherAddin)
If AddinObj IsNot Nothing Then
Try
Dim parameter As String = AdxCommandBarEdit1.Text
AddinObj.GetType().InvokeMember("MySub2", Reflection.BindingFlags.InvokeMethod, Nothing, AddinObj, New Object() {parameter})
Catch ex As Exception
System.Windows.Forms.MessageBox.Show("btnCallSubWithParameter_Click. Error:" + ex.Message, "MyAddin357")
End Try
End If
End Sub

Private Sub btnCallFunction_Click(ByVal sender As System.Object) Handles btnCallFunction.Click
Dim AddinObj As Object = GetAddinObject(myOtherAddin)
If AddinObj IsNot Nothing Then
Try
Dim parameter As String = AdxCommandBarEdit1.Text
Dim returnValue As Object = _
AddinObj.GetType().InvokeMember("MyFunction", Reflection.BindingFlags.InvokeMethod, Nothing, AddinObj, New Object() {parameter})
System.Windows.Forms.MessageBox.Show("MyFunction returned " + returnValue.ToString(), "MyAddin357")
Catch ex As Exception
System.Windows.Forms.MessageBox.Show("btnCallFunction_Click. Error:" + ex.Message, "MyAddin357")
End Try
End If

End Sub
===

Hope this helps.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 14 Dec, 2009 16:24:14 Top
mani g




Posts: 66
Joined: 2009-06-09
This helps immensely. I deeply appreciate this, thank you so much!

Mani
Posted 14 Dec, 2009 16:54:03 Top