' VB.Net doesn't support a Union type, but it does allow you to
' "decorate" a structure to accomplish the same thing
<StructLayout(LayoutKind.Explicit)> _
Private Structure IP_Parts
<FieldOffset(0)> Dim address As Int64
<FieldOffset(3)> Dim Right As Byte
<FieldOffset(2)> Dim middleRight As Byte
<FieldOffset(1)> Dim middleLeft As Byte
<FieldOffset(0)> Dim Left As Byte
End Structure
Private Shared Function IPStrToLong(ByVal IPstr As String) As Long
Dim ip As IPAddress
Dim parts As IP_Parts
Dim buf() As String
ip = [IPAddress].None
If Not [IPAddress].TryParse(IPstr, ip) Then
Return 0
End If
' The IPAddress.Address property has been "depreciated", but we still
' need a way to convert a IPv4 "quad dot" to and from a long, so just
' keep the compiler happy, we use this routine
buf = ip.ToString.Split("."c)
If buf.Length <> 4 Then
' Sorry, we don't support IPv6 notation
Return 0
End If
parts.Right = CByte(buf(3))
parts.middleRight = CByte(buf(2))
parts.middleLeft = CByte(buf(1))
parts.Left = CByte(buf(0))
Return parts.address
End Function
|