IPアドレス取得(U)          <TOP>


IPアドレスを取得します。
MoveMemory メモリー領域をコピー 
gethostname ローカルマシンのホスト名を取得 
gethostbyname インターネットホスト名に対するIPアドレスを取得 
WSAStartup WinSockを初期化 
WSACleanup WinSockのリソースを解放 
WSAGetLastError WinSockのエラーコードを取得 
 
 
'================================================================
'= IPAddress取得
'=    (GetIpAddress.bas)
'================================================================
Type WSADATA
    wVersion       As Integer
    wHighVersion   As Integer
    szDescription  As String * 257
    szSystemStatus As String * 129
    iMaxSockets    As Integer
    iMaxUdpDg      As Integer
    lpVendorInfo   As Long
End Type

Type hostent
    h_name      As Long
    h_Aliases   As Long
    h_addrType  As Integer
    h_length    As Integer
    h_addr_list As Long
End Type

' メモリの指定領域をコピー
Declare Sub MoveMemory1 Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, ByVal Source&, ByVal length&)

' メモリの指定領域をコピー
Declare Sub MoveMemory2 Lib "kernel32" Alias "RtlMoveMemory" (Dest&, ByVal Source&, ByVal length&)

' ローカルマシンのホスト名を取得
Declare Function Api_gethostname& Lib "wsock32" Alias "gethostname" (ByVal Name$, ByVal namelen&)

' インターネットホスト名に対応するIPアドレスを取得
Declare Function Api_gethostbyname& Lib "wsock32" Alias "gethostbyname" (ByVal HostName$)

' WinSockを初期化
Declare Function Api_WSAStartup& Lib "wsock32" Alias "WSAStartup" (ByVal wVersionRequested%, lpWSAData As Any)

' WinSock のリソースを解放
Declare Function Api_WSACleanup& Lib "wsock32" Alias "WSACleanup" ()

' WinSockのエラーコードを取得
Declare Function Api_WSAGetLastError& Lib "wsock32" Alias "WSAGetLastError" ()

'================================================================
'=
'================================================================
Declare Function GetIpAddress() As String
Function GetIpAddress() As String
    Var wsa As WSADATA
    Var sName As String * 65
    Var he As hostent
    Var p As Long
    Var b(3) As byte
    Var Ret As Long

    If Api_WSAStartup(&H101, wsa) <> 0 Then exit Function

    If Api_gethostname(sName, 64) = 0 Then
        p = Api_gethostbyname(sName)
        If p <> 0 Then
            MoveMemory1 he, p, 16
            MoveMemory2 p, he.h_addr_list, 4
            MoveMemory1 b(0), p, 4
            GetIpAddress = Trim$(Str$(b(0))) & "." & Trim$(Str$(b(1))) & "." & Trim$(Str$(b(2))) & "." & Trim$(Str$(b(3)))
        End If
    End If

    Ret = Api_WSACleanup
End Function
'----------------------------------------------------------------

Print GetIpAddress

Stop
End