Dmitry Kostochko

HowTo: Synchronize settings of an IE plug-in

Nearly every software program has some settings. An Internet Explorer plug-in is not an exception. The IE add-on settings can be stored either in a file or in a registry branch, all is simple and transparent about this. But how to ensure the settings synchronization for all active instances of the IE plug-in in all running Internet Explorer processes? By using the AddinExpress.IE.ADXIESharedMemory class.

Well, here goes! Let’s create a new IE plug-in project using the “ADX IE Bar” wizard, populate our IEBarModule with a group box that has 3 radio buttons, add a couple of checkboxes, a combo box and text box. Ergo, we need to store and synchronize 5 values, Integer as index for RadioButtons, two Boolean values as CheckBox states, another Integer as index for ComboBox and String for TextBox. Now, add to the project your own class, a descendent of ADXIESharedMemory, by using “ADX IE Shared Data” wizard and modify the GlobalData class, which is present in every IE plug-in project.

Here is a list of private variables needed:

Private lastOutputPath As String
Private optionPageData As MySharedData
 
Private Const FILE_LOCKED As Integer = 0
Private Const RADIO_OPTION As Integer = 1
Private Const CHECK_OPTION_1 As Integer = 2
Private Const CHECK_OPTION_2 As Integer = 3
Private Const LIST_OPTION As Integer = 4
Private Const STRING_OPTION As Integer = 5

Modified constructor:

Public Sub New()
    MyBase.New()
    'System.Diagnostics.Debugger.Break()
    Dim outputPath As String = _
        Path.Combine(Environment.GetFolderPath( _
        Environment.SpecialFolder.InternetCache), "Low")
    outputPath = Path.Combine(outputPath, "MyIEOptionPage")
    If Not Directory.Exists(outputPath) Then
        Directory.CreateDirectory(outputPath)
    End If
    Me.lastOutputPath = outputPath
    Me.optionPageData = New MySharedData( _
        "MyIEOptionPage", 512, 10)
 
    Me.optionPageData(FILE_LOCKED) = 0
    Me.optionPageData(RADIO_OPTION) = 0
    Me.optionPageData(CHECK_OPTION_1) = False
    Me.optionPageData(CHECK_OPTION_2) = False
    Me.optionPageData(LIST_OPTION) = 0
    Me.optionPageData(STRING_OPTION) = "Default"
End Sub

Added methods:

Public Sub LoadOptions()
    If CInt(Me.optionPageData(FILE_LOCKED)) = 0 Then
        Me.optionPageData(FILE_LOCKED) = 1
        Try
            Dim outputFile As String = _
                Path.Combine(lastOutputPath, "myoptions.cfg")
            If File.Exists(outputFile) Then
                Using sr As New StreamReader(outputFile)
                    Dim line As String
                    line = sr.ReadLine()
                    Me.optionPageData(RADIO_OPTION) = _
                      Convert.ToInt32(line.Trim())
                    line = sr.ReadLine()
                    Me.optionPageData(CHECK_OPTION_1) = _
                      Convert.ToBoolean(line.Trim())
                    line = sr.ReadLine()
                    Me.optionPageData(CHECK_OPTION_2) = _
                      Convert.ToBoolean(line.Trim())
                    line = sr.ReadLine()
                    Me.optionPageData(LIST_OPTION) = _
                      Convert.ToInt32(line.Trim())
                    line = sr.ReadLine()
                    Me.optionPageData(STRING_OPTION) = _
                      line.Trim()
                End Using
            End If
        Finally
            Me.optionPageData(FILE_LOCKED) = 0
        End Try
    End If
End Sub
 
Private Sub SaveOptions()
    If CInt(Me.optionPageData(FILE_LOCKED)) = 0 Then
        Me.optionPageData(FILE_LOCKED) = 1
        Try
            Dim outputFile As String = _
                Path.Combine(lastOutputPath, "myoptions.cfg")
            Using sw As New StreamWriter(outputFile, False)
                sw.Write(Me.optionPageData(RADIO_OPTION))
                sw.Write(sw.NewLine)
                sw.Write(Me.optionPageData(CHECK_OPTION_1))
                sw.Write(sw.NewLine)
                sw.Write(Me.optionPageData(CHECK_OPTION_2))
                sw.Write(sw.NewLine)
                sw.Write(Me.optionPageData(LIST_OPTION))
                sw.Write(sw.NewLine)
                sw.Write(Me.optionPageData(STRING_OPTION))
            End Using
        Finally
            Me.optionPageData(FILE_LOCKED) = 0
        End Try
    End If
