IPアドレス文字列(例:192.168.10.1)をコード値に変換したり、コード値からIPアドレス文字列に変換するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
Private Const WSADESCRIPTION_LEN As Integer = 256 | |
Private Const WSASYS_STATUS_LEN As Integer = 128 | |
Private Type WSAData | |
wVersion As Integer | |
wHighVersion As Integer | |
szDescription(0 To WSADESCRIPTION_LEN) As Byte | |
szSystemStatus(0 To WSADESCRIPTION_LEN) As Byte | |
iMaxSockets As Integer | |
iMaxUDPDG As Integer | |
lpVendorInfo As Long | |
End Type | |
Public Const AF_INET As Integer = 2 | |
Public Const AF_INET6 As Integer = 23 | |
Private Declare PtrSafe Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequested As Integer, lpWSADATA As Any) As Long | |
Private Declare PtrSafe Function WSACleanup Lib "wsock32.dll" () As Long | |
'IPv4またはIPv6インターネットネットワークアドレスからインターネット標準形式の文字列に変換 | |
Private Declare PtrSafe Function inet_addr Lib "Ws2_32.dll" (ByVal cp As String) As Long | |
'IPv4またはIPv6インターネットネットワークアドレスからインターネット標準形式の文字列に変換 | |
Private Declare PtrSafe Function InetNtopW Lib "Ws2_32.dll" (ByVal Family As Integer, ByRef pAddr As Long, ByVal pStringBuf As String, ByVal StringBufSize As Integer) As Long | |
Public Function IPアドレス変換_逆変換サンプル() As String | |
Dim WSA As WSAData | |
Dim sName As String | |
Dim RetCode As Long | |
RetCode = WSAStartup(MAKEWORD(2, 2), WSA) | |
If (RetCode <> 0) Then | |
Call WSACleanup | |
Exit Function | |
End If | |
Dim s As String * 128 | |
s = String(100, vbNullChar) | |
Dim s2 As String | |
Dim i As Long | |
'サンプル1 IPネットワークアドレス(文字列)からネットワークアドレスへ | |
i = inet_addr("192.168.10.1") | |
Debug.Print "inet_addr:" & i | |
'サンプル2 ネットワークアドレスからIPネットワークアドレス(文字列)へ変換 | |
Call InetNtopW(AF_INET, i, s, 128) | |
s2 = Replace(s, vbNullChar, "") | |
Debug.Print "IPv4アドレス:" & s2 | |
'サンプル3 IPv6ネットワークアドレスもIPv6ネットワークアドレス(文字列)へ変換可能のようだ | |
Call InetNtopW(AF_INET6, i, s, 128) | |
s2 = Replace(s, vbNullChar, "") | |
Debug.Print "IPv6アドレス:" & s2 | |
Call WSACleanup | |
End Function | |
Public Function MAKEWORD(Lo As Byte, Hi As Byte) As Integer | |
MAKEWORD = Lo + Hi * 256& Or 32768 * (Hi > 127) | |
End Function | |
コメント