问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

vb获取网关

发布网友 发布时间:2022-05-07 23:23

我来回答

4个回答

热心网友 时间:2023-11-20 03:24

Private Function EthernetAddress(LanaNumber As Long) As String

Dim udtNCB As NCB
Dim bytResponse As Byte
Dim udtASTAT As ASTAT
Dim udtTempASTAT As ASTAT
Dim lngASTAT As Long
Dim strOut As String
Dim x As Integer

udtNCB.ncb_command = NCBRESET
bytResponse = Netbios(udtNCB)
udtNCB.ncb_command = NCBASTAT
udtNCB.ncb_lana_num = LanaNumber
udtNCB.ncb_callname = "* "
udtNCB.ncb_length = Len(udtASTAT)
lngASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, udtNCB.ncb_length)

strOut = ""

If lngASTAT Then
udtNCB.ncb_buffer = lngASTAT
bytResponse = Netbios(udtNCB)
CopyMemory udtASTAT, udtNCB.ncb_buffer, Len(udtASTAT)

With udtASTAT.adapt

For x = 0 To 5
strOut = strOut & Right$("00" & Hex$(.adapter_address(x)), 2)
Next x

End With

HeapFree GetProcessHeap(), 0, lngASTAT
End If

EthernetAddress = strOut
End Function

Function LocalIPAddress() As String
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING
Dim ptr1 As Long
Dim sIPAddr As String
Dim found As Boolean
Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then
ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
'获取存放在buff()中的数据的指针
ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)
'将第一个网卡的数据转换到IP_ADAPTER_INFO结构中
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter
'IpAddress.IpAddr成员给出了DHCP的IP地址
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

If Len(sIPAddr) > 0 Then
found = True
Exit Do
End If

ptr1 = .dwNext
End With 'With Adapter

'不再有网卡时,ptr1的值为0
Loop 'Do While (ptr1 <> 0)

End If 'If GetAdaptersInfo
End If 'If cbRequired > 0

'返回结果字符串
LocalIPAddress = sIPAddr
End Function

Function TrimNull(item As String)
Dim pos As Integer
pos = InStr(item, Chr$(0))

If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If

End Function

Private Sub Form_Load()
Text1 = "IP地址:" & LocalIPAddress
Text2 = "MAC地址:" & EthernetAddress(0)
End Sub

热心网友 时间:2023-12-12 04:22

Private Function EthernetAddress(LanaNumber As Long) As String

Dim udtNCB As NCB
Dim bytResponse As Byte
Dim udtASTAT As ASTAT
Dim udtTempASTAT As ASTAT
Dim lngASTAT As Long
Dim strOut As String
Dim x As Integer

udtNCB.ncb_command = NCBRESET
bytResponse = Netbios(udtNCB)
udtNCB.ncb_command = NCBASTAT
udtNCB.ncb_lana_num = LanaNumber
udtNCB.ncb_callname = "* "
udtNCB.ncb_length = Len(udtASTAT)
lngASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, udtNCB.ncb_length)

strOut = ""

If lngASTAT Then
udtNCB.ncb_buffer = lngASTAT
bytResponse = Netbios(udtNCB)
CopyMemory udtASTAT, udtNCB.ncb_buffer, Len(udtASTAT)

With udtASTAT.adapt

For x = 0 To 5
strOut = strOut & Right$("00" & Hex$(.adapter_address(x)), 2)
Next x

End With

HeapFree GetProcessHeap(), 0, lngASTAT
End If

EthernetAddress = strOut
End Function

Function LocalIPAddress() As String
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING
Dim ptr1 As Long
Dim sIPAddr As String
Dim found As Boolean
Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then
ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
'获取存放在buff()中的数据的指针
ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)
'将第一个网卡的数据转换到IP_ADAPTER_INFO结构中
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter
'IpAddress.IpAddr成员给出了DHCP的IP地址
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

If Len(sIPAddr) > 0 Then
found = True
Exit Do
End If

ptr1 = .dwNext
End With 'With Adapter

'不再有网卡时,ptr1的值为0
Loop 'Do While (ptr1 <> 0)

End If 'If GetAdaptersInfo
End If 'If cbRequired > 0

'返回结果字符串
LocalIPAddress = sIPAddr
End Function

Function TrimNull(item As String)
Dim pos As Integer
pos = InStr(item, Chr$(0))

If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If

End Function

Private Sub Form_Load()
Text1 = "IP地址:" & LocalIPAddress
Text2 = "MAC地址:" & EthernetAddress(0)
End Sub

热心网友 时间:2023-12-12 04:22

Option Explicit

Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256
Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128
Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8
Private Const ERROR_SUCCESS As Long = 0

Private Type IP_ADDRESS_STRING
IpAddr(0 To 15) As Byte
End Type

Private Type IP_MASK_STRING
IpMask(0 To 15) As Byte
End Type

Private Type IP_ADDR_STRING
dwNext As Long
IpAddress As IP_ADDRESS_STRING
IpMask As IP_MASK_STRING
dwContext As Long
End Type

Private Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long 'reserved
sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
dwAddressLength As Long
sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
dwIndex As Long
uType As Long
uDhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
bHaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
(pTcpTable As Any, _
pdwSize As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(dst As Any, _
src As Any, _
ByVal bcount As Long)

Private Sub Command1_Click()

Label1.Caption = LocalIPAddress()
End Sub

Private Function LocalIPAddress() As String

'api vars
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING

'working vars
Dim ptr1 As Long
Dim sIPAddr As String
Dim found As Boolean
Dim sGateway As String

Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then

ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then

ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)

CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter

'IP地址
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

'默认网关
sGateway = TrimNull(StrConv(.GatewayList.IpAddress.IpAddr, vbUnicode))

If Len(sGateway) > 0 Then 'Len(sIPAddr)
found = True
Exit Do
End If

ptr1 = .dwNext

End With
Loop

End If
End If

LocalIPAddress = sGateway ' sIPAddr

End Function

Private Function TrimNull(item As String)

Dim pos As Integer

'double check that there is a chr$(0) in the string
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If

End Function

热心网友 时间:2023-11-20 03:24

Private Function EthernetAddress(LanaNumber As Long) As String

