forked from basildane/WakeOnLAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationEvents.vb
191 lines (158 loc) · 7.89 KB
/
ApplicationEvents.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
' WakeOnLAN - Wake On LAN
' Copyright (C) 2004-2016 Aquila Technology, LLC. <webmaster@aquilatech.com>
'
' This file is part of WakeOnLAN.
'
' WakeOnLAN is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' WakeOnLAN is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with WakeOnLAN. If not, see <http://www.gnu.org/licenses/>.
Imports System.Globalization
Imports Localization
Imports AlphaWindow
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Namespace My
' The following events are available for MyApplication:
'
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function ShowWindow(ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
End Function
' Defines:
' DISPLAY - used to zero out the last part of MAC addresses for screenshots
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As ApplicationServices.StartupEventArgs) Handles Me.Startup
If (Control.ModifierKeys = Keys.Control) Then
SafeMode.ShowDialog()
End If
Debug.WriteLine("CommonApplicationData: " & Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
singleInstance()
upgradeSettings()
ConfigureCulture()
Dim version As String = String.Format(Resources.Strings.Version, Application.Info.Version.Major, Application.Info.Version.Minor, Application.Info.Version.Build, Application.Info.Version.Revision)
If (Application.Info.Version.Revision > 0) Then
version &= " BETA " & Application.Info.Version.Revision
End If
Splash.ShowSplash(Resources.Splash, Resources.Strings.Title, version, Resources.Strings.Copyright)
End Sub
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
Dim crash As New Crash()
crash.exception = e.Exception
crash.ShowDialog()
Application.Log.WriteException(e.Exception, TraceEventType.Critical, "Application shut down at " & Computer.Clock.GmtTime.ToString)
End Sub
''' <summary>
''' Setup the culture and language configuration.
''' </summary>
''' <remarks></remarks>
Private Sub ConfigureCulture()
If String.IsNullOrEmpty(Settings.Language) Then
Dim regKey As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\Aquila Technology\WakeOnLAN", RegistryKeyPermissionCheck.ReadSubTree)
If regKey Is Nothing Then
regKey = Registry.LocalMachine.OpenSubKey("Software\WOW6432Node\Aquila Technology\WakeOnLAN", RegistryKeyPermissionCheck.ReadSubTree)
End If
If regKey Is Nothing Then
Settings.Language = "en-US"
Else
Dim language As String = regKey.GetValue("Language", "en-US", RegistryValueOptions.None)
Select Case language
Case "ar_JO"
language = "ar-JO"
Case "de"
language = "de-DE"
Case "en"
language = "en-US"
Case "es"
language = "es-ES"
Case "fi"
language = "fi-FI"
Case "fr"
language = "fr-FR"
Case "hu"
language = "hu-HU"
Case "nl"
language = "nl-NL"
Case "pt_BR"
language = "pt-BR"
Case "ru"
language = "ru-RU"
Case "zh_TW"
language = "zh-TW"
End Select
regKey.Close()
Settings.Language = language
End If
End If
CultureManager.ApplicationUICulture = New CultureInfo(Settings.Language)
Debug.WriteLine("Language: " & Settings.Language)
End Sub
''' <summary>
''' Search for another running instance of WOL. If found, activate it and terminate this instance.
''' </summary>
''' <remarks></remarks>
Private Sub singleInstance()
Const SW_RESTORE As Int32 = 9
Const SW_SHOW As Int32 = 5
Dim hwnd As IntPtr
hwnd = FindWindow(Nothing, Resources.Strings.Title)
If hwnd <> IntPtr.Zero Then
ShowWindow(hwnd, SW_SHOW)
ShowWindow(hwnd, SW_RESTORE)
SetForegroundWindow(hwnd)
End
End If
End Sub
''' <summary>
''' If this is an upgrade from a previous version, try to recover the previous user settings.
''' </summary>
''' <remarks></remarks>
Private Sub upgradeSettings()
Dim regKey As RegistryKey
Dim database As String
Dim newPath As String
Dim filename As String
If Settings.needUpgrade Then
Settings.Upgrade()
Settings.needUpgrade = False
Settings.Save()
End If
Try
For i As Int16 = 0 To CommandLineArgs.Count
If (CommandLineArgs(i) = "-p") Then
Settings.dbPath = CommandLineArgs(i + 1)
Return
End If
Next
regKey = Registry.CurrentUser.OpenSubKey("Software\Aquila Technology\WakeOnLAN")
database = regKey.GetValue("Database", IO.Directory.GetParent(Computer.FileSystem.SpecialDirectories.AllUsersApplicationData.ToString).ToString, Microsoft.Win32.RegistryValueOptions.None)
regKey.Close()
filename = IO.Path.GetFileName(Settings.dbPath)
If (String.IsNullOrEmpty(filename)) Then
filename = "machines.xml"
End If
newPath = IO.Path.Combine(database, filename)
If (Settings.dbPath <> newPath) Then
Settings.dbPath = newPath
Settings.Save()
End If
Catch ex As Exception
End Try
End Sub
End Class
End Namespace