Option Explicit Private Const WSADESCRIPTION_LEN = 256 Private Const WSASYS_STATUS_LEN = 128 Private Type HOSTENT h_name As Long h_aliases As Long h_addrtype As Integer h_length As Integer h_addr_list As Long End Type Private Type WSADATA wVersion As Integer wHighVersion As Integer szDescription(WSADESCRIPTION_LEN) As Byte szSystemStatus(WSASYS_STATUS_LEN) As Byte iMaxSockets As Integer iMaxUdpDg As Integer lpVendorInfo As Long End Type Private Declare Function gethostbyname Lib "wsock32.dll" (ByVal _ szName As String) As Long Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal _ wVersionRequested As Integer, lpWSAData As WSADATA) As Long Private Declare Function WSACleanup Lib "wsock32.dll" () As Integer Private Declare Sub CopyMemory Lib "kernel32" Alias _ "RtlMoveMemory" (Destination As Any, Source As Any, ByVal _ Length As Long) Public Function LookupIPAddress(ByVal sHostName As String) As String Dim wsa As WSADATA Dim nRet As Long Dim nTemp As Long Dim bTemp(0 To 3) As Byte Dim sOut As String Dim he As HOSTENT
'Initialize WinSock WSAStartup &H10, wsa
'Attempt to lookup the host nRet = gethostbyname(sHostName)
'If it failed, just return nothing If nRet = 0 Then sOut = "" Else 'Take a look at the resulting hostent structure CopyMemory he, ByVal nRet, Len(he)
'Are there atlest four bytes, then we have ' at least one address If he.h_length >= 4 Then 'Copy the address out, CopyMemory nTemp, ByVal he.h_addr_list, 4 CopyMemory bTemp(0), ByVal nTemp, 4 ' and format it sOut = Format(bTemp(0)) & "." & Format(bTemp(1)) & "." _ & Format(bTemp(2)) & "." & Format(bTemp(3)) Else sOut = "" End If
End If
WSACleanup LookupIPAddress = sOut End Function
Private Sub Command1_Click() MsgBox LookupIPAddress("localhost") End Sub
Private Const WSASYS_STATUS_LEN = 128 Private Type HOSTENT
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(WSADESCRIPTION_LEN) As Byte
szSystemStatus(WSASYS_STATUS_LEN) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type Private Declare Function gethostbyname Lib "wsock32.dll" (ByVal _
szName As String) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal _
wVersionRequested As Integer, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, ByVal _
Length As Long) Public Function LookupIPAddress(ByVal sHostName As String) As String Dim wsa As WSADATA
Dim nRet As Long
Dim nTemp As Long
Dim bTemp(0 To 3) As Byte
Dim sOut As String
Dim he As HOSTENT
'Initialize WinSock
WSAStartup &H10, wsa
'Attempt to lookup the host
nRet = gethostbyname(sHostName)
'If it failed, just return nothing
If nRet = 0 Then
sOut = ""
Else
'Take a look at the resulting hostent structure
CopyMemory he, ByVal nRet, Len(he)
'Are there atlest four bytes, then we have
' at least one address
If he.h_length >= 4 Then
'Copy the address out,
CopyMemory nTemp, ByVal he.h_addr_list, 4
CopyMemory bTemp(0), ByVal nTemp, 4
' and format it
sOut = Format(bTemp(0)) & "." & Format(bTemp(1)) & "." _
& Format(bTemp(2)) & "." & Format(bTemp(3))
Else
sOut = ""
End If
End If
WSACleanup LookupIPAddress = sOut End Function
Private Sub Command1_Click()
MsgBox LookupIPAddress("localhost")
End Sub
不行呀!我的情况是:我上班的时候,我通过指定IP将我的IP地址、网关、DNS设置改为指定的,而回到家的时候改为自动获得IP和DNS,前面的通过查看资料能实现了,但是现在不知道怎样让它设置回自动获得IP和DNS。
在DOS下有个命令:netsh
使用这个命令就完全可以做到网卡的IP设置和保存.
例子1:保存当前的网卡IP设置到文件1.TXT
netsh -c interface dump > 1.txt例子2:从配置文件1.TXT中加载网卡设置
netsh -f 1.txt这两个例子中没有指定文件的绝对路径,所以都是在当前目录下.
因此你可以将这两个命令写成BAT文件,然后直接使用或者是在VB里用SHELL来调用.还有,你可以研究一下这个保存下来的配置文件1.TXT,就可以按照需要写出不同的配置来了,然后使用第二个命令来加载就可以立刻改变机器的网络设置了.