Re: 今度はサブネットマスクを。。


[コメントツリー表示を見る] [発言時刻順表示を見る]

Posted by Uz on 2001/06/15 16:45:00

In Reply to: 今度はサブネットマスクを。。 Posted by han on 2001/06/14 20:47:23


    > 今度は、サブネットマスクの取得の方法を教えてください。
    > IPアドレスを取得することはできたのですが、
    > サブネットマスクを取得できないんです。
    > 環境は、Win98SEのVB6.0 SP4です。
    > お願いします。

    以下のコードをお試しあれ。


    Option Explicit

    Private Const WS_VERSION_REQD = &H202
    Private Const WSADescription_Len = 256
    Private Const WSASYS_Status_Len = 128

    Private Const AF_INET As Long = 2
    Private Const SOCK_DGRAM As Long = 2
    Private Const IPPROTO_UDP As Long = 17
    Private Const SIO_GET_INTERFACE_LIST As Long = 1074033791

    Private Type WSADATA
    wversion As Integer
    wHighVersion As Integer
    szDescription(0 To WSADescription_Len) As Byte
    szSystemStatus(0 To WSASYS_Status_Len) As Byte
    iMaxSockets As Integer
    iMaxUdpDg As Integer
    lpszVendorInfo As Long
    End Type

    Private Type sockaddr
    sa_family As Long '; /* address family */
    sa_data(19) As Byte '; /* up to 14 bytes of direct address */
    End Type

    Private Type INTERFACE_INFO
    iiFlags As Long '; /* Interface flags */
    iiAddress As sockaddr '; /* Interface address */
    iiBroadcastAddress As sockaddr '; /* Broadcast address */
    iiNetmask As sockaddr '; /* Network mask */
    End Type

    Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long
    Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired&, lpWSAData As WSADATA) As Long
    Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long

    ' ソケットを作成する関数の宣言
    Private Declare Function WSASocket Lib "ws2_32.dll" Alias "WSASocketA" ( _
    ByVal af As Long, _
    ByVal lngType As Long, _
    ByVal protocol As Long, _
    lpProtocolInfo As Any, _
    ByVal g As Long, _
    ByVal dwFlags As Long _
    ) As Long



    ' ソケットのモードを制御する関数の宣言
    Declare Function WSAIoctl Lib "ws2_32.dll" ( _
    ByVal s As Long, _
    ByVal dwIoControlCode As Long, _
    lpvInBuffer As Any, _
    ByVal cbInBuffer As Long, _
    lpvOutBuffer As Any, _
    ByVal cbOutBuffer As Long, _
    lpcbBytesReturned As Long, _
    lpOverlapped As Long, _
    lpCompletionRoutine As Long _
    ) As Long

    ' ソケットをクローズする関数の宣言
    Declare Function closesocket Lib "ws2_32.dll" ( _
    ByVal s As Long _
    ) As Long

    Public Function GetIPInfo() As String
    Dim WSAD As WSADATA
    Dim nSock As Long
    Dim bytBuf(2048) As Byte
    Dim nBytesReturned As Long

    Dim iinfo(10) As INTERFACE_INFO
    Dim I As Long

    Call WSAStartup(WS_VERSION_REQD, WSAD)

    nSock = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, ByVal CLng(0), 0, 0)

    Call WSAIoctl(nSock, SIO_GET_INTERFACE_LIST, ByVal CLng(0), 0, iinfo(0), _
    Len(iinfo(0)) * 10, nBytesReturned, ByVal CLng(0), ByVal CLng(0))

    For I = 0 To (nBytesReturned \ Len(iinfo(0))) - 1
    Debug.Print "IP Address : " & iinfo(I).iiAddress.sa_data(0) & "." & iinfo(I).iiAddress.sa_data(1) & "." & iinfo(I).iiAddress.sa_data(2) & "." & iinfo(I).iiAddress.sa_data(3)
    Debug.Print "SubNetAddress : " & iinfo(I).iiNetmask.sa_data(0) & "." & iinfo(I).iiNetmask.sa_data(1) & "." & iinfo(I).iiNetmask.sa_data(2) & "." & iinfo(I).iiNetmask.sa_data(3)
    Debug.Print
    Next

    Call closesocket(nSock)

    Call WSACleanup
    End Function



記事スレッド一覧