| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 967 人关注过本帖, 1 人收藏
标题:怎样根据操作系统版本自动获取IP地址
只看楼主 加入收藏
lsll
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-10-26
收藏(1)
 问题点数:0 回复次数:1 
怎样根据操作系统版本自动获取IP地址
我写了如下函数来获取IP地址,这个在windows xp下正常,但在windows7下面获取的却是IPv6,所以我想如果是windows7,获取的IP是IPv4,而不是6,请各位指教,谢谢!
    Set objWMI = GetObject("winmgmts://" & strComputer & "/root/cimv2")
    Set colIP = objWMI.ExecQuery

[ 本帖最后由 lsll 于 2011-10-31 07:41 编辑 ]
搜索更多相关主题的帖子: 操作系统 windows7 IP地址 root 
2011-10-26 15:46
lsll
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-10-26
收藏
得分:0 
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, pdwSize As Long, ByVal Sort As Long) As Long


Private Const MAX_IP = 255

Private Type IPINFO
            dwAddr   As Long                        'ip地址·
            dwIndex   As Long
            dwMask   As Long                        '子网掩码
            dwBCastAddr   As Long                   '广播地址·
            dwReasmSize   As Long
            unused1   As Integer
            unused2   As Integer
End Type
Private Type MIB_IPADDRTABLE
            dEntrys   As Long
            mIPInfo(MAX_IP)   As IPINFO
End Type
Private Type IP_Array
            mBuffer   As MIB_IPADDRTABLE
            BufferLen   As Long
End Type
  
Dim strIP     As String

Private Function ConvertAddressToString(longAddr As Long) As String
            Dim myByte(3)     As Byte
            Dim Cnt     As Long
            CopyMemory myByte(0), longAddr, 4
            For Cnt = 0 To 3
            ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "."
            Next Cnt
            ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
  End Function

Public Function getMyIP()
            Dim Ret     As Long, Tel       As Long
            Dim bBytes()     As Byte
            Dim Listing     As MIB_IPADDRTABLE
            
            On Error GoTo END1
            
            GetIpAddrTable ByVal 0&, Ret, True
            
            If Ret <= 0 Then Exit Function
            
            ReDim bBytes(0 To Ret - 1) As Byte
            
            GetIpAddrTable bBytes(0), Ret, False
            
            CopyMemory Listing.dEntrys, bBytes(0), 4
            
            getMyIP = "0.0.0.0"
            For Tel = 0 To Listing.dEntrys - 1
                CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len(Listing.mIPInfo(Tel))
                strIP = ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr)
'            strIP = strIP & "IP   地址   :   " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr) & vbCrLf
'            strIP = strIP & "子网掩码   :   " & ConvertAddressToString(Listing.mIPInfo(Tel).dwMask) & vbCrLf
'            strIP = strIP & "广播地址   :   " & ConvertAddressToString(Listing.mIPInfo(Tel).dwBCastAddr) & vbCrLf
'            strIP = strIP & "------------------------------------------------" & vbCrLf
                Select Case Mid(strIP, 1, 3)
                       Case "127"
                       Case "192"
                       Case Else
                            getMyIP = strIP
                End Select
            Next
            Exit Function
END1:
            getMyIP = "0.0.0.0"
  End Function




Form1中的内容

Private Sub Command1_Click()
            MsgBox getMyIP

End Sub
2011-11-04 10:47
快速回复:怎样根据操作系统版本自动获取IP地址
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035760 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved