'
' File name: Application.xaml.vb
' Location: See Matrox Example Launcher in the MIL Control Center
' 
'
Imports System
Imports System.Windows
Imports Microsoft.Win32
Imports System.Reflection

'********************************************************************************
' Copyright (C) Matrox Electronic Systems Ltd., 1992-2020.
' All Rights Reserved
'********************************************************************************

Class Application

   ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
   ' can be handled in this file.
   Private Sub Application_Startup(sender As Object, e As StartupEventArgs)
      Dim wnd As New MainWindow

      ' First add registry key, if it doesn't already exist.  Necessary due to limitations with IE about websockets
      ' (https://msdn.microsoft.com/en-us/library/ee330736(v=vs.85).aspx#websocket_maxconn)
      Dim subKeyName As String = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_WEBSOCKET_MAXCONNECTIONSPERSERVER"
      Dim valueName As String = Assembly.GetExecutingAssembly().GetName().Name + ".exe"
      SetRegistryValue(subKeyName, valueName, &H80, RegistryValueKind.DWord)

      ' Then instantiate MainWindow (passing url to constructor, if given by user)
      If (e.Args.Length = 1) Then
         wnd = New MainWindow(e.Args(0))
      Else
         wnd = New MainWindow()
      End If

      MainWindow = wnd
      wnd.Show()
   End Sub

   Public Sub SetRegistryValue(registrySubKeyName As String, valueName As String, valueToSet As Object, registryKind As RegistryValueKind)
      Try
         ' Get current value in registry (if it already exists)
         Dim currentValueInRegistry As Object = Registry.GetValue(registrySubKeyName, valueName, Nothing)

         'If it doesn't exist, or a different value, we (re)create it
         If Not valueToSet.Equals(currentValueInRegistry) Then
            Registry.SetValue(registrySubKeyName, valueName, valueToSet, registryKind)
         End If
      Catch e As Exception
         Console.WriteLine("Exception occurred: " + e.Message)
      End Try
   End Sub

End Class