HowTo: Deal with Protected Mode API in Internet Explorer 7 and IE8
The Protected Mode feature was introduced in Internet Explorer 7 and continues to exist in Internet Explorer 8. Nearly at the same time when the Protected Mode feature appeared, Microsoft introduced the Protected Mode API for developers to use this feature.
All API functions are implemented in the Add-in Express for Internet Explorer and .net product and today we are going to look into their capabilities.
You can create an instance of the AddinExpress.IE.ADXIEProtectedModeAPI class and check out the Protection Mode state using the OnCreate event handler:
Private Sub IEToolBarModule_OnConnect( _ ByVal sender As System.Object, _ ByVal threadId As System.Int32) _ Handles MyBase.OnConnect Try Me.pAPI = New AddinExpress.IE.ADXIEProtectedModeAPI() If Me.pAPI.IsProtectedModeOn() Then Me.label1.Text = "Protected Mode: On" Else Me.label1.Text = "Protected Mode: Off" End If Catch err As Exception MessageBox.Show(err.Message, Me.MenuText, _ MessageBoxButtons.OK, MessageBoxIcon.[Error]) End Try End Sub
Then you can use the ADXIEProtectedModeAPI methods in your code. In the code below, the ShowSaveFileDialog, GetWriteableFolderPath and SaveFile methods are used for saving a file into a location specified by the end-user:
Private Sub buttonSave_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles buttonSave.Click Dim tmpFilePath As String = String.Empty Try If Me.pAPI IsNot Nothing Then Dim state As Integer = 0 Dim fileName As String fileName = Me.pAPI.ShowSaveFileDialog( _ Me.TabWindowHandle, _ "Log.txt", _ Nothing, _ "Text files|*.txt|All files|*.*|", "txt", _ 1, _ ADXIEOpenFileNameFlags.ofnEnableSizing Or _ ADXIEOpenFileNameFlags.ofnHideReadOnly Or _ ADXIEOpenFileNameFlags.ofnPathMustExist Or _ ADXIEOpenFileNameFlags.ofnOverWritePrompt, _ state) If state = 0 Then Exit Sub Try tmpFilePath = Me.pAPI.GetWriteableFolderPath() tmpFilePath = Path.Combine(tmpFilePath, _ "IEProtectedModeAPI") If Not Directory.Exists(tmpFilePath) Then Directory.CreateDirectory(tmpFilePath) End If tmpFilePath = Path.Combine(tmpFilePath, "log.txt") Using sw As New StreamWriter(tmpFilePath, False) sw.WriteLine("IEProtectedModeAPI Example") sw.WriteLine("--------------------------") sw.WriteLine("This is just a test.") End Using If Not File.Exists(tmpFilePath) Then Throw New FileNotFoundException( _ "The log file doesn't exist.") End If Me.pAPI.SaveFile(state, tmpFilePath) MessageBox.Show(Me, _ "The file has been successfully saved.", _ Me.MenuText, MessageBoxButtons.OK, _ MessageBoxIcon.Information) Catch Me.pAPI.CancelSaveFile(state) Throw End Try End If Catch err As Exception MessageBox.Show(Me, err.Message, Me.MenuText, _ MessageBoxButtons.OK, MessageBoxIcon.[Error]) Finally If tmpFilePath <> String.Empty Then If File.Exists(tmpFilePath) Then File.Delete(tmpFilePath) End If End If End Try End Sub
Now you can save your data not only to a special pre-determined location that you can obtain by using the GetWritableFolderPath function, but to any location specified by the end-user. That's all for today.
You may also be interested in:
How to create an add-on for IE6, IE7 and IE8
How to develop an IE toolbar with custom buttons
Building IE browser helper objects
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