2 gaidar
Ага !
Только вот маленькое отличие
ipconfig выдаёт мне имя соединения напр " Подключение к интернет 5"
и это то что мне нужно
а приведенный Вами код выдаёт вместо имени - хэш,
который кстати меняется от раза к разу
Ethernet adapter {4BA8E11F-67EC-4951-A19A-208897A9F61E}
Попробуйте, внизу код для консольного приложения которое использует приведеные Вами классы
(Всё это можно кстати гораздо проще через System.Net.Networkinformation достать)
Но проблема не решается.
Я не могу сказать однозначно из приведенного Вами кода какой виртуальный интерфейс породило напр " Подключение к интернет 5"
или наоборот. В ipconfig /ALL - это видно, а в результатах работы этого кода вместо имени идёт Ethernet adapter {4BA8E11F-67EC-4951-A19A-208897A9F61E}
- Код: Выделить всё
'Use the following syntax to get the result
'<name of the exe>.exe -l
' http://www.dotnet247.com/247reference/msgs/18/91898.aspx
Imports System.Runtime.InteropServices
Public Class App
'define usage of the program
Public Shared Sub usage()
Console.WriteLine("Usage: Iprenew [ -l ] [ -r<index id> ] [-n<index id>]")
Console.WriteLine(vbTab & "-l List adapters with corresponding index ID and other information")
Console.WriteLine(vbTab & "-r Release IP address for adapter index ID")
Console.WriteLine(vbTab & "-n Renew IP address for adapter index ID")
End Sub
Public Shared Sub Main(ByVal args() As String)
'used while invoking win32 API's
Dim retValue As Integer
Dim size As Integer
'for command line arguments
Dim optList As Boolean = False
Dim optRelease As Boolean = False
Dim optRenew As Boolean = False
Dim temp As String
'to check if adapter is of type Ethernet Adapter
Dim ifEthernet As Boolean = False
'index for which address is released or renewed
Dim index As Integer = 0
'local variable
Dim i As Integer
If args.Length = 0 Then
usage()
Return
End If
'checking for command line arguments
For i = 0 To args.Length - 1
If ((args(i).Chars(0) = "-") Or (args(i).Chars(0) = "/")) Then
Select Case args(i).Chars(1)
Case "l"
'to list all adapter information
optList = True
Case "r"
'to release IP address for given index
optRelease = True
If (args(i).Length >= 2) Then
temp = args(i).Substring(2, args(i).Length - 2)
index = Int32.Parse(temp)
Else
usage()
End If
Case "n"
'to renew IP address for given index
optRenew = True
If (args(i).Length >= 2) Then
temp = args(i).Substring(2, args(i).Length - 2)
index = Int32.Parse(temp)
Else
usage()
End If
Case Else
usage()
Return
End Select
End If
Next i
If (optRelease Or optRenew) Then
Console.WriteLine("Given Adapter Index : " & index)
End If
'print all the network adapter information
If (optList) Then
'since Byte[] is class, we can pass Nothing as parameter
'to get the required buffer size
retValue = LibWrap.GetNetworkParams(Nothing, size)
'checking for error
If (retValue <> IPConfigConst.ERROR_SUCCESS And _
retValue <> IPConfigConst.ERROR_BUFFER_OVERFLOW) Then
Console.WriteLine("Error invoking GetNetworkParams() : " + retValue)
Return
End If
'creating a buffer with required size
Dim PFixedInfoBuffer(size) As Byte
'Invoking GetNetworkParams()
retValue = LibWrap.GetNetworkParams(PFixedInfoBuffer, size)
If (retValue <> IPConfigConst.ERROR_SUCCESS) Then
Console.WriteLine("Error invoking GetNetworkParams() " + retValue)
Return
End If
Dim PFixedInfo As New FixedInfo()
'copy from the Buffer to the FIXED_INFO structure
LibWrap.CopyMemoryFixedInfo(PFixedInfo, PFixedInfoBuffer, _
Marshal.SizeOf(PFixedInfo))
Console.WriteLine("Windows IP Configuration:")
Console.WriteLine()
Console.WriteLine(vbTab & vbTab & "HostName....................: " + _
PFixedInfo.HostName)
Console.WriteLine(vbTab & vbTab & "DomainName..................: " + _
PFixedInfo.DomainName)
Dim List As New IPAddrString()
Dim ListNext As New IntPtr()
'Linked list of IP_ADDR_STRING structures that
'specify the set of DNS servers used by the local computer.
Console.Write(vbTab & vbTab & "DnsServerList............... : ")
Console.WriteLine(PFixedInfo.DnsServerList.IPAddressString)
ListNext = PFixedInfo.DnsServerList.NextPointer
While (ListNext.ToInt32() <> 0)
LibWrap.CopyMemoryIPAddrString(List, ListNext, Marshal.SizeOf(List))
Console.WriteLine(vbTab & vbTab & _
vbTab & vbTab & vbTab & _
vbTab & List.IPAddressString)
ListNext = List.NextPointer
End While
Select Case (PFixedInfo.NodeType)
Case 1
Console.WriteLine(vbTab & vbTab & "NodeType................... : Broadcast")
Case 2
Console.WriteLine(vbTab & vbTab & "NodeType................... : Peer to Peer")
Case 4
Console.WriteLine(vbTab & vbTab & "NodeType................... : Mixed")
Case 8
Console.WriteLine(vbTab & vbTab & "NodeType................... : Hybrid")
Case Else
Console.WriteLine(vbTab & vbTab & "NodeType................... : Unknown")
End Select
If (PFixedInfo.EnableRouting <> 0) Then
Console.WriteLine(vbTab & vbTab & "IP RoutingEnabled.......... : Yes")
Else
Console.WriteLine(vbTab & vbTab & "IP Routingenabled.......... : No")
End If
If (PFixedInfo.EnableProxy <> 0) Then
Console.WriteLine(vbTab & vbTab & "WINS Proxy Enabled.......... : Yes")
Else
Console.WriteLine(vbTab & vbTab & "WINS Proxy notenabled...... : No")
End If
If (PFixedInfo.EnableDns <> 0) Then
Console.WriteLine(vbTab & vbTab & "NetBIOS Resolution Uses DNS: Yes")
Else
Console.WriteLine(vbTab & vbTab & "NetBIOS Resolution UsesDNS: No")
End If
Console.WriteLine()
Console.WriteLine()
're-intializing the size
size = 0
'since Byte[] is class, we can pass null as parameter
'to get the required buffer size
retValue = LibWrap.GetAdaptersInfo(Nothing, size)
'checking for error
If (retValue <> IPConfigConst.ERROR_SUCCESS And _
retValue <> IPConfigConst.ERROR_BUFFER_OVERFLOW) Then
Console.WriteLine("Error invoking GetAdaptersInfo() : " + retValue)
Return
End If
Dim IPAdapterInfoBuffer(size) As Byte
Dim PAdapterInfo As New IPAdapterInfo()
'Invoking GetAdapterInfo()
retValue = LibWrap.GetAdaptersInfo(IPAdapterInfoBuffer, size)
'checking for error
If (retValue <> IPConfigConst.ERROR_SUCCESS) Then
Console.WriteLine("Error invoking GetAdaptersInfo() : " + retValue)
Return
End If
'copy from the Buffer to the IP_ADAPTER_INFO structure
LibWrap.CopyMemoryIPAdapterInfo(PAdapterInfo, IPAdapterInfoBuffer, _
Marshal.SizeOf(PAdapterInfo))
'pointing next block for IP_ADAPTER_INFO
Dim AdapterInfoNext As New IntPtr()
Do
Select Case PAdapterInfo.Type
Case IPAdapterTypes.MIB_IF_TYPE_ETHERNET
Console.Write("Ethernet adapter ")
ifEthernet = True
Case IPAdapterTypes.MIB_IF_TYPE_TOKENRING
Console.Write("Token Ring adapter ")
Case IPAdapterTypes.MIB_IF_TYPE_FDDI
Console.Write("FDDI adapter ")
Case IPAdapterTypes.MIB_IF_TYPE_PPP
Console.Write("PPP adapter ")
Case IPAdapterTypes.MIB_IF_TYPE_LOOPBACK
Console.Write("Loopback adapter ")
Case IPAdapterTypes.MIB_IF_TYPE_SLIP
Console.Write("Slip adapter ")
Case IPAdapterTypes.MIB_IF_TYPE_OTHER
Console.Write("Other type of adapter")
Case Else
Console.Write("Other adapter ")
End Select
Console.WriteLine(PAdapterInfo.AdapterName & vbCr)
Console.WriteLine(vbTab & vbTab & "AdapterIndex............... : " + _
PAdapterInfo.Index.ToString())
Console.WriteLine(vbTab & vbTab & "Description................. : " + _
PAdapterInfo.Description)
Console.Write(vbTab & vbTab & "Physical Address............: ")
Dim l As Integer
For l = 0 To PAdapterInfo.AddressLength - 1
If (l = PAdapterInfo.AddressLength - 1) Then
Console.WriteLine(Hex(PAdapterInfo.Address(l)))
Else
Console.Write(Hex(PAdapterInfo.Address(l)) + "-")
End If
Next l
If (PAdapterInfo.DhcpEnabled <> 0) Then
Console.WriteLine(vbTab & vbTab & "DHCP Enabled................ : Yes")
Else
Console.WriteLine(vbTab & vbTab & "DHCP Enabled................ : No")
End If
'IP Address list
Console.WriteLine(vbTab & vbTab & "IPAddress.................. : " + _
PAdapterInfo.IPAddressList.IPAddressString)
Console.WriteLine(vbTab & vbTab & "Subnet Mask................. : " + _
PAdapterInfo.IPAddressList.IPMaskString)
Dim PIPList As New IntPtr()
PIPList = PAdapterInfo.IPAddressList.NextPointer
Dim IPAddressList As New IPAddrString()
While (PIPList.ToInt32() <> 0)
LibWrap.CopyMemoryIPAddrString(IPAddressList, PIPList, _
Marshal.SizeOf(IPAddressList))
Console.WriteLine(vbTab & vbTab & "IPAddress.................. : " _
+ IPAddressList.IPAddressString)
Console.WriteLine(vbTab & vbTab & "SubnetMask................. : " _
+ IPAddressList.IPMaskString)
PIPList = IPAddressList.NextPointer
End While
'Gateway List
Console.WriteLine(vbTab & vbTab & " Default Gateway............. : " + PAdapterInfo.GatewayList.IPAddressString)
Dim PGateway As New IntPtr()
PGateway = PAdapterInfo.GatewayList.NextPointer
Dim IPGatewayList As New IPAddrString()
While (PGateway.ToInt32 <> 0)
LibWrap.CopyMemoryIPAddrString(IPGatewayList, PGateway, Marshal.SizeOf(IPGatewayList))
Console.WriteLine(vbTab & vbTab & "GatewayAddress............. : " + IPGatewayList.IPAddressString)
PGateway = IPGatewayList.NextPointer
End While
Console.WriteLine(vbTab & vbTab & "DHCP Server................. : " + PAdapterInfo.DhcpServer.IPAddressString)
Console.WriteLine(vbTab & vbTab & "Primary WINS Server......... : " + PAdapterInfo.PrimaryWinsServer.IPAddressString)
Console.WriteLine(vbTab & vbTab & "Gateway Address............. : " + PAdapterInfo.SecondaryWinsServer.IPAddressString)
'date and time when Lease is obtained and expired
If (ifEthernet) Then
Dim LeaseObt = New DateTime(1970, 1, 1)
Dim LeaseExp = New DateTime(1970, 1, 1)
LeaseObt = LeaseObt.AddSeconds(PAdapterInfo.LeaseObtained)
LeaseExp = LeaseExp.AddSeconds(PAdapterInfo.LeaseExpires)
Console.WriteLine(vbTab & vbTab & "Lease Obtained.............. : " + LeaseObt.ToString())
Console.WriteLine(vbTab & vbTab & "Lease Expires............... : " + LeaseExp.ToString())
End If
'setting it to next Adapter information
AdapterInfoNext = PAdapterInfo.NextPointer
If (AdapterInfoNext.ToInt32 <> 0) Then
LibWrap.CopyMemoryIPAdapterInfoP(PAdapterInfo, AdapterInfoNext, Marshal.SizeOf(PAdapterInfo))
End If
Console.WriteLine()
Console.WriteLine()
Loop While (AdapterInfoNext.ToInt32 <> 0)
'Depending on the number of adapters
End If
'Re-initializing the size
size = 0
'if IP Release or Renew
If (optRelease Or optRenew) Then
'since Byte[] is class, we can pass null as parameter
'to get the required buffer size
LibWrap.GetInterfaceInfo(Nothing, size)
'Checking for error
If (retValue <> IPConfigConst.ERROR_SUCCESS And _
retValue <> IPConfigConst.ERROR_BUFFER_OVERFLOW) Then
Console.WriteLine("Error invoking GetInterfaceInfo() : " + retValue)
Return
End If
Dim PIfTableBuffer(size) As Byte
Dim PIfTable As New IPInterfaceInfo()
'invoking GetInterfaceInfo()
retValue = LibWrap.GetInterfaceInfo(PIfTableBuffer, size)
'checking for error
If (retValue <> IPConfigConst.ERROR_SUCCESS) Then
Console.WriteLine("Error Value from GetInterfaceInfo(): " + retValue)
Return
End If
Dim NumAdapters As Integer = 0
Dim byteCount As Integer = 0
'copy from PIfTableBuffer to NumAdapters
LibWrap.CopyMemoryInt(NumAdapters, PIfTableBuffer(byteCount), 4)
byteCount = byteCount + 4
If (NumAdapters = 0) Then
Console.WriteLine("No Adpaters found")
Return
End If
Dim PIPAdapterIndexMap As New IPAdapterIndexMap()
For i = 0 To NumAdapters - 1
'copy from PIfTableBuffer to IP_ADAPTER_INDEX_MAP Structure
LibWrap.CopyMemoryIPAdapterIndexMap(PIPAdapterIndexMap, PIfTableBuffer(byteCount), Marshal.SizeOf(PIPAdapterIndexMap))
byteCount = byteCount + Marshal.SizeOf(PIPAdapterIndexMap)
'checking for index number
If (index = PIPAdapterIndexMap.Index) Then
'if IP Release
If (optRelease) Then
retValue = LibWrap.IpReleaseAddress(PIPAdapterIndexMap)
If (retValue <> IPConfigConst.ERROR_SUCCESS) Then
Console.WriteLine("Error invoking IPRelease: " & retValue)
Return
End If
Console.WriteLine("IP Address Released....")
End If
'if IP Renew
If (optRenew) Then
retValue = LibWrap.IpRenewAddress(PIPAdapterIndexMap)
If (retValue <> IPConfigConst.ERROR_SUCCESS) Then
Console.WriteLine("Error invoking IPRenew: " & retValue)
Return
End If
Console.WriteLine("IP Address Renewed....")
End If
End If
Next i
End If
End Sub
End Class