Dmitry Kostochko

HowTo: Get an IE add-on to interact with a standalone application

In general, you can have IE add-ons interact with standalone applications by using Windows messages and the Shared (static in C#) GetModulesByTypeName method of the AddinExpress.IE.ADXIEModule class. There is just one limitation: both processes (IE with your add-on and standalone application) must be running in the same Integrity Level.

Ok, so here we go now. In the IEModule class of your IE add-on, you need the following declarations:

Private ADDON_TO_MYAPP_MessageId As IntPtr
Private MSG_ADDON_TO_MYAPP As String = "ADDON_TO_MYAPP"
 _
Public Shared Function RegisterWindowMessage( _
    ByVal lpString As [String]) As IntPtr
End Function
 _
Public Shared Function FindWindow( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As IntPtr
End Function
 _
Public Shared Function PostMessage( _
    ByVal hwnd As IntPtr, _
    ByVal wMsg As IntPtr, ByVal wParam As Int32, _
    ByVal lParam As Int32) As IntPtr
End Function

Then the OnConnect, OnDisconnect, DocumentComplete event handlers:

Private Sub IEModule_OnConnect( _
    ByVal sender As System.Object, _
    ByVal threadId As System.Int32) _
    Handles MyBase.OnConnect
 
    Me.ADDON_TO_MYAPP_MessageId = _
        RegisterWindowMessage(MSG_ADDON_TO_MYAPP)
    SendMessageToMyApp(0)
End Sub
 
Private Sub IEModule_OnDisconnect( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.OnDisconnect
 
    SendMessageToMyApp(1000)
End Sub
 
Private Sub IEModule_DocumentComplete( _
    ByVal pDisp As System.Object, _
    ByVal url As System.String) _
    Handles MyBase.DocumentComplete
 
    SendMessageToMyApp(0)
End Sub

And three methods: SendMessageToMyApp is used to notify your standalone application about events, such as opening/closing a tab and web-page loading. The other two are called from the application:

Private Sub SendMessageToMyApp(ByVal delay As Integer)
    Dim hwnd As IntPtr = FindWindow(Nothing, "My Application")
    If hwnd <> IntPtr.Zero Then
        PostMessage(hwnd, Me.ADDON_TO_MYAPP_MessageId, delay, 0)
    End If
End Sub
 
Public Function GetCurrentTitle() As String
    Try
        Dim text As String = Me.HTMLDocument.title
        If [String].IsNullOrEmpty(text) Then
            text = "Blank Page"
        End If
        Return text
    Catch
    End Try
    Return [String].Empty
End Function
 
Public Sub NavigateTo(ByVal url As String)
    Try
        Dim dummy As Object = Type.Missing
        Me.IEApp.Navigate(url, dummy, dummy, dummy, dummy)
    Catch err As Exception
        MessageBox.Show(err.Message, Me.ModuleName, _
            MessageBoxButtons.OK, MessageBoxIcon.[Error])
    End Try
End Sub

And now the key piece of the application. Using the GetModulesByTypeName method, you can get a list of instances of your IEModule classes and use their methods and properties:

Private Sub RefreshItems()
    Dim [module] As ADX_IE_Addon_And_WinApp_VB.IEModule
    comboBox1.BeginUpdate()
    Try
        remoteModules.Clear()
        comboBox1.Items.Clear()
        Dim modules As ArrayList = _
            AddinExpress.IE.ADXIEModule.GetModulesByTypeName( _
                "ADX_IE_Addon_And_WinApp_VB.IEModule")
        If modules.Count > 0 Then
            For i As Integer = 0 To modules.Count - 1
                [module] = TryCast(modules(i), _
                    ADX_IE_Addon_And_WinApp_VB.IEModule)
                If [module] IsNot Nothing Then
                    remoteModules.Add([module])
                    comboBox1.Items.Add( _
                        [module].GetCurrentTitle())
                End If
            Next
        End If
        If comboBox1.Items.Count > 0 Then
            comboBox1.SelectedIndex = 0
        Else
            Application.[Exit]()
        End If
    Catch err As Exception
        MessageBox.Show(err.Message, Me.Text, _
            MessageBoxButtons.OK, MessageBoxIcon.[Error])
    Finally
        comboBox1.EndUpdate()
    End Try
End Sub

That's all with it.

You may also be interested in:

Understanding and Working in Protected Mode Internet Explorer
How to develop an add-on for IE6, IE7 and IE8
How to add a custom button to IE toolbar
How to create an Explorer bar and context menu

Available downloads:

This sample add-in was developed using Add-in Express 2009 for Internet Explorer and .net

C# sample IE add-on for VS 2005
VB.NET sample IE add-on for VS 2005

Post a comment

Have any questions? Ask us right now!