📜  VB获取外部IP地址 - VBA(1)

📅  最后修改于: 2023-12-03 14:48:17.943000             🧑  作者: Mango

VB获取外部IP地址 - VBA

VB,也即Visual Basic,是一种面向对象的基础编程语言,非常适合初学者入门。在VB中,我们可以很容易地获取外部IP地址,以便做一些网络操作。

实现方法

VB中获取外部IP地址的方法非常简单,我们只需要利用一个API,就可以轻松地完成。具体的实现方法如下:

Option Explicit

Private Declare PtrSafe Function InternetGetConnectedState Lib "wininet.dll" (ByRef dwflags As Long, ByVal dwReserved As Long) As Long
Private Declare PtrSafe Function InternetGetConnectedStateExA Lib "wininet.dll" (ByRef lpdwConnectionFlags As Long, ByVal lpszConnectionName As Any, ByVal dwNameLen As Long, ByVal dwReserved As Long) As Long
Private Declare PtrSafe Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoW" _
    (lpdwiStatCode As Long, ByVal lpBuffer As Long, lpdwBufferLength As Long) As Boolean
Private Declare PtrSafe Function InternetQueryOptionA Lib "wininet.dll" (ByVal hInternet As LongPtr, ByVal dwOption As Long, ByRef lpBuffer As Any, ByRef lpdwBufferLength As Long) As Long

Private Const INTERNET_OPTION_PER_CONNECTION_OPTION As Long = 75
Private Const INTERNET_OPTION_REFRESH As Long = 37
Private Const INTERNET_OPTION_PROXY_SETTINGS_CHANGED As Long = 95
Private Const INTERNET_OPTION_SETTINGS_CHANGED As Long = 39

Private Type INTERNET_PER_CONN_OPTION
    dwOption As Long
    dwValue As Long
End Type

Private Type INTERNET_PER_CONN_OPTION_LIST
    dwSize As Long
    pszConnection As LongPtr
    dwOptionCount As Long
    dwOptionError As Long
    dwOptions As LongPtr
End Type

Public Function GetExternalIP() As String
    Dim request As New MSXML2.XMLHTTP60
    Dim response As String
    Dim ip As String
    Dim dwSettings As Long
    Dim perConnOptions(2) As INTERNET_PER_CONN_OPTION
    Dim optionsList As INTERNET_PER_CONN_OPTION_LIST
    Dim bufferSize As Long
    Dim connectionFlags As Long
    Dim connectionName As String
    Dim url As String
    
    '获取默认网络连接设置
    optionsList.dwSize = Len(optionsList)
    optionsList.pszConnection = 0
    optionsList.dwOptionCount = 3
    optionsList.dwOptionError = 0
    optionsList.dwOptions = VarPtr(perConnOptions(0))
    
    ReDim perConnOptions(2) As INTERNET_PER_CONN_OPTION
    
    '设置自动配置脚本
    perConnOptions(0).dwOption = 39
    perConnOptions(0).dwValue = VarPtr("http://localhost:8080/proxy.pac")
    
    '获取连接信息
    dwSettings = INTERNET_OPTION_PER_CONNECTION_OPTION
    
    InternetQueryOptionA 0, dwSettings, optionsList, Len(optionsList)
    
    '更新设置
    InternetSetOptionA 0, INTERNET_OPTION_PER_CONNECTION_OPTION, optionsList, Len(optionsList)
    InternetSetOptionA 0, INTERNET_OPTION_PROXY_SETTINGS_CHANGED, ByVal 0&, 0&
    InternetSetOptionA 0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0&
    
    '检查网络是否连接
    InternetGetConnectedStateExA connectionFlags, vbNullString, 0, 0
    If (connectionFlags And 1) Then
        '获取外部ip地址
        url = "https://icanhazip.com"
        request.Open "GET", url, False
        request.send
        response = request.responseText
        ip = response
    End If
    
    '还原连接设置
    optionsList.dwOptionCount = 1
    perConnOptions(0).dwValue = 0
    
    InternetSetOptionA 0, INTERNET_OPTION_PER_CONNECTION_OPTION, optionsList, Len(optionsList)
    InternetSetOptionA 0, INTERNET_OPTION_PROXY_SETTINGS_CHANGED, ByVal 0&, 0&
    InternetSetOptionA 0, INTERNET_OPTION_SETTINGS_CHANGED, ByVal 0&, 0&
    
    GetExternalIP = ip
End Function
使用方法

以上的代码已经实现了获取外部ip地址的功能,我们只需要按以下步骤调用函数即可:

  1. 在VB中打开一个新的工程;
  2. 按“ALT-F11”打开VB编辑器;
  3. 在VB编辑器中选择“插入”->“模块”;
  4. 将以上的函数复制粘贴到模块中;
  5. 在主程序中调用GetExternalIP函数即可。
注意事项
  • 以上的代码需要在Windows操作系统中运行,因为它利用了Windows的API来获取外部ip地址;
  • 在运行以上代码时,我们需要确保网络连接是畅通的,否则将无法获取外部ip地址。
参考文献