Dim udtNCB As NCB
Dim bytResponse As Byte
Dim udtASTAT As ASTAT
Dim udtTempASTAT As ASTAT
Dim lngASTAT As Long
Dim strOut As String
Dim x As Integer

udtNCB.ncb_command = NCBRESET
bytResponse = Netbios(udtNCB)
udtNCB.ncb_command = NCBASTAT
udtNCB.ncb_lana_num = LanaNumber
udtNCB.ncb_callname = "* "
udtNCB.ncb_length = Len(udtASTAT)
lngASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, udtNCB.ncb_length)

strOut = ""

If lngASTAT Then
udtNCB.ncb_buffer = lngASTAT
bytResponse = Netbios(udtNCB)
CopyMemory udtASTAT, udtNCB.ncb_buffer, Len(udtASTAT)

With udtASTAT.adapt

For x = 0 To 5
strOut = strOut & Right$("00" & Hex$(.adapter_address(x)), 2)
Next x

End With

HeapFree GetProcessHeap(), 0, lngASTAT
End If

EthernetAddress = strOut
End Function

Function LocalIPAddress() As String
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING
Dim ptr1 As Long
Dim sIPAddr As String
Dim found As Boolean
Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then
ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
'获取存放在buff()中的数据的指针
ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)
'将第一个网卡的数据转换到IP_ADAPTER_INFO结构中
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter
'IpAddress.IpAddr成员给出了DHCP的IP地址
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

If Len(sIPAddr) > 0 Then
found = True
Exit Do
End If

ptr1 = .dwNext
End With 'With Adapter

'不再有网卡时,ptr1的值为0
Loop 'Do While (ptr1 <> 0)

End If 'If GetAdaptersInfo
End If 'If cbRequired > 0

'返回结果字符串
LocalIPAddress = sIPAddr
End Function

Function TrimNull(item As String)
Dim pos As Integer
pos = InStr(item, Chr$(0))

If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If

End Function

Private Sub Form_Load()
Text1 = "IP地址:" & LocalIPAddress
Text2 = "MAC地址:" & EthernetAddress(0)
End Sub

热心网友 时间:2023-11-20 03:24

Option Explicit

Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256
Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128
Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8
Private Const ERROR_SUCCESS As Long = 0

Private Type IP_ADDRESS_STRING
IpAddr(0 To 15) As Byte
End Type

Private Type IP_MASK_STRING
IpMask(0 To 15) As Byte
End Type

Private Type IP_ADDR_STRING
dwNext As Long
IpAddress As IP_ADDRESS_STRING
IpMask As IP_MASK_STRING
dwContext As Long
End Type

Private Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long 'reserved
sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
dwAddressLength As Long
sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
dwIndex As Long
uType As Long
uDhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
bHaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
(pTcpTable As Any, _
pdwSize As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(dst As Any, _
src As Any, _
ByVal bcount As Long)

Private Sub Command1_Click()

Label1.Caption = LocalIPAddress()
End Sub

Private Function LocalIPAddress() As String

'api vars
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING

'working vars
Dim ptr1 As Long
Dim sIPAddr As String
Dim found As Boolean
Dim sGateway As String

Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then

ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then

ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)

CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter

'IP地址
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

'默认网关
sGateway = TrimNull(StrConv(.GatewayList.IpAddress.IpAddr, vbUnicode))

If Len(sGateway) > 0 Then 'Len(sIPAddr)
found = True
Exit Do
End If

ptr1 = .dwNext

End With
Loop

End If
End If

LocalIPAddress = sGateway ' sIPAddr

End Function

Private Function TrimNull(item As String)

Dim pos As Integer

'double check that there is a chr$(0) in the string
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If

End Function

热心网友 时间:2023-11-20 03:25

GetAdaptersInfo

热心网友 时间:2023-11-20 03:25

窗体代码:

Private Sub Command1_Click()
If SocketsInitialize Then ShowAdapterInfo
SocketsCleanup
Label1.Caption = GatewayIP
End Sub

模块代码:

Option Explicit
Option Compare Text

Public GatewayIP As String

Public Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 127) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type

Public Type IP_ADDR_STRING
dwNext As Long
IpAddress As String * 16
IpMask As String * 16
Context As Long
End Type

