如何获得路由器名称和如Windows网络选项卡中所示的IP?(在代码中) [英] How to get Router Name & IP as shown in Windows Network Tab? (in Code)

查看:42
本文介绍了如何获得路由器名称和如Windows网络选项卡中所示的IP?(在代码中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,如果转到开始"并单击计算机",然后单击左侧的网络"链接,则会在右侧注意到几个类别,其中一个类别名为网络基础结构",其中类别中,列出了我的路由器,在我的情况下,它是"LINKSYS WAG160N Wireless-N ADSL2 + Gateway" ,当您右键单击并选择属性时,它会列出基本信息,例如内部/网关IP地址,在我的地址是"192.168.1.1"

Basically, if you go to Start and click Computer and then click on the Network link on the left hand side, you'll notice on the right hand side several categories, one of which is titled "Network Infrustructure", in that category, my router is listed, and in my case, it is "LINKSYS WAG160N Wireless-N ADSL2+ Gateway" and when you right-click and select properties, it lists basic info such as internal/gateway IP address, on mine it is "192.168.1.1"

我想知道如何在代码中检索此信息,最好是Windows API,以便我可以使用旧版VB6应用程序调用它.另外,即使您只知道.NET版本或Delphi版本,也请列出并同时在两者中进行编码.但是,我最终寻求的答案是一种通过VB6访问此方法的方法,因此可能是WinAPI调用(将投票并选择此作为答案),但是,如果我找不到一个其他方法,也可以使用其他方法.遗留方法(我会投票赞成).

I would like to know how to retreive this information in code, preferably a Windows API so that i can call it using a legacy VB6 app. Also, even if you just know a .NET version or a Delphi version, please do list that as well as i can code in both. However, the answer i'm ultimately seeking is a method to access this via VB6 so that would probably be a WinAPI call (will upvote & select this as answer), however, other methods are also welcome incase i can't find a legacy method (i will upvote these).

谢谢大家:)

推荐答案

所需的代码比我想要的多.如果有更紧凑的方法,我会对自己感兴趣.

There is a bit more code required than I'd like. If there is a more compact approach I'd be interested myself.

据我了解,Windows通过UPnP获取信息.UPnP充当一种基于UDP的Web服务.它具有怪癖,因为它使用UDP多播,因此很难进行显式编码,但是Windows提供了一个帮助程序库.对于从VB6程序中使用该库而言,它并不是包装得很好"的,但是通过一些技巧,您可以访问其大多数功能.

As I understand this, Windows gets the information via UPnP. UPnP works as a sort of Web Service over UDP. It has quirks because it uses UDP multicasts so it can be dificult to code explicitly, but Windows offers a helper library. This library is not "wrapped" well for use from a VB6 program, but with a few tricks you can get access to most of its functionality.

编写以下示例,以便可以在Win XP以及更高版本的Windows上进行编译.该库的Win XP版本缺少关键的typelib信息,该信息阻止VB6使用其提供的所有内容.在Vista中,此问题已得到纠正,但是对于此应用程序,我们不需要其提供的完整回调功能.如果需要完全访问权限,则可以使用外部typelib,或者可以在Vista或更高版本上进行编译.在Vista上编译的程序可以在XP上正常工作.

The sample below is written so that it can compile on Win XP as well as later versions of Windows. The Win XP version of the library lacks critical typelib info that prevents VB6 from using everything it offers. This was corrected in Vista, however for this application we don't need the full callback capabilities it offers. You could use an external typelib if you needed full access, or you can compile on Vista or later. A progam compiled on Vista works fine on XP.

下面的代码是从我用于VB6服务器中的UPnP NAT端口映射的较大类中抽象出来的.该子集可能会满足您的要求.

The code below is abstracted from a larger Class I use for UPnP NAT port mapping in VB6 servers. This subset may do what you require though.

UPnPNAT.cls

Option Explicit
'Requires reference to:
'
'   UPnP 1.0 Type Library (Control Point)
'

Private Const CONN_SVCTYPEID_URI As String = "urn:schemas-upnp-org:service:WANIPConnection:1"
Private Const CONN_ID_URI As String = "urn:upnp-org:serviceId:WANIPConn1"

Private UDFinder As UPNPLib.UPnPDeviceFinder
Private WithEvents UNCBs As UPnPNATCBs
Private findData As Long
Private blnSuccess As Boolean

Public Event Result(ByVal Success As Boolean, ByVal FriendlyName As String, ByVal IP As String)

Public Sub Fetch()
    blnSuccess = False
    Set UDFinder = New UPNPLib.UPnPDeviceFinder
    Set UNCBs = New UPnPNATCBs
    findData = CallByName(UDFinder, "CreateAsyncFind", VbMethod, CONN_SVCTYPEID_URI, 0, UNCBs)
    UDFinder.StartAsyncFind findData
End Sub

Private Sub UNCBs_DeviceAdded(ByVal Device As UPNPLib.IUPnPDevice)
    Dim Services As UPNPLib.UPnPServices
    Dim Service As UPNPLib.UPnPService
    Dim varInActionArgs, varOutActionArgs
    Dim strFriendlyName As String
    Dim strIP As String

    strFriendlyName = Device.FriendlyName
    On Error Resume Next
    Set Services = Device.Services
    If Err.Number = 0 Then
        On Error GoTo 0
        With Services
            If .Count > 0 Then
                On Error Resume Next
                Set Service = .Item(CONN_ID_URI)
                If Err.Number = 0 Then
                    On Error GoTo 0
                    ReDim varInActionArgs(0 To 0)
                    ReDim varOutActionArgs(0 To 0)
                    Service.InvokeAction "GetExternalIPAddress", _
                                         varInActionArgs, _
                                         varOutActionArgs
                    strIP = varOutActionArgs(0)
                    blnSuccess = True
                Else
                    On Error GoTo 0
                End If
            End If
        End With
    Else
        On Error GoTo 0
    End If

    UDFinder.CancelAsyncFind findData
    RaiseEvent Result(blnSuccess, strFriendlyName, strIP)
    Set UDFinder = Nothing
    Set UNCBs = Nothing
End Sub

Private Sub UNCBs_SearchComplete()
    If Not blnSuccess Then
        RaiseEvent Result(False, "", "")
    End If
End Sub

UPnPNATCBs.cls

Option Explicit

Public Event DeviceAdded(ByVal Device As UPNPLib.IUPnPDevice)
Public Event DeviceRemoved(ByVal UDN As String)
Public Event SearchComplete()

Public Sub IDispatchCallback( _
    ByVal pDevice As Variant, _
    ByVal bstrUDN As Variant, _
    ByVal lType As Variant)
    'NOTE: Must be dispID = 0, i.e. the default method of the class.

    Select Case lType
        Case 0
            RaiseEvent DeviceAdded(pDevice)
        Case 1
            RaiseEvent DeviceRemoved(bstrUDN)
        Case 2
            RaiseEvent SearchComplete
    End Select
End Sub

Form1.frm

Option Explicit

Private WithEvents UN As UPnPNAT

Private Sub Form_Load()
    Set UN = New UPnPNAT
    lblStatus.Caption = "Searching..."
    UN.Fetch
End Sub

Private Sub UN_Result(ByVal Success As Boolean, ByVal FriendlyName As String, ByVal IP As String)
    If Success Then
        lblStatus.Caption = FriendlyName & " " & IP
    Else
        lblStatus.Caption = "Failed"
    End If
End Sub

如果您有多个在网络中提供连接的UPnP设备,则可能需要对此进行一些调整.

You may have to tweak this some if you have multiple UPnP devices providing connections in your network.

这篇关于如何获得路由器名称和如Windows网络选项卡中所示的IP?(在代码中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