Results 1 to 4 of 4

Thread: Ethernet Adapter Address

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2002
    Posts
    49

    Ethernet Adapter Address

    Hi,

    I write a software in Visual Basic and I would like to get the following info from my computer:

    1. Ethernet Addapter address
    2. IP Address
    3. Netmask
    4. Default Gateway
    5. Host name of PC

    Could you please send an example code that can do the above?
    Thanks a lot for your help.
    H.

  2. #2
    Hyperactive Member abhid's Avatar
    Join Date
    Nov 2001
    Location
    3rd rock from the sun
    Posts
    467
    hi,
    you can use following function for this purpose. I got this example in API guide from www.allapi.net

    VB Code:
    1. 'Paste this code into a module and set the Startup Object
    2. 'to 'Sub Main'
    3. '(To set the startup object, go to
    4. 'Project->Project Properties->General Tab->Startup Object)
    5.  
    6. Public Const MAX_HOSTNAME_LEN = 132
    7. Public Const MAX_DOMAIN_NAME_LEN = 132
    8. Public Const MAX_SCOPE_ID_LEN = 260
    9. Public Const MAX_ADAPTER_NAME_LENGTH = 260
    10. Public Const MAX_ADAPTER_ADDRESS_LENGTH = 8
    11. Public Const MAX_ADAPTER_DESCRIPTION_LENGTH = 132
    12. Public Const ERROR_BUFFER_OVERFLOW = 111
    13. Public Const MIB_IF_TYPE_ETHERNET = 1
    14. Public Const MIB_IF_TYPE_TOKENRING = 2
    15. Public Const MIB_IF_TYPE_FDDI = 3
    16. Public Const MIB_IF_TYPE_PPP = 4
    17. Public Const MIB_IF_TYPE_LOOPBACK = 5
    18. Public Const MIB_IF_TYPE_SLIP = 6
    19.  
    20. Type IP_ADDR_STRING
    21.     Next As Long
    22.     IpAddress As String * 16
    23.     IpMask As String * 16
    24.     Context As Long
    25. End Type
    26.  
    27. Type IP_ADAPTER_INFO
    28.     Next As Long
    29.     ComboIndex As Long
    30.     AdapterName As String * MAX_ADAPTER_NAME_LENGTH
    31.     Description As String * MAX_ADAPTER_DESCRIPTION_LENGTH
    32.     AddressLength As Long
    33.     Address(MAX_ADAPTER_ADDRESS_LENGTH - 1) As Byte
    34.     Index As Long
    35.     Type As Long
    36.     DhcpEnabled As Long
    37.     CurrentIpAddress As Long
    38.     IpAddressList As IP_ADDR_STRING
    39.     GatewayList As IP_ADDR_STRING
    40.     DhcpServer As IP_ADDR_STRING
    41.     HaveWins As Boolean
    42.     PrimaryWinsServer As IP_ADDR_STRING
    43.     SecondaryWinsServer As IP_ADDR_STRING
    44.     LeaseObtained As Long
    45.     LeaseExpires As Long
    46. End Type
    47.  
    48. Type FIXED_INFO
    49.     HostName As String * MAX_HOSTNAME_LEN
    50.     DomainName As String * MAX_DOMAIN_NAME_LEN
    51.     CurrentDnsServer As Long
    52.     DnsServerList As IP_ADDR_STRING
    53.     NodeType As Long
    54.     ScopeId  As String * MAX_SCOPE_ID_LEN
    55.     EnableRouting As Long
    56.     EnableProxy As Long
    57.     EnableDns As Long
    58. End Type
    59.  
    60. Public Declare Function GetNetworkParams Lib "IPHlpApi" (FixedInfo As Any, pOutBufLen As Long) As Long
    61. Public Declare Function GetAdaptersInfo Lib "IPHlpApi" (IpAdapterInfo As Any, pOutBufLen As Long) As Long
    62. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    63. Sub main()
    64.     'This example was created by George Bernier ([email protected])
    65.     Dim error As Long
    66.     Dim FixedInfoSize As Long
    67.     Dim AdapterInfoSize As Long
    68.     Dim i As Integer
    69.     Dim PhysicalAddress  As String
    70.     Dim NewTime As Date
    71.     Dim AdapterInfo As IP_ADAPTER_INFO
    72.     Dim Adapt As IP_ADAPTER_INFO
    73.     Dim AddrStr As IP_ADDR_STRING
    74.     Dim FixedInfo As FIXED_INFO
    75.     Dim Buffer As IP_ADDR_STRING
    76.     Dim pAddrStr As Long
    77.     Dim pAdapt As Long
    78.     Dim Buffer2 As IP_ADAPTER_INFO
    79.     Dim FixedInfoBuffer() As Byte
    80.     Dim AdapterInfoBuffer() As Byte
    81.    
    82.     'Get the main IP configuration information for this machine using a FIXED_INFO structure
    83.     FixedInfoSize = 0
    84.     error = GetNetworkParams(ByVal 0&, FixedInfoSize)
    85.     If error <> 0 Then
    86.         If error <> ERROR_BUFFER_OVERFLOW Then
    87.            MsgBox "GetNetworkParams sizing failed with error " & error
    88.            Exit Sub
    89.         End If
    90.     End If
    91.     ReDim FixedInfoBuffer(FixedInfoSize - 1)
    92.  
    93.     error = GetNetworkParams(FixedInfoBuffer(0), FixedInfoSize)
    94.     If error = 0 Then
    95.             CopyMemory FixedInfo, FixedInfoBuffer(0), Len(FixedInfo)
    96.             MsgBox "Host Name:  " & FixedInfo.HostName 'host name
    97.             MsgBox "DNS Servers:  " & FixedInfo.DnsServerList.IpAddress 'dns server IP
    98.             pAddrStr = FixedInfo.DnsServerList.Next
    99.             Do While pAddrStr <> 0
    100.                   CopyMemory Buffer, ByVal pAddrStr, Len(Buffer)
    101.                   MsgBox "DNS Servers:  " & Buffer.IpAddress 'dns server IP
    102.                   pAddrStr = Buffer.Next
    103.             Loop
    104.            
    105.             Select Case FixedInfo.NodeType 'node type
    106.                        Case 1
    107.                                   MsgBox "Node type: Broadcast"
    108.                        Case 2
    109.                                    MsgBox "Node type: Peer to peer"
    110.                        Case 4
    111.                                     MsgBox "Node type: Mixed"
    112.                        Case 8
    113.                                     MsgBox "Node type: Hybrid"
    114.                        Case Else
    115.                                     MsgBox "Unknown node type"
    116.             End Select
    117.            
    118.             MsgBox "NetBIOS Scope ID:  " & FixedInfo.ScopeId 'scope ID
    119.             'routing
    120.             If FixedInfo.EnableRouting Then
    121.                        MsgBox "IP Routing Enabled "
    122.             Else
    123.                        MsgBox "IP Routing not enabled"
    124.             End If
    125.             ' proxy
    126.             If FixedInfo.EnableProxy Then
    127.                        MsgBox "WINS Proxy Enabled "
    128.             Else
    129.                        MsgBox "WINS Proxy not Enabled "
    130.             End If
    131.             ' netbios
    132.             If FixedInfo.EnableDns Then
    133.                       MsgBox "NetBIOS Resolution Uses DNS "
    134.             Else
    135.                       MsgBox "NetBIOS Resolution Does not use DNS  "
    136.             End If
    137.     Else
    138.             MsgBox "GetNetworkParams failed with error " & error
    139.             Exit Sub
    140.     End If
    141.    
    142.     'Enumerate all of the adapter specific information using the IP_ADAPTER_INFO structure.
    143.     'Note:  IP_ADAPTER_INFO contains a linked list of adapter entries.
    144.    
    145.     AdapterInfoSize = 0
    146.     error = GetAdaptersInfo(ByVal 0&, AdapterInfoSize)
    147.     If error <> 0 Then
    148.         If error <> ERROR_BUFFER_OVERFLOW Then
    149.            MsgBox "GetAdaptersInfo sizing failed with error " & error
    150.            Exit Sub
    151.         End If
    152.     End If
    153.    ReDim AdapterInfoBuffer(AdapterInfoSize - 1)
    154.  
    155.  ' Get actual adapter information
    156.    error = GetAdaptersInfo(AdapterInfoBuffer(0), AdapterInfoSize)
    157.    If error <> 0 Then
    158.       MsgBox "GetAdaptersInfo failed with error " & error
    159.       Exit Sub
    160.    End If
    161.    CopyMemory AdapterInfo, AdapterInfoBuffer(0), Len(AdapterInfo)
    162.    pAdapt = AdapterInfo.Next
    163.  
    164.    Do While pAdapt <> 0
    165.         CopyMemory Buffer2, AdapterInfo, Len(Buffer2)
    166.           Select Case Buffer2.Type
    167.                 Case MIB_IF_TYPE_ETHERNET
    168.                     MsgBox "Ethernet adapter "
    169.                 Case MIB_IF_TYPE_TOKENRING
    170.                     MsgBox "Token Ring adapter "
    171.                 Case MIB_IF_TYPE_FDDI
    172.                     MsgBox "FDDI adapter "
    173.                 Case MIB_IF_TYPE_PPP
    174.                     MsgBox "PPP adapter"
    175.                 Case MIB_IF_TYPE_LOOPBACK
    176.                     MsgBox "Loopback adapter "
    177.                 Case MIB_IF_TYPE_SLIP
    178.                     MsgBox "Slip adapter "
    179.                 Case Else
    180.                     MsgBox "Other adapter "
    181.         End Select
    182.     MsgBox " AdapterName: " & Buffer2.AdapterName
    183.     MsgBox "AdapterDescription: " & Buffer2.Description 'adatpter name
    184.  
    185.     For i = 0 To Buffer2.AddressLength - 1
    186.            PhysicalAddress = PhysicalAddress & Hex(Buffer2.Address(i))
    187.             If i < Buffer2.AddressLength - 1 Then
    188.              PhysicalAddress = PhysicalAddress & "-"
    189.             End If
    190.  
    191.     Next
    192.     MsgBox "Physical Address: " & PhysicalAddress 'mac address
    193.     If Buffer2.DhcpEnabled Then
    194.             MsgBox "DHCP Enabled "
    195.     Else
    196.             MsgBox "DHCP disabled"
    197.     End If
    198.  
    199.     pAddrStr = Buffer2.IpAddressList.Next
    200.     Do While pAddrStr <> 0
    201.            CopyMemory Buffer, Buffer2.IpAddressList, LenB(Buffer)
    202.            MsgBox "IP Address: " & Buffer.IpAddress
    203.            MsgBox "Subnet Mask: " & Buffer.IpMask
    204.            pAddrStr = Buffer.Next
    205.            If pAddrStr <> 0 Then
    206.             CopyMemory Buffer2.IpAddressList, ByVal pAddrStr, Len(Buffer2.IpAddressList)
    207.            End If
    208.    Loop
    209.     MsgBox "Default Gateway: " & Buffer2.GatewayList.IpAddress
    210.     pAddrStr = Buffer2.GatewayList.Next
    211.     Do While pAddrStr <> 0
    212.             CopyMemory Buffer, Buffer2.GatewayList, Len(Buffer)
    213.             MsgBox "IP Address: " & Buffer.IpAddress
    214.             pAddrStr = Buffer.Next
    215.             If pAddrStr <> 0 Then
    216.             CopyMemory Buffer2.GatewayList, ByVal pAddrStr, Len(Buffer2.GatewayList)
    217.             End If
    218.     Loop
    219.  
    220.     MsgBox "DHCP Server: " & Buffer2.DhcpServer.IpAddress
    221.     MsgBox "Primary WINS Server: " & Buffer2.PrimaryWinsServer.IpAddress
    222.     MsgBox "Secondary WINS Server: " & Buffer2.SecondaryWinsServer.IpAddress
    223.  
    224.     ' Display time
    225.     NewTime = CDate(Adapt.LeaseObtained)
    226.     MsgBox "Lease Obtained: " & CStr(NewTime)
    227.  
    228.     NewTime = CDate(Adapt.LeaseExpires)
    229.     MsgBox "Lease Expires :  " & CStr(NewTime)
    230.     pAdapt = Buffer2.Next
    231.     If pAdapt <> 0 Then
    232.         CopyMemory AdapterInfo, ByVal pAdapt, Len(AdapterInfo)
    233.     End If
    234.  
    235.    Loop
    236.    
    237. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2002
    Posts
    49
    Hi,

    Thanks for the info. Is there any way I can make a unique serail number for a computer so that I can at least make harder the way people can hack my software? I would like to make a key which is unique for each computer.

    Thanks.
    H.

  4. #4
    Hyperactive Member abhid's Avatar
    Join Date
    Nov 2001
    Location
    3rd rock from the sun
    Posts
    467
    seems you are on correct track.
    you can take help of MAC address (if that PC has a network card) and apply some algorithm on it to generate a hash or unique number.
    There are lot of thread on forum regarding same issue. you can have lot of ideas to get it done.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width