vb.net 第八节 获取本机网络参数 GetNetworkParams

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb.net 第八节 获取本机网络参数 GetNetworkParams相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

Imports System.Runtime.InteropServices

Public Class Form9

    'typedef struct _IP_ADDR_STRING 
    '  struct _IP_ADDR_STRING* Next;
    '  IP_ADDRESS_STRING IpAddress;
    '  IP_MASK_STRING IpMask;
    '  DWORD Context;
    ' IP_ADDR_STRING, *PIP_ADDR_STRING;

    'typedef struct 
    '  Char            HostName[MAX_HOSTNAME_LEN + 4];
    '  Char            DomainName[MAX_DOMAIN_NAME_LEN + 4];
    '  PIP_ADDR_STRING CurrentDnsServer;
    '  IP_ADDR_STRING  DnsServerList;
    '  UINT            NodeType;
    '  Char            ScopeId[MAX_SCOPE_ID_LEN + 4];
    '  UINT            EnableRouting;
    '  UINT            EnableProxy;
    '  UINT            EnableDns;
    ' FIXED_INFO, *PFIXED_INFO;


    'Const MAX_HOSTNAME_LEN = 132
    'Const MAX_DOMAIN_NAME_LEN = 132
    'Const MAX_SCOPE_ID_LEN = 260

    'Type IP_ADDR_STRING
    '    Next As Long		'指向列表中下一个IP_ADDR_STRING类型。为空,则是最后一个地址
    '    IpAddress As String * 16	'点式十进制字串表示Ip地址
    '    IpMask As String * 16		'子网掩码
    '    Context As Long			'网络IP地址标识,符合AddIPAddress和DeleteIPAddress函数中的网络接口关联参数。
    'End Type

    'Type FIXED_INFO
    '    HostName As String * MAX_HOSTNAME_LEN	'本机名
    '    DomainName As String * MAX_DOMAIN_NAME_LEN	'本机DNS域
    '    CurrentDnsServer As Long		'保留,使用DnsServerList获取DNS服务器的IP地址
    '    DnsServerList As IP_ADDR_STRING	'本机采用的DNS服务器链表
    '    NodeType As Long		'节点类型,以下值:
    '        1       BROADCAST_NODETYPE 		IP广播,b节点NetBios名字解析法
    '        2       PEER_TO_PEER_NODETYPE 	点到点通信,p节点名字解析
    '        4       MIXED_NODETYPE				Mixed,m节点NetBIOS名字解析法
    '        8       HYBRID_NODETYPE 			Hybrid,h节点NetBIOS名字解析法
    '        Else									未知节点类型
    '    ScopeId  As String * MAX_SCOPE_ID_LEN	'DHCP范围名字
    '    EnableRouting As Long	'本机能否路由
    '    EnableProxy As Long	'本机能否作为地址解析协议(ARP)代理
    '    EnableDns As Long	'本机能否DNS查询
    'End Type


    Const MAX_HOSTNAME_LEN = 128
    Const MAX_DOMAIN_NAME_LEN = 128
    Const MAX_SCOPE_ID_LEN = 256

    <StructLayout(LayoutKind.Sequential)>
    Public Structure IP_ADDR_STRING
        Public [Next] As IntPtr     '指向列表中下一个IP_ADDR_STRING类型。为空,则是最后一个地址
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=16)> Public IpAddress() As Char     '点式十进制字串表示Ip地址
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=16)> Public IpMask() As Char       '子网掩码
        Public Context As Integer      '网络IP地址标识,符合AddIPAddress和DeleteIPAddress函数中的网络接口关联参数。
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Public Structure FIXED_INFO
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_HOSTNAME_LEN + 4)> Public HostName As String    '本机名
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_DOMAIN_NAME_LEN + 4)> Public DomainName As String   '本机DNS域
        Public CurrentDnsServer As Integer        '保留,使用DnsServerList获取DNS服务器的IP地址
        Public DnsServerList As IP_ADDR_STRING '本机采用的DNS服务器链表
        Public NodeType As Integer     '节点类型,以下值:
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_SCOPE_ID_LEN + 4)> Public ScopeId As String     'DHCP范围名字
        Public EnableRouting As Integer   '本机能否路由
        Public EnableProxy As Integer '本机能否作为地址解析协议(ARP)代理
        Public EnableDns As Integer    '本机能否DNS查询
    End Structure

    'Private Declare Function GetNetworkParams Lib "IPHlpApi.dll" (FixedInfo As Any, pOutBufLen As Long) As Long
    Private Declare Function GetNetworkParams Lib "iphlpapi.dll" (ByRef FixedInfo As IntPtr, ByRef pOutBufLen As Integer) As Integer
    Private Declare Function GetNetworkParams Lib "iphlpapi.dll" (ByVal FixedInfo() As Byte, ByRef pOutBufLen As Integer) As Integer
    Private Declare Function GetNetworkParams Lib "iphlpapi.dll" (ByRef FixedInfo As FIXED_INFO, ByRef pOutBufLen As Integer) As Integer


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ptrFixedInfo As IntPtr = IntPtr.Zero
        Dim lenOutBufLen As Integer

        Dim returnValue As Integer

        returnValue = GetNetworkParams(ptrFixedInfo, lenOutBufLen)

        Dim bufFixedInfo(lenOutBufLen - 1) As Byte
        returnValue = GetNetworkParams(bufFixedInfo, lenOutBufLen)

        Dim pFixedInfo As IntPtr = Marshal.AllocHGlobal(lenOutBufLen)

        Dim FIXEDINFO As FIXED_INFO
        Dim sizeFIXEDINFO As Integer = Marshal.SizeOf(FIXEDINFO)
        Marshal.Copy(bufFixedInfo, 0, pFixedInfo, sizeFIXEDINFO)

        FIXEDINFO = Marshal.PtrToStructure(pFixedInfo, GetType(FIXED_INFO))

        TextBox1.Text = FixedInfo.HostName & vbCrLf

        TextBox1.Text &= "DNS地址:" & vbCrLf
        Dim ipaddrDNS As IntPtr = FixedInfo.DnsServerList

        Do While True

            If ipaddrDNS = IntPtr.Zero Then
                Exit Do
            End If

            Dim dns As New IP_ADDR_STRING
            dns = Marshal.PtrToStructure(ipaddrDNS, GetType(IP_ADDR_STRING))

            Dim ipaddrIpAddress As New String(dns.IpAddress)
            Dim ipmaskIpAddress As New String(dns.IpMask)
            TextBox1.Text &= ipaddrIpAddress.Replace(Chr(0), "") & " " & ipmaskIpAddress.Replace(Chr(0), "") & vbCrLf
            ipaddrDNS = dns.Next


    End Sub
End Class

 

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于vb.net 第八节 获取本机网络参数 GetNetworkParams的主要内容,如果未能解决你的问题,请参考以下文章

网络层-第八节:网际控制报文协议ICMP

vb.net 第一节 获取本机网络适配器的信息 GetAdaptersInfo

传输层-第八节2:TCP连接管理实践部分

第八节

传输层-第八节1:TCP连接管理理论部分(三次握手与四次挥手)

传输层-第八节2:TCP连接管理实践部分