End Sub
 
Public Sub UpdateOptions(ByVal [module] As IEBarModule)
    SyncLock Instance
        Me.optionPageData(RADIO_OPTION) = [module].RadioOptions
        Me.optionPageData(CHECK_OPTION_1) = [module].CheckOption1
        Me.optionPageData(CHECK_OPTION_2) = [module].CheckOption2
        Me.optionPageData(LIST_OPTION) = [module].ListOption
        Me.optionPageData(STRING_OPTION) = [module].StringOption
        SaveOptions()
    End SyncLock
End Sub
 
Public Sub RefreshOptions(ByVal [module] As IEBarModule)
    SyncLock Instance
        [module].RadioOptions = _
          CInt(Me.optionPageData(RADIO_OPTION))
        [module].CheckOption1 = _
          CBool(Me.optionPageData(CHECK_OPTION_1))
        [module].CheckOption2 = _
          CBool(Me.optionPageData(CHECK_OPTION_2))
        [module].ListOption = _
          CInt(Me.optionPageData(LIST_OPTION))
        [module].StringOption = _
          DirectCast(Me.optionPageData(STRING_OPTION), String)
    End SyncLock
End Sub
 
Public Sub CloseOptions()
    SyncLock Instance
        If Me.optionPageData IsNot Nothing Then
            Try
                SaveOptions()
            Finally
                Me.optionPageData.Dispose()
                Me.optionPageData = Nothing
            End Try
        End If
    End SyncLock
End Sub

Now let’s go back to IEBarModule and add Load, OnConnect, OnDisconnect, OnSendMessage and two button Click event handlers (one to apply settings, and another to cancel them):

Private startMode As Boolean = True
Private Const WM_UPDATE_OPTIONS As Integer = (&H400 + 3000)
 
