VERSION 5.00
Begin VB.Form frmMain
BorderStyle = 1 'Fixed Single
Caption = "IP Checker"
ClientHeight = 2070
ClientLeft = 45
ClientTop = 330
ClientWidth = 3240
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 2070
ScaleWidth = 3240
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdCheck
Caption = "Check IP"
Height = 375
Left = 1080
TabIndex = 6
Top = 1560
Width = 1095
End
Begin VB.TextBox txtIP2
Height = 285
Left = 1800
TabIndex = 2
Text = "192.168.1.1"
Top = 1080
Width = 1335
End
Begin VB.TextBox txtSubnetMask
Height = 285
Left = 1800
TabIndex = 1
Text = "255.255.255.0"
Top = 600
Width = 1335
End
Begin VB.TextBox txtIP1
Height = 285
Left = 1800
TabIndex = 0
Text = "192.168.1.168"
Top = 240
Width = 1335
End
Begin VB.Label lblIP2
Caption = "Second IP Number"
Height = 255
Left = 120
TabIndex = 5
Top = 1080
Width = 1575
End
Begin VB.Label lblSubnetMask
Caption = "Subnet Mask"
Height = 255
Left = 120
TabIndex = 4
Top = 600
Width = 1575
End
Begin VB.Label lblIP1
Caption = "First IP Number"
Height = 255
Left = 120
TabIndex = 3
Top = 240
Width = 1575
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub cmdCheck_Click()
Dim lIP1, lIP2, lMask
lIP1 = IPText2Value(txtIP1.Text)
lIP2 = IPText2Value(txtIP2.Text)
'Beware, there is no check of the correctness of the mask!
'In binary form a correct subnet mask starts with a number of one's and is
'then follwed by a number of zero's.
lMask = IPText2Value(txtSubnetMask.Text)
If lIP1 = 0 Or lIP2 = 0 Or lMask = 0 Then
'Something has gone wrong
MsgBox "One of the text boxes contains an invalid IP Address", vbCritical
Exit Sub
End If
If (lIP1 And lMask) = (lIP2 And lMask) Then
MsgBox "Both addresses are within the same subnet!", vbInformation
Else
MsgBox "Both addresses are within different subnets!", vbExclamation
End If
End Sub
Private Function IPText2Value(p_sIPText As String) As Long
Dim asFields() As String
Dim i As Integer
Dim lIP As Long
'Split the IP address in its respective values
asFields = Split(p_sIPText, ".")
'Check if there are really three values
If UBound(asFields) <> 3 Then
Exit Function
End If
'Check for numerical values, lower (0) and upper (255) bounds
For i = 0 To 3
If Not IsNumeric(asFields(i)) Then
Exit Function
End If
'CLng will fail on non-numeric values, so there check is divided in two parts
If CLng(asFields(i)) < 0 Or CLng(asFields(i)) > 255 Then
Exit Function
End If
'Overflow check
If i = 0 And CLng(asFields(i)) > 127 Then
lIP = lIP + ((CLng(asFields(i)) - 128) * 2 ^ ((3 - i) * 8) - 2 ^ 31)
Else
lIP = lIP + CLng(asFields(i)) * 2 ^ ((3 - i) * 8)
End If
Next i
IPText2Value = lIP
Debug.Print Hex$(lIP)
End Function