界面:
控件类型 控件名
ListBox List1
TextBox Text1
TextBox Text2
CommandButton Command1
CommandButton Command2
窗体代码:
'=========================================== 'Desc:获取本机IP地址和子网掩码 'Author:yoyoba(stuyou@126.com) 'Date:2007-11-28 'Modify:2007-12-6 '=========================================== Option Explicit Public WithEvents vpcap As vbPacket '声明一个使用vbpcap事件的vpcap对象变量 Dim AdapterString As String * 1000 'GetAdapterNames函数返回的网卡名,存放于该变量 Dim selAdapterName As String '在List列表中被选中网卡的名字 Private Sub Command2_Click() vpcap.CloseAdapter End End Sub Private Sub Form_Load() Set vpcap = New vbPacket '为对象变量vpcap赋值 Dim NumAdapter As Long 'GetAdapterNames函数返回的网卡数,存放于该变量 Dim AdapterList Dim i As Integer NumAdapter = vpcap.GetAdapterNames(AdapterString) '调用GetAdapterNames函数。获得本机网卡名字 If NumAdapter = 0 Then MsgBox "Founded NONE adapter!,please check it!" End If AdapterList = Split(AdapterString, vbNullChar) '把获得得网卡名置于List控件中 For i = 0 To NumAdapter List1.AddItem AdapterList(i) Next i End Sub Private Sub Command1_Click()
Dim NetIP As Long '返回该网卡的IP Dim NetMask As Long '返回该网卡的掩码 selAdapterName = List1.Text 'SetAdapterName为在List中选中的网卡 If selAdapterName = "" Then MsgBox "You have not selected a adapter!" Else vpcap.GetNetInfo selAdapterName, NetIP, NetMask '调用GetNetInfo函数,获取IP和submask Text1.Text = GetIpFromLong(NetIP) '调用在getip_4byte模块中定义的GetIpFromLong函数,_ '把Long型的NetIp转换成4字节表示的IP地址表示法 Text2.Text = GetIpFromLong(NetMask) End If End Sub
|
标准模块(mdl3.bas)代码:
Option Explicit Public Declare Sub memcpy Lib "kernel32" Alias _ "RtlMoveMemory" (ByRef Destination As Any, _ ByRef Source As Any, ByVal Length As Long) '调用WindowsAPI,使用memcpy库中的函数,把一个4字节的long数据转换成4个byte数据, '_该声明必须放在模块或类中,不能放在窗体中 '===================================================== 'Name:GetIpFromLong 'Desc:把一个long型数据表示的IP地址转换成点分10进制表示 'Parameters:[in]lngIPAddress:以long型数据表示的IP地址 'Return: 点分10进制表示的IP地址,String类型 'Author:yoyoba(stuyou@126.com) 'Date:2007-11-28 'Modify:2007-11-28 '===================================================== Public Function GetIpFromLong(lngIPAddress As Long) As String '声明函数,用来把4字节的long数据转换成4个byte数据 Dim arrIpParts(3) As Byte memcpy arrIpParts(0), lngIPAddress, 4 '调用memcpy函数 GetIpFromLong = CStr(arrIpParts(3)) & "." & _ CStr(arrIpParts(2)) & "." & _ CStr(arrIpParts(1)) & "." & _ CStr(arrIpParts(0)) End Function
|
阅读(2281) | 评论(0) | 转发(0) |