`
hereson2
  • 浏览: 466362 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

如何从 Visual Basic 中调用 GetNetworkParams / GetAdapter

F# 
阅读更多
   1. 启动一个新的 Visual Basic 标准 EXE 工程。 默认情况下会创建 Form 1。
   2. 在 项目 菜单中上, 单击 删除 Form 1 。
   3. 在 项目 菜单中上, 单击 添加模块 。 默认情况下会创建 Module 1。
   4. 将以下代码粘贴到 Module 1 的通用声明部分中:

      Public Const MAX_HOSTNAME_LEN = 132
      Public Const MAX_DOMAIN_NAME_LEN = 132
      Public Const MAX_SCOPE_ID_LEN = 260
      Public Const MAX_ADAPTER_NAME_LENGTH = 260
      Public Const MAX_ADAPTER_ADDRESS_LENGTH = 8
      Public Const MAX_ADAPTER_DESCRIPTION_LENGTH = 132
      Public Const ERROR_BUFFER_OVERFLOW = 111
      Public Const MIB_IF_TYPE_ETHERNET = 6
      Public Const MIB_IF_TYPE_TOKENRING = 9
      Public Const MIB_IF_TYPE_FDDI = 15
      Public Const MIB_IF_TYPE_PPP = 23
      Public Const MIB_IF_TYPE_LOOPBACK = 24
      Public Const MIB_IF_TYPE_SLIP = 28

      Type IP_ADDR_STRING
                  Next As Long
                  IpAddress As String * 16
                  IpMask As String * 16
                  Context As Long
      End Type

      Type IP_ADAPTER_INFO
                  Next As Long
                  ComboIndex As Long
                  AdapterName As String * MAX_ADAPTER_NAME_LENGTH
                  Description As String * MAX_ADAPTER_DESCRIPTION_LENGTH
                  AddressLength As Long
                  Address(MAX_ADAPTER_ADDRESS_LENGTH - 1) As Byte
                  Index As Long
                  Type As Long
                  DhcpEnabled As Long
                  CurrentIpAddress As Long
                  IpAddressList As IP_ADDR_STRING
                  GatewayList As IP_ADDR_STRING
                  DhcpServer As IP_ADDR_STRING
                  HaveWins As Byte
                  PrimaryWinsServer As IP_ADDR_STRING
                  SecondaryWinsServer As IP_ADDR_STRING
                  LeaseObtained As Long
                  LeaseExpires As Long
      End Type

      Type FIXED_INFO
                  HostName As String * MAX_HOSTNAME_LEN
                  DomainName As String * MAX_DOMAIN_NAME_LEN
                  CurrentDnsServer As Long
                  DnsServerList As IP_ADDR_STRING
                  NodeType As Long
                  ScopeId  As String * MAX_SCOPE_ID_LEN
                  EnableRouting As Long
                  EnableProxy As Long
                  EnableDns As Long
      End Type

      Public Declare Function GetNetworkParams Lib "IPHlpApi.dll" _
             (FixedInfo As Any, pOutBufLen As Long) As Long
      Public Declare Function GetAdaptersInfo Lib "IPHlpApi.dll" _
             (IpAdapterInfo As Any, pOutBufLen As Long) As Long
      Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
             (Destination As Any, Source As Any, ByVal Length As Long)

      Sub main()
          Dim error As Long
          Dim FixedInfoSize As Long
          Dim AdapterInfoSize As Long
          Dim i As Integer
          Dim PhysicalAddress  As String
          Dim NewTime As Date
          Dim AdapterInfo As IP_ADAPTER_INFO
          Dim AddrStr As IP_ADDR_STRING
          Dim FixedInfo As FIXED_INFO
          Dim Buffer As IP_ADDR_STRING
          Dim pAddrStr As Long
          Dim pAdapt As Long
          Dim Buffer2 As IP_ADAPTER_INFO
          Dim FixedInfoBuffer() As Byte
          Dim AdapterInfoBuffer() As Byte
         
          ' Get the main IP configuration information for this machine
          ' using a FIXED_INFO structure.
          FixedInfoSize = 0
          error = GetNetworkParams(ByVal 0&, FixedInfoSize)
          If error <> 0 Then
              If error <> ERROR_BUFFER_OVERFLOW Then
                 MsgBox "GetNetworkParams sizing failed with error " & error
                 Exit Sub
              End If
          End If
          ReDim FixedInfoBuffer(FixedInfoSize - 1)
         
          error = GetNetworkParams(FixedInfoBuffer(0), FixedInfoSize)
          If error = 0 Then
                  CopyMemory FixedInfo, FixedInfoBuffer(0), FixedInfoSize
                  MsgBox "Host Name:  " & FixedInfo.HostName
                  MsgBox "DNS Servers:  " & FixedInfo.DnsServerList.IpAddress
                  pAddrStr = FixedInfo.DnsServerList.Next
                  Do While pAddrStr <> 0
                        CopyMemory Buffer, ByVal pAddrStr, LenB(Buffer)
                        MsgBox "DNS Servers:  " & Buffer.IpAddress
                        pAddrStr = Buffer.Next
                  Loop
                 
                  Select Case FixedInfo.NodeType
                             Case 1
                                        MsgBox "Node type: Broadcast"
                             Case 2
                                        MsgBox "Node type: Peer to peer"
                             Case 4
                                        MsgBox "Node type: Mixed"
                             Case 8
                                        MsgBox "Node type: Hybrid"
                             Case Else
                                        MsgBox "Unknown node type"
                  End Select
                 
                  MsgBox "NetBIOS Scope ID:  " & FixedInfo.ScopeId
                  If FixedInfo.EnableRouting Then
                             MsgBox "IP Routing Enabled "
                  Else
                             MsgBox "IP Routing not enabled"
                  End If
                  If FixedInfo.EnableProxy Then
                             MsgBox "WINS Proxy Enabled "
                  Else
                             MsgBox "WINS Proxy not Enabled "
                  End If
                  If FixedInfo.EnableDns Then
                            MsgBox "NetBIOS Resolution Uses DNS "
                  Else
                            MsgBox "NetBIOS Resolution Does not use DNS  "
                  End If
          Else
                  MsgBox "GetNetworkParams failed with error " & error
                  Exit Sub
          End If
         
          ' Enumerate all of the adapter specific information using the
          ' IP_ADAPTER_INFO structure.
          ' Note:  IP_ADAPTER_INFO contains a linked list of adapter entries.
         
          AdapterInfoSize = 0
          error = GetAdaptersInfo(ByVal 0&, AdapterInfoSize)
          If error <> 0 Then
              If error <> ERROR_BUFFER_OVERFLOW Then
                 MsgBox "GetAdaptersInfo sizing failed with error " & error
                 Exit Sub
              End If
          End If
          ReDim AdapterInfoBuffer(AdapterInfoSize - 1)

          ' Get actual adapter information
          error = GetAdaptersInfo(AdapterInfoBuffer(0), AdapterInfoSize)
          If error <> 0 Then
             MsgBox "GetAdaptersInfo failed with error " & error
             Exit Sub
          End If
        
          ' Allocate memory
           CopyMemory AdapterInfo, AdapterInfoBuffer(0), AdapterInfoSize
          pAdapt = AdapterInfo.Next

          Do
           CopyMemory Buffer2, AdapterInfo, AdapterInfoSize
             Select Case Buffer2.Type
                    Case MIB_IF_TYPE_ETHERNET
                         MsgBox "Adapter name: Ethernet adapter "
                    Case MIB_IF_TYPE_TOKENRING
                         MsgBox "Adapter name: Token Ring adapter "
                    Case MIB_IF_TYPE_FDDI
                         MsgBox "Adapter name: FDDI adapter "
                    Case MIB_IF_TYPE_PPP
                         MsgBox "Adapter name: PPP adapter"
                    Case MIB_IF_TYPE_LOOPBACK
                         MsgBox "Adapter name: Loopback adapter "
                    Case MIB_IF_TYPE_SLIP
                         MsgBox "Adapter name: Slip adapter "
                    Case Else
                         MsgBox "Adapter name: Other adapter "
             End Select
             MsgBox "AdapterDescription: " & Buffer2.Description

             PhysicalAddress = ""
             For i = 0 To Buffer2.AddressLength - 1
                 PhysicalAddress = PhysicalAddress & Hex(Buffer2.Address(i))
                 If i < Buffer2.AddressLength - 1 Then
                    PhysicalAddress = PhysicalAddress & "-"
                 End If
             Next
             MsgBox "Physical Address: " & PhysicalAddress
         
             If Buffer2.DhcpEnabled Then
                MsgBox "DHCP Enabled "
             Else
                MsgBox "DHCP disabled"
             End If

             MsgBox "IP Address: " & Buffer2.IpAddressList.IpAddress
             MsgBox "Subnet Mask: " & Buffer2.IpAddressList.IpMask
             pAddrStr = Buffer2.IpAddressList.Next
             Do While pAddrStr <> 0
                CopyMemory Buffer, Buffer2.IpAddressList, LenB(Buffer)
                MsgBox "IP Address: " & Buffer.IpAddress
                MsgBox "Subnet Mask: " & Buffer.IpMask
                pAddrStr = Buffer.Next
                If pAddrStr <> 0 Then
                   CopyMemory Buffer2.IpAddressList, ByVal pAddrStr, _
                              LenB(Buffer2.IpAddressList)
                End If
             Loop
         
             MsgBox "Default Gateway: " & Buffer2.GatewayList.IpAddress
             pAddrStr = Buffer2.GatewayList.Next
             Do While pAddrStr <> 0
                CopyMemory Buffer, Buffer2.GatewayList, LenB(Buffer)
                MsgBox "IP Address: " & Buffer.IpAddress
                pAddrStr = Buffer.Next
                If pAddrStr <> 0 Then
                   CopyMemory Buffer2.GatewayList, ByVal pAddrStr, _
                              LenB(Buffer2.GatewayList)
                End If
             Loop

             MsgBox "DHCP Server: " & Buffer2.DhcpServer.IpAddress
             MsgBox "Primary WINS Server: " & _
                    Buffer2.PrimaryWinsServer.IpAddress
             MsgBox "Secondary WINS Server: " & _
                    Buffer2.SecondaryWinsServer.IpAddress

             ' Display time.
             NewTime = DateAdd("s", Buffer2.LeaseObtained, #1/1/1970#)
             MsgBox "Lease Obtained: " & _
                    CStr(Format(NewTime, "dddd, mmm d hh:mm:ss yyyy"))
          
             NewTime = DateAdd("s", Buffer2.LeaseExpires, #1/1/1970#)
             MsgBox "Lease Expires :  " & _
                    CStr(Format(NewTime, "dddd, mmm d hh:mm:ss yyyy"))
             pAdapt = Buffer2.Next
             If pAdapt <> 0 Then
                 CopyMemory AdapterInfo, ByVal pAdapt, AdapterInfoSize
              End If
            Loop Until pAdapt = 0
      End Sub
     

   5. 按 F 5 键运行该项目,请单击每个消息框将显示并注意结果上的 确定 。
分享到:
评论

相关推荐

    174.rar_ActiveX/DCOM/ATL_Visual_Basic_

    获取网关通常涉及到对网络配置的系统调用,这在Windows中可能涉及API函数,如`GetNetworkParams`或者`IP Helper` API。修改网关则可能需要更高权限,并且可能涉及更复杂的步骤,如更新注册表或使用管理员级别的网络...

    VBIP.rar_网络编程_Visual_Basic_

    在VBIP.rar_网络编程_Visual_Basic_这个压缩包中,我们主要探讨的是如何使用Visual Basic进行网络编程,特别是获取计算机上网卡的IP地址。这是一个实用的范例程序,适合初学者和有一定经验的开发者参考学习。 首先...

    BasicBanMon.zip_Windows编程_Visual_Basic_

    《使用Visual Basic进行Windows网络监控程序开发》 在Windows编程领域,Visual Basic(VB)以其易学易用的特性,成为许多开发者首选的编程语言。本资料包"BasicBanMon.zip"提供了一个实用的示例,用于监测互联网的...

    MAC_ADDRESS.rar_VB excel_mac

    在这个“MAC_ADDRESS.rar_VB excel_mac”压缩包中,包含了一个VB(Visual Basic)脚本文件“MAC_ADDRESS.bas”和一个图像文件“MAC_ADDRESS.PNG”,它们共同构成了一个利用VB在Excel环境中读取MAC地址的解决方案。...

    VISUAL C++ 6.0 开发网络典型应用实例导航 光盘代码

    综上所述,这个资源提供了Visual C++ 6.0网络编程的全面实例,覆盖了从基础的IP获取到复杂的网络扫描,再到完整的TCP/IP通信,对于学习和提升网络编程技能具有很高的价值。通过这些实例,开发者不仅可以深入理解网络...

    evc_dialog_net.rar_evc_dialog_net_visual c_wince 网络_网络流量_网络流量监控

    2. **调用IPHLPAPI**:使用`GetNetworkParams`函数获取网络接口的信息,然后通过`GetIfTable`或`GetIfEntry2`来获取每个接口的数据传输统计。这些函数返回的结构体包含了接口的发送和接收字节数、数据包数等信息。 ...

    VB获取详细的IP地址信息,包括子网掩码等.rar

    在VB(Visual Basic)编程中,获取详细的IP地址信息,包括子网掩码、默认网关、DNS服务器等网络配置,是常见的需求。这通常涉及到Windows操作系统提供的API函数调用,因为VB自身并不直接提供获取这些信息的内建方法...

    VB获取并修改网关的网络基础类代码.rar

    在VB(Visual Basic)编程中,获取和修改网络设置,如网关地址,涉及到网络编程的基础知识。VB作为Microsoft开发的一种面向对象的编程语言,提供了丰富的API调用来处理网络相关的任务。在这个“VB获取并修改网关的...

    windows api 中文帮助

    VB(Visual Basic)例子则可能包含如何在VB环境中调用这些Windows API函数的代码片段,帮助开发者理解API的使用方法和应用场景。例如,VB中调用API可能需要使用 Declare 函数来导入外部函数,并使用ByVal、ByRef等...

    局域网侦测器

    这些文件是Visual Basic开发环境中项目的基本配置,使得开发者能够恢复到上次离开时的状态。 理解并利用这些源代码,我们可以学习到以下知识点: 1. **网络扫描技术**:如何通过网络层的协议,如ARP或ICMP,来探测...

    易语言网络API取本机名源码

    在易语言中,通过调用网络API(应用程序接口)来获取本机名称是常见的系统操作之一。网络API是操作系统提供的一系列函数,用于执行特定的网络功能,如连接、发送数据、接收数据等。在这个场景中,我们关注的是获取本...

    ActiveX控件OCX中得到网卡地址并增加受信任站点

    这个方法需要调用Windows API函数,如`GetAdaptersInfo`或`GetNetworkParams`,这些函数可以从操作系统中获取网络适配器的信息,包括MAC地址。 ```delphi uses ..., Windows, SysUtils; function GetMACAddress: ...

    vc制作windows任务管理器.visual c++

    在本项目中,"vc制作windows任务管理器"是一个使用Visual C++开发的程序,它旨在模拟或扩展Windows操作系统中的任务管理器功能。这个压缩包包含了一系列的源代码文件,如T-PsKit.cpp以及相关的头文件(.h),这些...

    IPhelper 库调用

    - `GetNetworkParams`:获取网络参数,如子网掩码、默认网关等。 - `AddIPAddress` 和 `DeleteIPAddress`:添加或删除指定的IP地址。 - `CreateIpForwardEntry` 和 `DeleteIpForwardEntry`:添加或删除路由条目。...

    VB.obtain.IP.subnet.mask.information.rar_IP Address VB

    在VB(Visual Basic)编程环境中,获取IP地址和子网掩码信息是常见的网络编程任务。这主要涉及到Windows操作系统中的网络API调用,通过这些调用,VB程序可以与网络接口进行交互,获取网络配置的相关数据。下面我们将...

    在pb获得mac

    这通常涉及系统调用和网络编程API,如Windows的`GetAdaptersInfo`或`GetNetworkParams`函数。 2. **使用.NET组件**:如果你的PowerBuilder应用支持.NET互操作性,可以创建一个.NET组件或引用已有的.NET库,如System...

    vb.rar_VB 监控_vb 网络_监控_监控 VB_网络监控 VB

    标题中的"vb.rar_VB 监控_vb 网络_监控_监控 VB_网络监控 VB"表明这是一个关于VB(Visual Basic)编程语言的压缩包,内容主要涉及网络监控的功能实现。VB是一种由微软公司开发的面向对象的编程语言,广泛应用于...

    网速监控 源码

    在VB.NET中,可以通过P/Invoke(平台调用)技术调用操作系统提供的API函数,如`GetNetworkParams`和`GetPerAdapterInfo`来获取网络状态信息。此外,还可以使用第三方库如`System.Net.NetworkInformation`来监测网络...

    csbandwidthmonitor网络流量监视源码(VB版)

    "csbandwidthmonitor"是一个专门用于网络流量监测的程序,其源码以Visual Basic (VB)编写,为开发者提供了一个直观的例子,展示了如何实现这一功能。 VB是一种面向对象的编程语言,由Microsoft开发,广泛应用于...

    取IP协议信息.rar

    在易语言中,获取IP信息通常需要调用系统提供的网络函数,比如Windows API中的`GetAdaptersInfo`或`GetNetworkParams`函数。这些函数可以返回关于网络适配器的各种信息,包括IP地址、子网掩码、默认网关等。开发者...

Global site tag (gtag.js) - Google Analytics