• VB6获取本机所有IP地址公用函数


    因为正好有用到,整了个公用函数,一次获取本机全部IP地址:

    Option Explicit
    
    Private Declare Function WSAstartup Lib "WSOCK32.DLL" Alias "WSAStartup" (ByVal wVersionRequired As Integer, ByRef lpWSAData As WSAdata) As Long
    Private Declare Function WsACleanup Lib "WSOCK32.DLL" Alias "WSACleanup" () As Long
    Private 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
    Private Declare Function socket Lib "WSOCK32.DLL" (ByVal af As Long, ByVal s_type As Long, ByVal protocol As Long) As Long
    Private Declare Function closesocket Lib "WSOCK32.DLL" (ByVal s As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Private Type sockaddr
        sin_family As Integer
        sin_port As Integer
        sin_addr As Long
        sin_zero As String * 8
    End Type
    
    Private Type sockaddr_gen
        AddressIn As sockaddr
        filler(0 To 7) As Byte
    End Type
    
    Private Type INTERFACE_INFO
        iiFlags As Long
        iiAddress As sockaddr_gen
        iiBroadcastAddress As sockaddr_gen
        iiNetmask As sockaddr_gen
    End Type
    
    Private Type INTERFACEINFO
        iInfo(0 To 7) As INTERFACE_INFO
    End Type
    
    Private Type WSAdata
        wVersion As Integer
        wHighVersion As Integer
        szDescription As String * 255
        szSystemStatus As String * 128
        iMaxSockets As Integer
        iMaxUdpDg As Integer
        lpVendorInfo As Long
    End Type
    
    Private Const AF_INET = 2
    Private Const SOCK_STREAM = 1
    Private Const INVALID_SOCKET = -1
    Private Const SIO_GET_INTERFACE_LIST As Long = &H4004747F
    
    Private Function GetStrIPFromLong(nIP As Long) As String
        On Error Resume Next
        Dim btBuffer(3) As Byte
        Call CopyMemory(ByVal VarPtr(btBuffer(0)), ByVal VarPtr(nIP), 4)
        Let GetStrIPFromLong = btBuffer(0) & "." & btBuffer(1) & "." & btBuffer(2) & "." & btBuffer(3)
    End Function
    
    Public Function EnumLocalIpAddress() As String()
        On Error GoTo Z
        Dim lngSocketHandle       As Long
        Dim lngBytesReturned      As Long
        Dim tpBuffer              As INTERFACEINFO
        Dim nNumInterfaces        As Integer
        Dim i                     As Integer
        Dim StartupInfo           As WSAdata
        Dim strIPBuffer()         As String
    
        If WSAstartup(&H202, StartupInfo) <> 0 Then
           Exit Function
        End If
        
        lngSocketHandle = socket(AF_INET, SOCK_STREAM, 0)
        If lngSocketHandle = INVALID_SOCKET Then
           Exit Function
        End If
        
        If WSAIoctl(lngSocketHandle, SIO_GET_INTERFACE_LIST, ByVal 0, ByVal 0, tpBuffer, 1024, lngBytesReturned, ByVal 0, ByVal 0) Then
           closesocket lngSocketHandle
           Exit Function
        End If
    
        nNumInterfaces = CInt(lngBytesReturned / 76)
        
        ReDim strIPBuffer(nNumInterfaces - 1)
        
        For i = 0 To nNumInterfaces - 1
            strIPBuffer(i) = GetStrIPFromLong(tpBuffer.iInfo(i).iiAddress.AddressIn.sin_addr)
        Next i
        
        EnumLocalIpAddress = strIPBuffer
        closesocket lngSocketHandle
        WsACleanup
        Exit Function
    Z:
    End Function
    
    


    使用很简单:

        Dim i As Long, strIPAdd() As String
        strIPAdd = EnumLocalIpAddress
        If SafeArrayGetDim(strIPAdd) > 0 Then
           For i = 0 To UBound(strIPAdd)
               Debug.Print strIPAdd(i)
           Next
        End If
    


     

  • 相关阅读:
    Cocos2d-x 3.0 事件系统【转】
    cocos2d-x中false,setSwallowTouches,stopPropagation的区别
    类成员函数指针 ->*语法剖析
    cocos2d-lua 3.5 ios搭建步骤
    cocos2d-lua 3.5 android搭建步骤
    cocos2d-lua 3.5 android搭建常见错误
    结构体
    乒乓球(0)<P2003_1>
    不高兴的津津(0)<P2004_1>
    陶陶摘苹果(0)<P2005_1>
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/2980722.html
Copyright © 2020-2023  润新知