Public Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long
AdapterName As String * 260
Description As String * 132
AddressLength As Long
Address(7) As Byte
Index As Long
dwType As Long
DhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
HaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "IPHlpApi" (IpAdapterInfo As Any, pOutBufLen As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function WSACleanup Lib "ws2_32.DLL" () As Long
Private Declare Function WSAStartup Lib "ws2_32.DLL" (ByVal wVR As Long, lpWSAD As WSADATA) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Function SocketsCleanup() As Boolean
SocketsCleanup = CBool(WSACleanup = 0)
End Function

Public Function SocketsInitialize() As Boolean
Dim WSAD As WSADATA
If WSAStartup(&H101, WSAD) = 0 Then
SocketsInitialize = True
Else
SocketsCleanup
SocketsInitialize = False
End If
End Function

Public Function TrimString(ByVal vData As String) As String
Dim ZPos As Long
ZPos = InStr(vData, Chr$(0))
If ZPos <> 0 Then vData = Left(vData, ZPos - 1)
TrimString = Trim(vData)
End Function

Public Sub ShowAdapterInfo()

Const MIB_IF_TYPE_ETHERNET = 6
Const MIB_IF_TYPE_TOKENRING = 9
Const MIB_IF_TYPE_FDDI = 15
Const MIB_IF_TYPE_PPP = 23
Const MIB_IF_TYPE_LOOPBACK = 24
Const MIB_IF_TYPE_SLIP = 28
Const MAX_ADAPTER_NAME = 128
Const ERROR_BUFFER_OVERFLOW = 111
Const GMEM_FIXED = &H0

Dim Error As Long
Dim AdapterInfoSize As Long
Dim I As Long
Dim NewTime As Date
Dim AdapterInfo As IP_ADAPTER_INFO
Dim Buffer As IP_ADDR_STRING
Dim pAddrStr As Long
Dim pAdapt As Long
Dim Buffer2 As IP_ADAPTER_INFO
Dim AdapterInfoBuffer As Long

AdapterInfoSize = 0
Error = GetAdaptersInfo(ByVal 0&, AdapterInfoSize)
If Error <> 0 Then
If Error <> ERROR_BUFFER_OVERFLOW Then
MsgBox "Error retrieving devices."
Exit Sub
End If
End If
AdapterInfoBuffer = GlobalAlloc(GMEM_FIXED, AdapterInfoSize)
If AdapterInfoBuffer = 0 Then
MsgBox "Error retrieving devices."
Exit Sub
End If
Error = GetAdaptersInfo(ByVal AdapterInfoBuffer, AdapterInfoSize)
If Error <> 0 Then
MsgBox "Error retrieving devices."
GlobalFree AdapterInfoBuffer
Exit Sub
End If
CopyMemory AdapterInfo, ByVal AdapterInfoBuffer, Len(AdapterInfo)
pAdapt = AdapterInfo.dwNext
Do
CopyMemory Buffer2, AdapterInfo, Len(Buffer2)
Debug.Print "****************************************************************"
Debug.Print "Adapter Index : "; Format(Buffer2.Index, "0")
Debug.Print "Adapter Name : "; TrimString(AdapterInfo.AdapterName)
Debug.Print "Description : "; TrimString(Buffer2.Description)
Select Case Buffer2.dwType
Case MIB_IF_TYPE_ETHERNET
Debug.Print "Type : "; "Ethernet adapter"
Case MIB_IF_TYPE_TOKENRING
Debug.Print "Type : "; "Token ring adapter"
Case MIB_IF_TYPE_FDDI
Debug.Print "Type : "; "FDDI adapter"
Case MIB_IF_TYPE_PPP
Debug.Print "Type : "; "PPP adapter"
Case MIB_IF_TYPE_LOOPBACK
Debug.Print "Type : "; "Loopback adapter"
Case MIB_IF_TYPE_SLIP
Debug.Print "Type : "; "Slip adapter"
Case Else
Debug.Print "Type : "; "Other adapter"
End Select
Debug.Print "MAC Address : ";
For I = 0 To Buffer2.AddressLength - 1
If Len(Hex(Buffer2.Address(I))) = 1 Then
Debug.Print "0"; Hex(Buffer2.Address(I));
Else
Debug.Print Hex(Buffer2.Address(I));
End If
If I < Buffer2.AddressLength - 1 Then Debug.Print "-";
Next
Debug.Print ""
If Buffer2.DhcpEnabled Then
Debug.Print "DHCP : "; "Enabled"
Else
Debug.Print "DHCP : "; "Disabled"
End If
Debug.Print "IP Address : "; TrimString(Buffer2.IpAddressList.IpAddress)
Debug.Print "Subnet Mask : "; TrimString(Buffer2.IpAddressList.IpMask)
pAddrStr = Buffer2.IpAddressList.dwNext
Do While pAddrStr <> 0
'NOTE : I haven't tested this
CopyMemory Buffer, Buffer2.IpAddressList, Len(Buffer)
Debug.Print "IP Address : "; TrimString(Buffer.IpAddress)
Debug.Print "Subnet Mask : "; TrimString(Buffer.IpMask)
pAddrStr = Buffer.dwNext
If pAddrStr <> 0 Then CopyMemory Buffer2.IpAddressList, ByVal pAddrStr, Len(Buffer2.IpAddressList)
Loop
Debug.Print "Default Gateway : "; TrimString(Buffer2.GatewayList.IpAddress)
If TrimString(Buffer2.GatewayList.IpAddress) <> "0.0.0.0" Then GatewayIP = TrimString(Buffer2.GatewayList.IpAddress)
pAddrStr = Buffer2.GatewayList.dwNext
Do While pAddrStr <> 0
CopyMemory Buffer, Buffer2.GatewayList, Len(Buffer)
pAddrStr = Buffer.dwNext
If pAddrStr <> 0 Then CopyMemory Buffer2.GatewayList, ByVal pAddrStr, Len(Buffer2.GatewayList)
Loop
Debug.Print "DHCP Server : "; TrimString(Buffer2.DhcpServer.IpAddress)
Debug.Print "Primary WINS Server : "; TrimString(Buffer2.PrimaryWinsServer.IpAddress)
Debug.Print "Secondary WINS Server : "; TrimString(Buffer2.SecondaryWinsServer.IpAddress)
NewTime = DateAdd("s", Buffer2.LeaseObtained, #1/1/1970#)
Debug.Print "Lease Obtained : "; CStr(Format(NewTime, "dddd,mmm d hh:mm:ss yyyy"))
NewTime = DateAdd("s", Buffer2.LeaseExpires, #1/1/1970#)
Debug.Print "Lease Expires : "; CStr(Format(NewTime, "dddd,mmm d hh:mm:ss yyyy"))
pAdapt = Buffer2.dwNext
If pAdapt <> 0 Then CopyMemory AdapterInfo, ByVal pAdapt, Len(AdapterInfo)
Loop While pAdapt <> 0
GlobalFree AdapterInfoBuffer
End Sub

热心网友 时间:2023-12-12 04:22

GetAdaptersInfo

热心网友 时间:2023-11-20 03:24

Option Explicit

Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256
Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128
Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8
Private Const ERROR_SUCCESS As Long = 0

Private Type IP_ADDRESS_STRING
IpAddr(0 To 15) As Byte
End Type

Private Type IP_MASK_STRING
IpMask(0 To 15) As Byte
End Type

Private Type IP_ADDR_STRING
dwNext As Long
IpAddress As IP_ADDRESS_STRING
IpMask As IP_MASK_STRING
dwContext As Long
End Type

Private Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long 'reserved
sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
dwAddressLength As Long
sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
dwIndex As Long
uType As Long
uDhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
bHaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
(pTcpTable As Any, _
pdwSize As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(dst As Any, _
src As Any, _
ByVal bcount As Long)

Private Sub Command1_Click()

Label1.Caption = LocalIPAddress()
End Sub

Private Function LocalIPAddress() As String

'api vars
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING

'working vars
Dim ptr1 As Long
Dim sIPAddr As String
Dim found As Boolean
Dim sGateway As String

Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then

ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then

ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)

CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter

'IP地址
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

'默认网关
sGateway = TrimNull(StrConv(.GatewayList.IpAddress.IpAddr, vbUnicode))

If Len(sGateway) > 0 Then 'Len(sIPAddr)
found = True
Exit Do
End If

ptr1 = .dwNext

End With
Loop

End If
End If

LocalIPAddress = sGateway ' sIPAddr

End Function

Private Function TrimNull(item As String)

Dim pos As Integer

'double check that there is a chr$(0) in the string
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If

End Function

热心网友 时间:2023-11-20 03:25

GetAdaptersInfo

热心网友 时间:2023-11-20 03:25

窗体代码:

Private Sub Command1_Click()
If SocketsInitialize Then ShowAdapterInfo
SocketsCleanup
Label1.Caption = GatewayIP
End Sub

模块代码:

Option Explicit
Option Compare Text

Public GatewayIP As String

Public Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 127) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type

Public Type IP_ADDR_STRING
dwNext As Long
IpAddress As String * 16
IpMask As String * 16
Context As Long
End Type

Public Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long
AdapterName As String * 260
Description As String * 132
AddressLength As Long
Address(7) As Byte
Index As Long
dwType As Long
DhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
HaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "IPHlpApi" (IpAdapterInfo As Any, pOutBufLen As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function WSACleanup Lib "ws2_32.DLL" () As Long
Private Declare Function WSAStartup Lib "ws2_32.DLL" (ByVal wVR As Long, lpWSAD As WSADATA) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Function SocketsCleanup() As Boolean
SocketsCleanup = CBool(WSACleanup = 0)
End Function

Public Function SocketsInitialize() As Boolean
Dim WSAD As WSADATA
If WSAStartup(&H101, WSAD) = 0 Then
SocketsInitialize = True
Else
SocketsCleanup
SocketsInitialize = False
End If
End Function

Public Function TrimString(ByVal vData As String) As String
Dim ZPos As Long
ZPos = InStr(vData, Chr$(0))
If ZPos <> 0 Then vData = Left(vData, ZPos - 1)
TrimString = Trim(vData)
End Function

Public Sub ShowAdapterInfo()

Const MIB_IF_TYPE_ETHERNET = 6
Const MIB_IF_TYPE_TOKENRING = 9
Const MIB_IF_TYPE_FDDI = 15
Const MIB_IF_TYPE_PPP = 23
Const MIB_IF_TYPE_LOOPBACK = 24
Const MIB_IF_TYPE_SLIP = 28
Const MAX_ADAPTER_NAME = 128
Const ERROR_BUFFER_OVERFLOW = 111
Const GMEM_FIXED = &H0

Dim Error As Long
Dim AdapterInfoSize As Long
Dim I As Long
Dim NewTime As Date
Dim AdapterInfo As IP_ADAPTER_INFO
Dim Buffer As IP_ADDR_STRING
Dim pAddrStr As Long
Dim pAdapt As Long
Dim Buffer2 As IP_ADAPTER_INFO
Dim AdapterInfoBuffer As Long

AdapterInfoSize = 0
Error = GetAdaptersInfo(ByVal 0&, AdapterInfoSize)
If Error <> 0 Then
If Error <> ERROR_BUFFER_OVERFLOW Then
MsgBox "Error retrieving devices."
Exit Sub
End If
End If
AdapterInfoBuffer = GlobalAlloc(GMEM_FIXED, AdapterInfoSize)
If AdapterInfoBuffer = 0 Then
MsgBox "Error retrieving devices."
Exit Sub
End If
Error = GetAdaptersInfo(ByVal AdapterInfoBuffer, AdapterInfoSize)
If Error <> 0 Then
MsgBox "Error retrieving devices."
GlobalFree AdapterInfoBuffer
Exit Sub
End If
CopyMemory AdapterInfo, ByVal AdapterInfoBuffer, Len(AdapterInfo)
pAdapt = AdapterInfo.dwNext
Do
CopyMemory Buffer2, AdapterInfo, Len(Buffer2)
Debug.Print "****************************************************************"
Debug.Print "Adapter Index : "; Format(Buffer2.Index, "0")
Debug.Print "Adapter Name : "; TrimString(AdapterInfo.AdapterName)
Debug.Print "Description : "; TrimString(Buffer2.Description)
Select Case Buffer2.dwType
Case MIB_IF_TYPE_ETHERNET
Debug.Print "Type : "; "Ethernet adapter"
Case MIB_IF_TYPE_TOKENRING
Debug.Print "Type : "; "Token ring adapter"
Case MIB_IF_TYPE_FDDI
Debug.Print "Type : "; "FDDI adapter"
Case MIB_IF_TYPE_PPP
Debug.Print "Type : "; "PPP adapter"
Case MIB_IF_TYPE_LOOPBACK
Debug.Print "Type : "; "Loopback adapter"
Case MIB_IF_TYPE_SLIP
Debug.Print "Type : "; "Slip adapter"
Case Else
Debug.Print "Type : "; "Other adapter"
End Select
Debug.Print "MAC Address : ";
For I = 0 To Buffer2.AddressLength - 1
If Len(Hex(Buffer2.Address(I))) = 1 Then
Debug.Print "0"; Hex(Buffer2.Address(I));
Else
Debug.Print Hex(Buffer2.Address(I));
End If
If I < Buffer2.AddressLength - 1 Then Debug.Print "-";
Next
Debug.Print ""
If Buffer2.DhcpEnabled Then
Debug.Print "DHCP : "; "Enabled"
Else
Debug.Print "DHCP : "; "Disabled"
End If
Debug.Print "IP Address : "; TrimString(Buffer2.IpAddressList.IpAddress)
Debug.Print "Subnet Mask : "; TrimString(Buffer2.IpAddressList.IpMask)
pAddrStr = Buffer2.IpAddressList.dwNext
Do While pAddrStr <> 0
'NOTE : I haven't tested this
CopyMemory Buffer, Buffer2.IpAddressList, Len(Buffer)
Debug.Print "IP Address : "; TrimString(Buffer.IpAddress)
Debug.Print "Subnet Mask : "; TrimString(Buffer.IpMask)
pAddrStr = Buffer.dwNext
If pAddrStr <> 0 Then CopyMemory Buffer2.IpAddressList, ByVal pAddrStr, Len(Buffer2.IpAddressList)
Loop
Debug.Print "Default Gateway : "; TrimString(Buffer2.GatewayList.IpAddress)
If TrimString(Buffer2.GatewayList.IpAddress) <> "0.0.0.0" Then GatewayIP = TrimString(Buffer2.GatewayList.IpAddress)
pAddrStr = Buffer2.GatewayList.dwNext
Do While pAddrStr <> 0
CopyMemory Buffer, Buffer2.GatewayList, Len(Buffer)
pAddrStr = Buffer.dwNext
If pAddrStr <> 0 Then CopyMemory Buffer2.GatewayList, ByVal pAddrStr, Len(Buffer2.GatewayList)
Loop
Debug.Print "DHCP Server : "; TrimString(Buffer2.DhcpServer.IpAddress)
Debug.Print "Primary WINS Server : "; TrimString(Buffer2.PrimaryWinsServer.IpAddress)
Debug.Print "Secondary WINS Server : "; TrimString(Buffer2.SecondaryWinsServer.IpAddress)
NewTime = DateAdd("s", Buffer2.LeaseObtained, #1/1/1970#)
Debug.Print "Lease Obtained : "; CStr(Format(NewTime, "dddd,mmm d hh:mm:ss yyyy"))
NewTime = DateAdd("s", Buffer2.LeaseExpires, #1/1/1970#)
Debug.Print "Lease Expires : "; CStr(Format(NewTime, "dddd,mmm d hh:mm:ss yyyy"))
pAdapt = Buffer2.dwNext
If pAdapt <> 0 Then CopyMemory AdapterInfo, ByVal pAdapt, Len(AdapterInfo)
Loop While pAdapt <> 0
GlobalFree AdapterInfoBuffer
End Sub

热心网友 时间:2023-12-12 04:23

窗体代码:

Private Sub Command1_Click()
If SocketsInitialize Then ShowAdapterInfo
SocketsCleanup
Label1.Caption = GatewayIP
End Sub

模块代码:

Option Explicit
Option Compare Text

Public GatewayIP As String

Public Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 127) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type

Public Type IP_ADDR_STRING
dwNext As Long
IpAddress As String * 16
IpMask As String * 16
Context As Long
End Type

Public Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long
AdapterName As String * 260
Description As String * 132
AddressLength As Long
Address(7) As Byte
Index As Long
dwType As Long
DhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
HaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "IPHlpApi" (IpAdapterInfo As Any, pOutBufLen As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function WSACleanup Lib "ws2_32.DLL" () As Long
Private Declare Function WSAStartup Lib "ws2_32.DLL" (ByVal wVR As Long, lpWSAD As WSADATA) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Function SocketsCleanup() As Boolean
SocketsCleanup = CBool(WSACleanup = 0)
End Function

Public Function SocketsInitialize() As Boolean
Dim WSAD As WSADATA
If WSAStartup(&H101, WSAD) = 0 Then
SocketsInitialize = True
Else
SocketsCleanup
SocketsInitialize = False
End If
End Function

Public Function TrimString(ByVal vData As String) As String
Dim ZPos As Long
ZPos = InStr(vData, Chr$(0))
If ZPos <> 0 Then vData = Left(vData, ZPos - 1)
TrimString = Trim(vData)
End Function

Public Sub ShowAdapterInfo()

Const MIB_IF_TYPE_ETHERNET = 6
Const MIB_IF_TYPE_TOKENRING = 9
Const MIB_IF_TYPE_FDDI = 15
Const MIB_IF_TYPE_PPP = 23
Const MIB_IF_TYPE_LOOPBACK = 24
Const MIB_IF_TYPE_SLIP = 28
Const MAX_ADAPTER_NAME = 128
Const ERROR_BUFFER_OVERFLOW = 111
Const GMEM_FIXED = &H0

Dim Error As Long
Dim AdapterInfoSize As Long
Dim I As Long
Dim NewTime As Date
Dim AdapterInfo As IP_ADAPTER_INFO
Dim Buffer As IP_ADDR_STRING
Dim pAddrStr As Long
Dim pAdapt As Long
Dim Buffer2 As IP_ADAPTER_INFO
Dim AdapterInfoBuffer As Long

AdapterInfoSize = 0
Error = GetAdaptersInfo(ByVal 0&, AdapterInfoSize)
If Error <> 0 Then
If Error <> ERROR_BUFFER_OVERFLOW Then
MsgBox "Error retrieving devices."
Exit Sub
End If
End If
AdapterInfoBuffer = GlobalAlloc(GMEM_FIXED, AdapterInfoSize)
If AdapterInfoBuffer = 0 Then
MsgBox "Error retrieving devices."
Exit Sub
End If
Error = GetAdaptersInfo(ByVal AdapterInfoBuffer, AdapterInfoSize)
If Error <> 0 Then
MsgBox "Error retrieving devices."
GlobalFree AdapterInfoBuffer
Exit Sub
End If
CopyMemory AdapterInfo, ByVal AdapterInfoBuffer, Len(AdapterInfo)
pAdapt = AdapterInfo.dwNext
Do
CopyMemory Buffer2, AdapterInfo, Len(Buffer2)
Debug.Print "****************************************************************"
Debug.Print "Adapter Index : "; Format(Buffer2.Index, "0")
Debug.Print "Adapter Name : "; TrimString(AdapterInfo.AdapterName)
Debug.Print "Description : "; TrimString(Buffer2.Description)
Select Case Buffer2.dwType
Case MIB_IF_TYPE_ETHERNET
Debug.Print "Type : "; "Ethernet adapter"
Case MIB_IF_TYPE_TOKENRING
Debug.Print "Type : "; "Token ring adapter"
Case MIB_IF_TYPE_FDDI
Debug.Print "Type : "; "FDDI adapter"
Case MIB_IF_TYPE_PPP
Debug.Print "Type : "; "PPP adapter"
Case MIB_IF_TYPE_LOOPBACK
Debug.Print "Type : "; "Loopback adapter"
Case MIB_IF_TYPE_SLIP
Debug.Print "Type : "; "Slip adapter"
Case Else
Debug.Print "Type : "; "Other adapter"
End Select
Debug.Print "MAC Address : ";
For I = 0 To Buffer2.AddressLength - 1
If Len(Hex(Buffer2.Address(I))) = 1 Then
Debug.Print "0"; Hex(Buffer2.Address(I));
Else
Debug.Print Hex(Buffer2.Address(I));
End If
If I < Buffer2.AddressLength - 1 Then Debug.Print "-";
Next
Debug.Print ""
If Buffer2.DhcpEnabled Then
Debug.Print "DHCP : "; "Enabled"
Else
Debug.Print "DHCP : "; "Disabled"
End If
Debug.Print "IP Address : "; TrimString(Buffer2.IpAddressList.IpAddress)
Debug.Print "Subnet Mask : "; TrimString(Buffer2.IpAddressList.IpMask)
pAddrStr = Buffer2.IpAddressList.dwNext
Do While pAddrStr <> 0
'NOTE : I haven't tested this
CopyMemory Buffer, Buffer2.IpAddressList, Len(Buffer)
Debug.Print "IP Address : "; TrimString(Buffer.IpAddress)
Debug.Print "Subnet Mask : "; TrimString(Buffer.IpMask)
pAddrStr = Buffer.dwNext
If pAddrStr <> 0 Then CopyMemory Buffer2.IpAddressList, ByVal pAddrStr, Len(Buffer2.IpAddressList)
Loop
Debug.Print "Default Gateway : "; TrimString(Buffer2.GatewayList.IpAddress)
If TrimString(Buffer2.GatewayList.IpAddress) <> "0.0.0.0" Then GatewayIP = TrimString(Buffer2.GatewayList.IpAddress)
pAddrStr = Buffer2.GatewayList.dwNext
Do While pAddrStr <> 0
CopyMemory Buffer, Buffer2.GatewayList, Len(Buffer)
pAddrStr = Buffer.dwNext
If pAddrStr <> 0 Then CopyMemory Buffer2.GatewayList, ByVal pAddrStr, Len(Buffer2.GatewayList)
Loop
Debug.Print "DHCP Server : "; TrimString(Buffer2.DhcpServer.IpAddress)
Debug.Print "Primary WINS Server : "; TrimString(Buffer2.PrimaryWinsServer.IpAddress)
Debug.Print "Secondary WINS Server : "; TrimString(Buffer2.SecondaryWinsServer.IpAddress)
NewTime = DateAdd("s", Buffer2.LeaseObtained, #1/1/1970#)
Debug.Print "Lease Obtained : "; CStr(Format(NewTime, "dddd,mmm d hh:mm:ss yyyy"))
NewTime = DateAdd("s", Buffer2.LeaseExpires, #1/1/1970#)
Debug.Print "Lease Expires : "; CStr(Format(NewTime, "dddd,mmm d hh:mm:ss yyyy"))
pAdapt = Buffer2.dwNext
If pAdapt <> 0 Then CopyMemory AdapterInfo, ByVal pAdapt, Len(AdapterInfo)
Loop While pAdapt <> 0
GlobalFree AdapterInfoBuffer
End Sub

热心网友 时间:2023-11-20 03:24

Private Function EthernetAddress(LanaNumber As Long) As String

Dim udtNCB As NCB
Dim bytResponse As Byte
Dim udtASTAT As ASTAT
Dim udtTempASTAT As ASTAT
Dim lngASTAT As Long
Dim strOut As String
Dim x As Integer

udtNCB.ncb_command = NCBRESET
bytResponse = Netbios(udtNCB)
udtNCB.ncb_command = NCBASTAT
udtNCB.ncb_lana_num = LanaNumber
udtNCB.ncb_callname = "* "
udtNCB.ncb_length = Len(udtASTAT)
lngASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS Or HEAP_ZERO_MEMORY, udtNCB.ncb_length)

strOut = ""

If lngASTAT Then
udtNCB.ncb_buffer = lngASTAT
bytResponse = Netbios(udtNCB)
CopyMemory udtASTAT, udtNCB.ncb_buffer, Len(udtASTAT)

With udtASTAT.adapt

For x = 0 To 5
strOut = strOut & Right$("00" & Hex$(.adapter_address(x)), 2)
Next x

End With

HeapFree GetProcessHeap(), 0, lngASTAT
End If

EthernetAddress = strOut
End Function

Function LocalIPAddress() As String
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING
Dim ptr1 As Long
Dim sIPAddr As String
Dim found As Boolean
Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then
ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
'获取存放在buff()中的数据的指针
ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)
'将第一个网卡的数据转换到IP_ADAPTER_INFO结构中
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter
'IpAddress.IpAddr成员给出了DHCP的IP地址
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

If Len(sIPAddr) > 0 Then
found = True
Exit Do
End If

ptr1 = .dwNext
End With 'With Adapter

'不再有网卡时,ptr1的值为0
Loop 'Do While (ptr1 <> 0)

End If 'If GetAdaptersInfo
End If 'If cbRequired > 0

'返回结果字符串
LocalIPAddress = sIPAddr
End Function

Function TrimNull(item As String)
Dim pos As Integer
pos = InStr(item, Chr$(0))

If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If

End Function

Private Sub Form_Load()
Text1 = "IP地址:" & LocalIPAddress
Text2 = "MAC地址:" & EthernetAddress(0)
End Sub

热心网友 时间:2023-11-20 03:24

Option Explicit

Private Const MAX_ADAPTER_NAME_LENGTH As Long = 256
Private Const MAX_ADAPTER_DESCRIPTION_LENGTH As Long = 128
Private Const MAX_ADAPTER_ADDRESS_LENGTH As Long = 8
Private Const ERROR_SUCCESS As Long = 0

Private Type IP_ADDRESS_STRING
IpAddr(0 To 15) As Byte
End Type

Private Type IP_MASK_STRING
IpMask(0 To 15) As Byte
End Type

Private Type IP_ADDR_STRING
dwNext As Long
IpAddress As IP_ADDRESS_STRING
IpMask As IP_MASK_STRING
dwContext As Long
End Type

Private Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long 'reserved
sAdapterName(0 To (MAX_ADAPTER_NAME_LENGTH + 3)) As Byte
sDescription(0 To (MAX_ADAPTER_DESCRIPTION_LENGTH + 3)) As Byte
dwAddressLength As Long
sIPAddress(0 To (MAX_ADAPTER_ADDRESS_LENGTH - 1)) As Byte
dwIndex As Long
uType As Long
uDhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
bHaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "iphlpapi.dll" _
(pTcpTable As Any, _
pdwSize As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(dst As Any, _
src As Any, _
ByVal bcount As Long)

Private Sub Command1_Click()

Label1.Caption = LocalIPAddress()
End Sub

Private Function LocalIPAddress() As String

'api vars
Dim cbRequired As Long
Dim buff() As Byte
Dim Adapter As IP_ADAPTER_INFO
Dim AdapterStr As IP_ADDR_STRING

'working vars
Dim ptr1 As Long
Dim sIPAddr As String
Dim found As Boolean
Dim sGateway As String

Call GetAdaptersInfo(ByVal 0&, cbRequired)

If cbRequired > 0 Then

ReDim buff(0 To cbRequired - 1) As Byte

If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then

ptr1 = VarPtr(buff(0))

Do While (ptr1 <> 0)

CopyMemory Adapter, ByVal ptr1, LenB(Adapter)

With Adapter

'IP地址
sIPAddr = TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode))

'默认网关
sGateway = TrimNull(StrConv(.GatewayList.IpAddress.IpAddr, vbUnicode))

If Len(sGateway) > 0 Then 'Len(sIPAddr)
found = True
Exit Do
End If

ptr1 = .dwNext

End With
Loop

End If
End If

LocalIPAddress = sGateway ' sIPAddr

End Function

Private Function TrimNull(item As String)

Dim pos As Integer

'double check that there is a chr$(0) in the string
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If

End Function

热心网友 时间:2023-11-20 03:25

GetAdaptersInfo

热心网友 时间:2023-11-20 03:26

窗体代码:

Private Sub Command1_Click()
If SocketsInitialize Then ShowAdapterInfo
SocketsCleanup
Label1.Caption = GatewayIP
End Sub

模块代码:

Option Explicit
Option Compare Text

Public GatewayIP As String

Public Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 127) As Byte
wMaxSockets As Integer
wMaxUDPDG As Integer
dwVendorInfo As Long
End Type

Public Type IP_ADDR_STRING
dwNext As Long
IpAddress As String * 16
IpMask As String * 16
Context As Long
End Type

Public Type IP_ADAPTER_INFO
dwNext As Long
ComboIndex As Long
AdapterName As String * 260
Description As String * 132
AddressLength As Long
Address(7) As Byte
Index As Long
dwType As Long
DhcpEnabled As Long
CurrentIpAddress As Long
IpAddressList As IP_ADDR_STRING
GatewayList As IP_ADDR_STRING
DhcpServer As IP_ADDR_STRING
HaveWins As Long
PrimaryWinsServer As IP_ADDR_STRING
SecondaryWinsServer As IP_ADDR_STRING
LeaseObtained As Long
LeaseExpires As Long
End Type

Private Declare Function GetAdaptersInfo Lib "IPHlpApi" (IpAdapterInfo As Any, pOutBufLen As Long) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function WSACleanup Lib "ws2_32.DLL" () As Long
Private Declare Function WSAStartup Lib "ws2_32.DLL" (ByVal wVR As Long, lpWSAD As WSADATA) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public Function SocketsCleanup() As Boolean
SocketsCleanup = CBool(WSACleanup = 0)
End Function

Public Function SocketsInitialize() As Boolean
Dim WSAD As WSADATA
If WSAStartup(&H101, WSAD) = 0 Then
SocketsInitialize = True
Else
SocketsCleanup
SocketsInitialize = False
End If
End Function

Public Function TrimString(ByVal vData As String) As String
Dim ZPos As Long
ZPos = InStr(vData, Chr$(0))
If ZPos <> 0 Then vData = Left(vData, ZPos - 1)
TrimString = Trim(vData)
End Function

Public Sub ShowAdapterInfo()

Const MIB_IF_TYPE_ETHERNET = 6
Const MIB_IF_TYPE_TOKENRING = 9
Const MIB_IF_TYPE_FDDI = 15
Const MIB_IF_TYPE_PPP = 23
Const MIB_IF_TYPE_LOOPBACK = 24
Const MIB_IF_TYPE_SLIP = 28
Const MAX_ADAPTER_NAME = 128
Const ERROR_BUFFER_OVERFLOW = 111
Const GMEM_FIXED = &H0

Dim Error As Long
Dim AdapterInfoSize As Long
Dim I As Long
Dim NewTime As Date
Dim AdapterInfo As IP_ADAPTER_INFO
Dim Buffer As IP_ADDR_STRING
Dim pAddrStr As Long
Dim pAdapt As Long
Dim Buffer2 As IP_ADAPTER_INFO
Dim AdapterInfoBuffer As Long

AdapterInfoSize = 0
Error = GetAdaptersInfo(ByVal 0&, AdapterInfoSize)
If Error <> 0 Then
If Error <> ERROR_BUFFER_OVERFLOW Then
MsgBox "Error retrieving devices."
Exit Sub
End If
End If
AdapterInfoBuffer = GlobalAlloc(GMEM_FIXED, AdapterInfoSize)
If AdapterInfoBuffer = 0 Then
MsgBox "Error retrieving devices."
Exit Sub
End If
Error = GetAdaptersInfo(ByVal AdapterInfoBuffer, AdapterInfoSize)
If Error <> 0 Then
MsgBox "Error retrieving devices."
GlobalFree AdapterInfoBuffer
Exit Sub
End If
CopyMemory AdapterInfo, ByVal AdapterInfoBuffer, Len(AdapterInfo)
pAdapt = AdapterInfo.dwNext
Do
CopyMemory Buffer2, AdapterInfo, Len(Buffer2)
Debug.Print "****************************************************************"
Debug.Print "Adapter Index : "; Format(Buffer2.Index, "0")
Debug.Print "Adapter Name : "; TrimString(AdapterInfo.AdapterName)
Debug.Print "Description : "; TrimString(Buffer2.Description)
Select Case Buffer2.dwType
Case MIB_IF_TYPE_ETHERNET
Debug.Print "Type : "; "Ethernet adapter"
Case MIB_IF_TYPE_TOKENRING
Debug.Print "Type : "; "Token ring adapter"
Case MIB_IF_TYPE_FDDI
Debug.Print "Type : "; "FDDI adapter"
Case MIB_IF_TYPE_PPP
Debug.Print "Type : "; "PPP adapter"
Case MIB_IF_TYPE_LOOPBACK
Debug.Print "Type : "; "Loopback adapter"
Case MIB_IF_TYPE_SLIP
Debug.Print "Type : "; "Slip adapter"
Case Else
Debug.Print "Type : "; "Other adapter"
End Select
Debug.Print "MAC Address : ";
For I = 0 To Buffer2.AddressLength - 1
If Len(Hex(Buffer2.Address(I))) = 1 Then
Debug.Print "0"; Hex(Buffer2.Address(I));
Else
Debug.Print Hex(Buffer2.Address(I));
End If
If I < Buffer2.AddressLength - 1 Then Debug.Print "-";
Next
Debug.Print ""
If Buffer2.DhcpEnabled Then
Debug.Print "DHCP : "; "Enabled"
Else
Debug.Print "DHCP : "; "Disabled"
End If
Debug.Print "IP Address : "; TrimString(Buffer2.IpAddressList.IpAddress)
Debug.Print "Subnet Mask : "; TrimString(Buffer2.IpAddressList.IpMask)
pAddrStr = Buffer2.IpAddressList.dwNext
Do While pAddrStr <> 0
'NOTE : I haven't tested this
CopyMemory Buffer, Buffer2.IpAddressList, Len(Buffer)
Debug.Print "IP Address : "; TrimString(Buffer.IpAddress)
Debug.Print "Subnet Mask : "; TrimString(Buffer.IpMask)
pAddrStr = Buffer.dwNext
If pAddrStr <> 0 Then CopyMemory Buffer2.IpAddressList, ByVal pAddrStr, Len(Buffer2.IpAddressList)
Loop
Debug.Print "Default Gateway : "; TrimString(Buffer2.GatewayList.IpAddress)
If TrimString(Buffer2.GatewayList.IpAddress) <> "0.0.0.0" Then GatewayIP = TrimString(Buffer2.GatewayList.IpAddress)
pAddrStr = Buffer2.GatewayList.dwNext
Do While pAddrStr <> 0
CopyMemory Buffer, Buffer2.GatewayList, Len(Buffer)
pAddrStr = Buffer.dwNext
If pAddrStr <> 0 Then CopyMemory Buffer2.GatewayList, ByVal pAddrStr, Len(Buffer2.GatewayList)
Loop
Debug.Print "DHCP Server : "; TrimString(Buffer2.DhcpServer.IpAddress)
Debug.Print "Primary WINS Server : "; TrimString(Buffer2.PrimaryWinsServer.IpAddress)
Debug.Print "Secondary WINS Server : "; TrimString(Buffer2.SecondaryWinsServer.IpAddress)
NewTime = DateAdd("s", Buffer2.LeaseObtained, #1/1/1970#)
Debug.Print "Lease Obtained : "; CStr(Format(NewTime, "dddd,mmm d hh:mm:ss yyyy"))
NewTime = DateAdd("s", Buffer2.LeaseExpires, #1/1/1970#)
Debug.Print "Lease Expires : "; CStr(Format(NewTime, "dddd,mmm d hh:mm:ss yyyy"))
pAdapt = Buffer2.dwNext
If pAdapt <> 0 Then CopyMemory AdapterInfo, ByVal pAdapt, Len(AdapterInfo)
Loop While pAdapt <> 0
GlobalFree AdapterInfoBuffer
End Sub
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
请问我的网络一天了找个部门到现在没有解决请问我上哪投诉? 计算机具备哪几种功能? 计算机有哪些功能是什么 歌词中有丑姑娘的歌曲 驾照不年审会怎样 没开过车驾照年审会通过吗 本来白色的背心放入洗衣机清洗后就变成有黑色斑点 其他一起洗的衣物... ...衣服颜色保持鲜艳(主要是白色衣服洗几次感觉就有点颜色不亮丽了... 为什么登陆lol会叫你解绑 小姨子告诉我说,炒菜时记住这4个小技巧,就是比别人做的好吃 能治好强直性脊柱炎吗或控制病情发展【强直性脊柱炎】 vb试题,谢谢大家了 vb redim 什么意思 急 用vb制作db数据库文件 有关于VB对内存读写的操作,那位帮帮忙 ReDim Preserve 的用途 我想摆摊手机贴膜的 有哪些步骤??? vb下雪的代码? 我摆摊手机贴膜,是新手,摆了一个星期,好几天一个生意没有,我看别人一天能贴五,六张。啥原因??? vb 如何定义一个未知大小一维数组? win7屏保后打不开,电脑工作正常,这样才能解开锁定状态啊? WIN7屏幕保护程序出来,按键盘上的键都不出桌面,怎么回事? 求助!!!求两片耽美文!! 代父子文。年上,帝王家的,受是双性的。。。其他的忘记了? 请问 这季节在什么水位能钓到鲢鱼?我钓15天了,是用蚯蚓,为什么鲢鱼不吃钓?能告诉我最简单的饵料怎做吗 什么鱼&#xF41F;不能吃蚯蚓? 求文!!腹黑攻单纯受,忠犬攻女王受 秋季,冬季,草鱼,鲢鱼吃什么呢 用蚯蚓能钓到鲢鳙吗 强受双性文。反正就是要强受708818104@qq.com 类似小秦子的文。像荒唐,风骨之类的。。 钩蚯蚓钓起鲢鱼预示着什么? VB读取Text到二维数组 求帮助。代码解释一下最好了。 Vb初学者 如何为reportviewer指定打印机而不用系统默认的? 为什么朋友生日祝福语红色礼盒朋友会生气? 和男朋友一起去参加朋友的生日聚会穿红色和绿色配么? 红色节日有哪些?各有什么历史? 什么地方过生日把鸡蛋染成红色谢谢 分手了,女人生日,男人送33朵红色玫瑰花代表什么意思? 造梦西游ol后期哪个人物最厉害 《造梦西游》ol公认最强角色是什么? 造梦西游ol为什么沙僧后期强 造梦西游ol谁厉害 造梦西游OL什么角色厉害 各职业特点分析攻略详解 造梦西游ol那个角色后期最强大 造梦西游ol 那个角色pk最叼? 造梦西游ol,各个各个职业介绍,刷图方法 三星note3怎么长截屏 鬼谷子百数第五十五数意思 嗨,你好:《易经》中,大衍之数是五十五,还是五十?贝体演卦全过程,不妨指导可好?依据卦怎样推断未来 大衍之数55还是50 懂易经的同仁;天地之数五十有五,为何只取四十九?