IPアドレスからホスト名を取得 <TOP>
IPアドレスからホスト名を取得します。
WSAStartup WinSockを初期化
WSACleanup WinSock のリソースを解放
inet_addr 文字列を32ビットのバイナリ値に変換
gethostbyaddr IPアドレスをホスト名に変換する関数
RtlMoveMemory メモリブロックのコピーを行うための関数
lstrlen 文字列の長さを調べる
アドレスは、E-Mailのプロパティ → 詳細 → インターネットヘッダー のIPアドレスを入力し結果の確認をしています。
例1 IPアドレスを入力しない状態
例2
例3
例4 適当な数字を入れてみた
参照
<ホスト名からIPアドレスを取得>
'================================================================ '= IPアドレスからホスト名を取得 '= (gethostbyaddr.bas) '================================================================ #include "Windows.bi" #define WSADescription_Len 256 #define WSASYS_Status_Len 128 #define WS_VERSION_REQD &H101 #define IP_SUCCESS 0 #define SOCKET_ERROR -1 #define AF_INET 2 Type WSADATA wVersion As Integer wHighVersion As Integer szDescription(WSADescription_Len) As Byte szSystemStatus(WSASYS_Status_Len) As Byte imaxsockets As Integer imaxudp As Integer lpszvEnderinfo As Long End Type ' WinSockを初期化 Declare Function Api_WSAStartup& Lib "wsock32" Alias "WSAStartup" (ByVal VersionReq&, WSADataReturn As WSADATA) ' WinSock のリソースを解放 Declare Function Api_WSACleanup& Lib "wsock32" Alias "WSACleanup" () ' 文字列を32ビットのバイナリ値に変換 Declare Function Api_inet_addr& Lib "wsock32" Alias "inet_addr" (ByVal s$) ' IP アドレスをホスト名に変換する関数 Declare Function Api_gethostbyaddr& Lib "wsock32" Alias "gethostbyaddr" (haddr&, ByVal hnlen&, ByVal addrtype&) ' メモリブロックのコピーを行うための関数 Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (xDest As Any, xSource As Any, ByVal nbytes&) ' 文字列の長さを調べる Declare Function Api_lstrlen& Lib "kernel32" Alias "lstrlenA" (lpString As Any) Var Shared Edit1 As Object Var Shared Text(2) As Object Var Shared Button1 As Object Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14 For i = 0 To 2 Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1))) Text(i).SetFontSize 14 Next i Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14 '================================================================ '= '================================================================ Declare Function SocketsInitialize () As Integer Function SocketsInitialize() As Integer Var WSAD As WSADATA SocketsInitialize = Api_WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS End Function '================================================================ '= '================================================================ Declare Sub SocketsCleanup () Sub SocketsCleanup() If Api_WSACleanup() <> 0 Then A% = MessageBox("", "CleanUpエラー!", 0, 2) End If End Sub '================================================================ '= '================================================================ Declare Function GetHostNameFromIP(sAddress As String) As String Function GetHostNameFromIP(sAddress As String) As String Var ptrHosent As Long Var hAddress As Long Var nbytes As Long Var Ret As Long If SocketsInitialize() Then hAddress = Api_inet_addr(sAddress) If hAddress <> SOCKET_ERROR Then ptrHosent = Api_gethostbyaddr(hAddress, 4, AF_INET) If ptrHosent <> 0 Then CopyMemory ptrHosent, ByVal ptrHosent, 4 nbytes = Api_lstrLen(ByVal ptrHosent) If nbytes > 0 Then sAddress = Space$(nbytes) CopyMemory sAddress, ByVal ptrHosent, nbytes GetHostNameFromIP = sAddress End If Else A% = MessageBox("", "取得に失敗しました!", 0, 2) End If SocketsCleanup Else A% = MessageBox("", "無効のIPです!", 0, 2) End If Else A% = MessageBox("", "ソケットの初期化失敗!", 0, 2) End If End Function '================================================================ '= '================================================================ Declare Sub Button1_on edecl () Sub Button1_on() Var hName As String Text(2).SetWindowText "" hName = GetHostNameFromIP(GetDlgItemText("Edit1")) Text(2).SetWindowText hName End Sub '================================================================ '= '================================================================ While 1 WaitEvent Wend Stop End