forked from basildane/WakeOnLAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Globals.vb
176 lines (142 loc) · 6.8 KB
/
Globals.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
' WakeOnLAN - Wake On LAN
' Copyright (C) 2004-2019 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.Linq
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Net
Imports Machines
Module Globals
Private Declare Function FormatMessageA Lib "kernel32" (ByVal flags As Integer, ByRef source As Object, ByVal messageID As Integer, ByVal languageID As Integer, ByVal buffer As String, ByVal size As Integer, ByRef arguments As Integer) As Integer
Public Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Int32, ByVal SrcIP As Int32, ByVal pMacAddr As Byte(), ByRef PhyAddrLen As Integer) As Integer
<DllImport("user32.dll")>
Public Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Public ShutdownMode As Boolean 'true if in shutdown mode, false if in abort mode
Public CurrentItem As ListViewItem
Public Sub ShowHelp(parent As Control, url As String)
Try
#If DEBUG Then
Help.ShowHelp(parent, "file:///" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\..\help\" + url))
#Else
Help.ShowHelp(parent, "file:///" + AppDomain.CurrentDomain.BaseDirectory + "help\" + url)
#End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Help Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function SaveListViewState(ByVal listview As ListView) As String
Return listview.Columns.Cast(Of ColumnHeader)().Aggregate("", Function(current, c) current & (c.Width & " "))
End Function
Public Sub GetListViewState(ByVal listview As ListView, ByVal state As String)
Dim s() As String
Dim i As Int16
s = Split(state)
If (UBound(s) <> listview.Columns.Count) Then Exit Sub
For i = 0 To UBound(s) - 1
listview.Columns(i).Width = Int(s(i))
Next
End Sub
Public Function FormatMessage(ByVal [error] As Integer) As String
Const FORMAT_MESSAGE_FROM_SYSTEM As UInteger = &H1000
Const LANG_NEUTRAL As Integer = &H0
Const bufferLen As Integer = 1024
Dim buffer As String = Space(bufferLen)
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, IntPtr.Zero, [error], LANG_NEUTRAL, buffer, bufferLen, IntPtr.Zero)
buffer = Replace(Replace(buffer, Chr(13), ""), Chr(10), "")
If buffer.Contains(Chr(0)) Then
buffer = buffer.Substring(0, buffer.IndexOf(Chr(0)))
Else
buffer = String.Empty
End If
Return buffer
End Function
Public Function IpToInt(address As IPAddress) As UInt32
Dim bytes As Byte() = address.GetAddressBytes()
If BitConverter.IsLittleEndian Then
Array.Reverse(bytes)
End If
Dim num As UInt32 = BitConverter.ToUInt32(bytes, 0)
Return num
End Function
Public Function IpToInt(address As String) As UInt32
Dim _address As IPAddress = Nothing
If (IPAddress.TryParse(address, _address)) Then
Return IpToInt(_address)
Else
Return 0
End If
End Function
Public Function CompareMac(mac1 As String, mac2 As String) As Int32
Try
Dim _mac1 As String = Replace(mac1, ":", "")
_mac1 = Replace(_mac1, "-", "")
Dim _mac2 As String = Replace(mac2, ":", "")
_mac2 = Replace(_mac2, "-", "")
Return StrComp(_mac1, _mac2, CompareMethod.Text)
Catch ex As Exception
MessageBox.Show(ex.ToString, "compareMAC")
End Try
End Function
''' <summary>
''' Executes the appropriate Remote Desktop function.
''' Supports rdp, putty, cmd, http, https.
''' </summary>
''' <param name="MachineName"></param>
Public Sub RemoteCommand(MachineName As String)
Dim machine As Machine = Machines(MachineName)
Dim command As String = String.Empty
Try
If (machine.RemoteCommand.StartsWith("rdp:")) Then
command = String.Format("mstsc.exe -v:{0}", machine.RemoteCommand.Substring(4))
ElseIf (machine.RemoteCommand.ToLower().EndsWith(".rdp")) Then
command = String.Format("mstsc.exe ""{0}""", machine.RemoteCommand)
ElseIf (machine.RemoteCommand.StartsWith("putty:")) Then
command = String.Format("putty -load ""{0}""", machine.RemoteCommand.Substring(6))
ElseIf (machine.RemoteCommand.StartsWith("cmd:")) Then
command = machine.RemoteCommand.Substring(4)
ElseIf (String.IsNullOrWhiteSpace(machine.RemoteCommand)) Then
command = String.Format("mstsc.exe -v:{0}", machine.Netbios)
ElseIf (machine.RemoteCommand.StartsWith("http:") Or machine.RemoteCommand.StartsWith("https:")) Then
Process.Start(machine.RemoteCommand)
Return
End If
Shell(command, AppWinStyle.NormalFocus, False)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "RemoteCommand")
End Try
End Sub
''' <summary>
''' Returns TRUE if form is on screen.
''' </summary>
''' <param name="form">The form to test</param>
''' <returns>Boolean</returns>
Public Function IsOnScreen(form As Form) As Boolean
Const margin As Integer = 100
If form.Left < 0 Or form.Top < 0 Then Return False
Dim formRectangle As New Rectangle(form.Left + margin, form.Top + margin, form.Width, form.Height)
Return Screen.AllScreens.Any(Function(s) s.WorkingArea.IntersectsWith(formRectangle))
End Function
''' <summary>
''' Centers a given form on the screen.
''' </summary>
''' <param name="form">The form to center</param>
Public Sub CenterForm(form As Form)
form.Top = (My.Computer.Screen.WorkingArea.Height \ 2) - (form.Height \ 2)
form.Left = (My.Computer.Screen.WorkingArea.Width \ 2) - (form.Width \ 2)
End Sub
End Module