0

We have a WPF app that uses the browser control. As this emulates an older version of IE most sites do not render correctly.

By adding a registry key of 'OurApp.exe' and a value of 11000 to HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION\ it then works perfectly.

The question is - how can I detect the current setting (if there is one) and change it if required from within the app using VB.NET?

I can return the current version of IE on the local machine using

Public Function ReturnIEVersion() As String
    Try
        Dim vVersionO As Object = New System.Windows.Forms.WebBrowser().Version
        Dim vVersion As String = vVersionO.ToString
        Return vVersion
    Catch ex As Exception
        EmailError(ex)
        Return "Error"
    End Try
End Function

Then I just need to compare that to any existing entry and update if required, and I assume a method to determine if the local machine is 64 or 32 bit (as the registry paths will be different)?

Thank you

gchq
  • 1,603
  • 2
  • 27
  • 52

2 Answers2

1

This question is a duplicate. The answer here should resolve your problem!

I have converted the code to VB for you:

Private Shared Sub Main()
    If Not mutex.WaitOne(TimeSpan.FromSeconds(2), False) Then
        'another application instance is running
        Return
    End If
    Try

        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)

        Dim targetApplication = Process.GetCurrentProcess().ProcessName + ".exe"
        Dim ie_emulation As Integer = 10000
        Try
            Dim tmp As String = Properties.Settings.[Default].ie_emulation
            ie_emulation = Integer.Parse(tmp)
        Catch
        End Try
        SetIEVersioneKeyforWebBrowserControl(targetApplication, ie_emulation)

        m_webLoader = New FormMain()

        Application.Run(m_webLoader)
    Finally
        mutex.ReleaseMutex()
    End Try
End Sub

Private Shared Sub SetIEVersioneKeyforWebBrowserControl(appName As String, ieval As Integer)
    Dim Regkey As RegistryKey = Nothing
    Try


        Regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", True)

        'If the path is not correct or 
        'If user't have priviledges to access registry 
        If Regkey Is Nothing Then
            YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION Failed - Registry key Not found")
            Return
        End If

        Dim FindAppkey As String = Convert.ToString(Regkey.GetValue(appName))

        'Check if key is already present 
        If FindAppkey = "" + ieval Then
            YukLoggerObj.logInfoMsg("Application FEATURE_BROWSER_EMULATION already set to " + ieval)
            Regkey.Close()
            Return
        End If

        'If key is not present or different from desired, add/modify the key , key value 
        Regkey.SetValue(appName, CInt(ieval), RegistryValueKind.DWord)

        'check for the key after adding 
        FindAppkey = Convert.ToString(Regkey.GetValue(appName))

        If FindAppkey = "" + ieval Then
            YukLoggerObj.logInfoMsg("Application FEATURE_BROWSER_EMULATION changed to " + ieval + "; changes will be visible at application restart")
        Else
            YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION setting failed; current value is  " + ieval)



        End If
    Catch ex As Exception

        YukLoggerObj.logWarnMsg("Application FEATURE_BROWSER_EMULATION setting failed; " + ex.Message)
    Finally
        'Close the Registry 
        If Regkey IsNot Nothing Then
            Regkey.Close()
        End If
    End Try


End Sub
Community
  • 1
  • 1
Sam Makin
  • 1,526
  • 8
  • 23
  • Thank you for the reply. How do I overcome 'Requested registry access is not allowed.' on the OpenSubKey line? I didn't specify before, but the app is a ClickOnce WPF application. – gchq Feb 17 '15 at 18:01
  • Hi - it sounds like your application isnt running as a local admin - either grant the account local admin privileges or change the registry permissions – Sam Makin Feb 18 '15 at 11:08
  • When I pause to think about it, changing any rights in the app could cause some real issues - a lot of users don't have admin rights on their PC and ClickOnce will install and run without that, unless I am missing something? Thank you for taking the time to post the converted code. – gchq Feb 18 '15 at 13:59
  • If you want to edit the registry the user will require permission to do so - that is the purpose of security – Sam Makin Feb 18 '15 at 14:19
  • Although there are ways around it (which I have just learned) see http://stackoverflow.com/questions/562350/requested-registry-access-is-not-allowed – Sam Makin Feb 18 '15 at 14:21
  • Please can you mark the answer as correct as the permission issue is not the same as the case you originally opened? Unless the answer does not solve the question – Sam Makin Feb 18 '15 at 14:26
  • I have marked as answer. I suspect if I change the app.manifest users without admin privileges will not be able to run the app. The other solution (Process.StartInfo.UseShellExecute = true) running a separate process could have some mileage as those with admin rights would be able to run it, others would not. With ClickOnce apps you cannot run as elevated (right click, run as admin) that I know of – gchq Feb 18 '15 at 16:19
  • Thanks - You may be correct - seperate issue though ;P – Sam Makin Feb 18 '15 at 16:19
0

The the original post inquired about setting the HKCU value, however I believe that the code is attempting to set the value in HKLM. This would lead to the issue with permissions.

To set the value for HKLM, the change should be made during the install routine which would be performed under the required elevated credentials.

To set the value at runtime, then the HKCU value should be set vice the HKLM value. HKCU value has the advantage of not caring about the "bitness" of the application, whereas for the HKLM value, the "bitness" will determine whether the entry should be added under the WOW6432Node or not.

Dan
  • 1
  • Hey Dan - thanks for your reply. A couple of issues - 1) This is a click-once app and if I am not mistaken this doesn't install with elevated credentials and 2) Would need to be updated each time the browser is updated to a new version. At the moment we have left this to the customer to update the registry as required – gchq Jun 20 '15 at 16:03