-
Notifications
You must be signed in to change notification settings - Fork 148
/
ApplicationEvents.vb
225 lines (186 loc) · 7.71 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
' WakeOnLAN - Wake On LAN
' Copyright (C) 2004-2019 Aquila Technology, LLC. <[email protected]>
'
' 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.Configuration
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
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
singleInstance()
upgradeSettings()
ConfigureCulture()
If My.Settings.TraceLog Then
Tracelog.Load()
End If
'Throw New Exception("This is a test unhandled exception.")
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
Tracelog.WriteLine(version)
Tracelog.Line()
#If Not DEBUG Then
If (Settings.ShowSplash) Then
Splash.ShowSplash(Resources.Splash, Resources.Strings.Title, version, Resources.Strings.Copyright)
End If
#End If
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)
Tracelog.WriteLine("CRASH: " & e.Exception.ToString)
Tracelog.Close()
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 "it"
language = "it-IT"
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)
Tracelog.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
Try
If Settings.needUpgrade Then
Settings.Upgrade()
Settings.needUpgrade = False
Settings.Save()
End If
Catch ex As Exception
Try
filename = DirectCast(ex.InnerException, ConfigurationErrorsException).Filename
If MessageBox.Show(Text.RegularExpressions.Regex.Unescape(Resources.Strings.errUserConfigCorrupt), Resources.Strings.errUserConfigTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Error) = DialogResult.Yes Then
IO.File.Delete(filename)
Windows.Forms.Application.Restart()
End If
Process.GetCurrentProcess().Kill()
Catch ex1 As Exception
MessageBox.Show(ex.ToString)
End Try
End Try
Try
For i As Int16 = 0 To CommandLineArgs.Count - 1
If (CommandLineArgs(i) = "-p") Then
Settings.dbPath = CommandLineArgs(i + 1)
Exit For
End If
Next
regKey = Registry.CurrentUser.OpenSubKey("Software\Aquila Technology\WakeOnLAN")
If regKey Is Nothing Then
regKey = Registry.CurrentUser.CreateSubKey("Software\Aquila Technology\WakeOnLAN", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryOptions.None)
End If
database = regKey.GetValue("Database", IO.Directory.GetParent(Computer.FileSystem.SpecialDirectories.AllUsersApplicationData.ToString).ToString, 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