Private Sub IEBarModule_Load( _
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
 
    Try
        GlobalData.Instance.LoadOptions()
    Catch err As Exception
        MessageBox.Show(err.Message, Me.Title, _
          MessageBoxButtons.OK, MessageBoxIcon.[Error])
    End Try
End Sub
 
Private Sub IEBarModule_OnConnect( _
    ByVal sender As System.Object, _
    ByVal threadId As System.Int32) Handles MyBase.OnConnect
 
    comboBox1.SelectedIndex = 0
    SendMessageToAll(WM_UPDATE_OPTIONS, IntPtr.Zero, IntPtr.Zero)
    startMode = False
End Sub
 
Private Sub IEBarModule_OnDisconnect( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.OnDisconnect
 
    Try
        GlobalData.Instance.CloseOptions()
    Catch err As Exception
        MessageBox.Show(err.Message, Me.Title, _
          MessageBoxButtons.OK, MessageBoxIcon.[Error])
    End Try
End Sub
 
Private Sub IEBarModule_OnSendMessage( _
    ByVal e As AddinExpress.IE.ADXIESendMessageEventArgs) _
    Handles MyBase.OnSendMessage
 
    Select Case e.Message
        Case WM_UPDATE_OPTIONS
            If True Then
                startMode = True
                Try
                    GlobalData.Instance.RefreshOptions(Me)
                Finally
                    startMode = False
                End Try
            End If
            Exit Select
    End Select
End Sub
 
Private Sub buttonApply_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles buttonApply.Click
 
    Try
        GlobalData.Instance.UpdateOptions(Me)
        SendMessageToAll(WM_UPDATE_OPTIONS, IntPtr.Zero, _
            IntPtr.Zero)
    Finally
        buttonApply.Enabled = False
        buttonCancel.Enabled = False
    End Try
End Sub
 
Private Sub buttonCancel_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles buttonCancel.Click
 
    Try
        GlobalData.Instance.RefreshOptions(Me)
    Finally
        buttonApply.Enabled = False
        buttonCancel.Enabled = False
    End Try
End Sub

So, now we have all the settings stored in the myoptions.cfg file. Once the settings are changed, all active instances of the IE plug-in in all running Internet Explorer processes learn the fact and get to know their new values.

That's all for today.

You may also be interested in:

How to develop an add-on for IE6, IE7 and IE8
How to create a custom button for IE toolbar
How to build 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 plug-in for VS 2005
VB.NET sample IE plug-in for VS 2005

32 Comments

  • PB says:

    Hi,

    isn’t using Windows registry better solution? I had a lot of problems (not solved yet) in storing app data in Vista (I used standard C# Settings mechanism).

    Storing data in the registry works pretty well on every tested environment (XP, Vista, 2003 Server).

    Regards,
    PB

  • Dmitry Kostochko (Add-in Express Team) says:

    Sure, you can store your data in the Windows registry. I only possible problem that I foresee is that you will need somehow to delete the stored data when uninstalling the plug-in. Though, it may not be a problem.

  • toolbar says:

    i would like to know if it is possible to use the same toolbar as opened in the first tab with C#?

  • Sergey Grischenko (a member of the Add-in Express team) says:

    No, it is not possible in IE.

  • toolbar says:

    sir but i have to do that possible to use the same toolbar as opened in the first tab.please help me..

  • toolbar says:

    when a new instance of the toolbar is created and all the settings and information from the first tab is lost.how to save or store it?

  • Sergey Grischenko (a member of the Add-in Express team) says:

    In this case all the data should be stored in a file as shown in the example.

  • toolbar says:

    can you give the link for that example..
    or name of that example

  • Sergey Grischenko (a member of the Add-in Express team) says:

    This article provides the full source code of the example. Please see the “Available downloads” section at the end of this article.

  • toolbar says:

    using Windows registry better solution
    but how can i store data in the Windows registry?
    please solve it..

  • toolbar says:

    please send code for Synchronize settings in c# because we work on c#.

  • Sergey Grischenko (a member of the Add-in Express team) says:

    You can download the example using the link below:
    https://www.add-in-express.com/files/howtos/blog/adx-ie-shared-memory-cs.zip
    To read/write your settings in the system registry you can use the RegistryKey class of the .NET Framework:
    https://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey(v=VS.80).aspx

  • toolbar says:

    i have check the example https://www.add-in-express.com/files/howtos/blog/adx-ie-shared-memory-cs.zip but its not working proper with add-in-express 2011 + visual studio 2010.

    its generate frame work related warning..

  • Dmitry Kostochko (Add-in Express Team) says:

    I suppose you meant the warning in the Conversion Report window of Visual Studio 2010 that says “Your project is targeting .NET Framework 2.0 or 3.0. If your project uses assemblies requiring a newer .NET Framework, your project will fail to build. You can change the .NET Framework version by clicking Properties on the project menu and then selecting a new version in the ‘.NET Framework’ dropdown box”. Am I right? If it is so, please ignore this pseudo-warning, it is just a notification. You can build, register and test the project without any problems.

  • toolbar says:

    yes that warning is come but after i run that its not work with multiple tab..

  • Dmitry Kostochko (Add-in Express Team) says:

    Let’s try to compare our testing steps. I do the following:
    1. Build and register the sample in Visual Studio 2010.
    2. Run IE 9 and open 3 tabs with the Google home page.
    3. Switch to the first tab, select “Option 2” in the group box, check the “Option 4” and “Option 5” check boxes, change the text in the “String Value” text box and click the Apply button.
    4. Switch to the second and then to the third tab and see that changes I made in the first tab are applied correctly.

    Could you please describe steps that you do?

  • toolbar says:

    k but sir i am work on IE9.

  • toolbar says:

    sory iam work with IE8.
    is that steps are work with ie8???

  • toolbar says:

    when i am going to open in vs2010 that sample project need to be convert..
    means its require conversion..

  • toolbar says:

    first i build the sample project as your advice.
    after that when i am going to register its shows this warning:

    “you are using an outdated version of add-in express loader.we strongly recommend that you update the loader.
    you can find the latest version in the redistributable folder of the add-in-express installation directory.”
    with two buttons”UPDATE LOADER” & “OK”

    what i am do update or
    OK??

  • toolbar says:

    first i build the sample project as your advice.
    after that when i am going to regester its shows this warning:

    “you are using an outdated vaersion of add-in expressloader.we strongly recommend thayt you update the loader.
    you can find the latest version in the redistributables folder of the add-in-express installation directory.”
    with two buttons”UPDATE LOADER” & “OK”

    what i am do update or
    ok??

    ———————————————–

    after regeter i am going to run that sample with F5 it shows this warning:

    “A project with an output type of class liabrary cannot started directly.

    in order to debuge this project,add an executable project to this solution which references the library project.set the
    executable project as the startup project”

    i hope you give me solution for this two warnings..

    ??

  • Dmitry Kostochko (Add-in Express Team) says:

    IE8: Yes, for IE8 the steps are the same, one additional thing you may need to do is turning on the explorer bar manually by clicking View -> Explorer Bars -> NAME_OF_YOUR_EXPLORER_BAR in the Internet Explorer main menu.

    Conversion: The sample was developed in Visual Studio 2005 intentionally so that it can be opened in VS 2005, 2008 and 2010. Of course, Visual Studio 2008 and 2010 will convert the project automatically before opening.

    Update Loader: We always suggest using the latest loader version, so please update adxLoader.dll and adxLoader64.dll of the project by clicking the Update Loader button.

    Run the sample: To run Internet Explorer from Visual Studio you need to specify the full path to iexplore.exe in the “Start external program” text box. You can find it by selecting your project in the Solution Explorer window, clicking the Properties toolbar button and switching to the Debug tab. BTW, you can run IE in a usual way after registering the sample.

  • toolbar says:

    thanks, friend your example is work now i am work that with toolbar add-on……

  • toolbar says:

    this.optionPageData[STRING_OPTION] = “Default”;

    like “Default” which value i set for PANEL??
    ———————————————————
    and also what for this if i am take panel and want to change color??

    public string StringOption
    {
    get { return textBox1.Text; }
    set { textBox1.Text = value; }
    }

  • Dmitry Kostochko (Add-in Express Team) says:

    This is a very specific question that does not directly relate to the sample described in this post. Can you please contact our support service, I hope we will be able to help you.

  • toolbar says:

    i doing a sample IE Addons Project, In that project there is one toolbar.
    Toolbar contain one button and Panel. At the Stating level panel color is red than when i click on button then panel color is change to green. but when i open new tab than at the initial stag panel color is red instead on green. that means panel color is red not green

    so, how to set the same color as a first tab in all next tab(Multiple tab)???????????????????

  • toolbar says:

    your sample for IE addon is run nice with multiple tab but sir now i want same work
    with panel.
    in that you have use textbox and its getter setter like this..

    public string StringOption
    {
    get { return textBox1.Text; }
    set { textBox1.Text = value; }
    }

    sir now, how i do same for panel mean how to do get set with panel..

  • Dmitry Kostochko (Add-in Express Team) says:

    You can utilize the approach used in this sample in your main project. You can use a descendant of the AddinExpress.IE.ADXIESharedMemory class to store the color of your toolbar, the SendMessageToAll method to inform all instances of your toolbar that the color was changed, and the OnSendMessage event to apply a new color to your toolbar.

    If you need any help or further assistance, please contact our support service or me directly, I will try to create a sample for you.

  • toolbar says:

    please provide example which demonstrate OnTabActivated Event and how to use
    sendGlobalMessage(); in it to make changes for all tabs??

  • toolbar says:

    hi,
    i am working on ie toolbar using add-in-express 2011+vs2010.i have a query regarding
    OnTabChanging().

    -First i create open ie and first process start
    -After take new tab(+) First process come in new tab.
    -but when i stop process which in new tab its not reflect in First tab.

    so, how to reflect the process which run in new tab in previous tab..?

  • toolbar says:

    even i write just message in onTabchange and in ontabchanging event it will not displayed after..

  • Dmitry Kostochko (Add-in Express Team) says:

    I am sorry, your questions do not directly relate to the topics covered in this blog post. Please contact our support service with such technical questions, issues, requests etc., we will try to help you.

Post a comment

Have any questions? Ask us right now!