概括VB.NET获取网卡地址的步骤
创始人
2024-06-08 22:41:06
0

VB.NET还是比较常用的,于是我研究了一下VB.NET获取网卡地址,在这里拿出来和大家分享一下,希望对大家有用。

程序语言:Microsoft Visual Basic 4.0,5.0,6.0
运行平台:WINDOWS
功能描述:IPX和NETBIOS接口需要网络地址。该文通过详细的步骤演示了如何通过VB.NET获取网卡地址。

VB.NET获取网卡地址步骤:

1.在Visual Basic生成标准的EXE文件。缺省创建 Form1.

2.在Form1中添加一命令按钮,缺省名为Command1.

3.把下列代码放到Form1中说明部分。

  1. Option Explicit   
  2. Private Const NCBASTAT = &H33   
  3. Private Const NCBNAMSZ = 16   
  4. Private Const HEAP_ZERO_MEMORY = &H8   
  5. Private Const HEAP_GENERATE_EXCEPTIONS = &H4   
  6. Private Const NCBRESET = &H32   
  7. Private Type NCB   
  8. ncb_command As Byte 'Integer   
  9. ncb_retcode As Byte 'Integer   
  10. ncb_lsn As Byte 'Integer   
  11. ncb_num As Byte ' Integer   
  12. ncb_buffer As Long 'String   
  13. ncb_length As Integer   
  14. ncb_callname As String * NCBNAMSZ   
  15. ncb_name As String * NCBNAMSZ   
  16. ncb_rto As Byte 'Integer   
  17. ncb_sto As Byte ' Integer   
  18. ncb_post As Long   
  19. ncb_lana_num As Byte 'Integer   
  20. ncb_cmd_cplt As Byte 'Integer   
  21. ncb_reserve(9) As Byte ' Reserved, must be 0   
  22. ncb_event As Long   
  23. End Type  
  24.  
  25. Private Type ADAPTER_STATUS   
  26. adapter_address(5) As Byte 'As String * 6   
  27. rev_major As Byte 'Integer   
  28. reserved0 As Byte 'Integer   
  29. adapter_type As Byte 'Integer   
  30. rev_minor As Byte 'Integer   
  31. duration As Integer   
  32. frmr_recv As Integer   
  33. frmr_xmit As Integer   
  34. iframe_recv_err As Integer   
  35. xmit_aborts As Integer   
  36. xmit_success As Long   
  37. recv_success As Long   
  38. iframe_xmit_err As Integer   
  39. recv_buff_unavail As Integer   
  40. t1_timeouts As Integer   
  41. ti_timeouts As Integer   
  42. Reserved1 As Long   
  43. free_ncbs As Integer   
  44. max_cfg_ncbs As Integer   
  45. max_ncbs As Integer   
  46. xmit_buf_unavail As Integer   
  47. max_dgram_size As Integer   
  48. pending_sess As Integer   
  49. max_cfg_sess As Integer   
  50. max_sess As Integer   
  51. max_sess_pkt_size As Integer   
  52. name_count As Integer   
  53. End Type   
  54.  
  55. Private Type NAME_BUFFER   
  56. name As String * NCBNAMSZ   
  57. name_num As Integer   
  58. name_flags As Integer   
  59. End Type  
  60.  
  61. Private Type ASTAT   
  62. adapt As ADAPTER_STATUS   
  63. NameBuff(30) As NAME_BUFFER   
  64. End Type   
  65.  
  66. Private Declare Function Netbios Lib "netapi32.dll" (pncb As NCB) As Byte   
  67. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _  
  68. (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)   
  69. Private Declare Function GetProcessHeap Lib "kernel32" () As Long   
  70. Private Declare Function HeapAlloc Lib "kernel32" _   
  71. (ByVal hHeap As Long, ByVal dwFlags As Long, _   
  72. ByVal dwBytes As Long) As Long   
  73. Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long,_   
  74. ByVal dwFlags As Long, lpMem As Any) As Long 

把下面的代码放入Command1_Click的事件中:

  1. Private Sub Command1_Click()   
  2. Dim myNcb As NCB   
  3. Dim bRet As Byte   
  4. myNcb.ncb_command = NCBRESET   
  5. bRet = Netbios(myNcb)   
  6. myNcb.ncb_command = NCBASTAT   
  7. myNcb.ncb_lana_num = 0   
  8. myNcb.ncb_callname = "* "   
  9. Dim myASTAT As ASTAT, tempASTAT As ASTAT   
  10. Dim pASTAT As Long   
  11. myNcb.ncb_length = Len(myASTAT)   
  12. Debug.Print Err.LastDllError   
  13. pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS _   
  14. Or HEAP_ZERO_MEMORY, myNcb.ncb_length)   
  15. If pASTAT = 0 Then   
  16. Debug.Print "memory allcoation failed!"   
  17. Exit Sub   
  18. End If  
  19.  
  20. myNcb.ncb_buffer = pASTAT   
  21. bRet = Netbios(myNcb)   
  22. Debug.Print Err.LastDllError   
  23. CopyMemory myASTAT, myNcb.ncb_buffer, Len(myASTAT)   
  24. MsgBox Hex(myASTAT.adapt.adapter_address(0)) & " " & _   
  25. Hex(myASTAT.adapt.adapter_address(1)) _   
  26. & " " & Hex(myASTAT.adapt.adapter_address(2)) & " " _   
  27. & Hex(myASTAT.adapt.adapter_address(3)) _   
  28. & " " & Hex(myASTAT.adapt.adapter_address(4)) & " " _   
  29. & Hex(myASTAT.adapt.adapter_address(5))   
  30. HeapFree GetProcessHeap(), 0, pASTAT   
  31. End Sub  

4.按F5,运行该程序。

5.点击Command1。注意,VB.NET获取网卡地址将在一信息框中显示出来。

【编辑推荐】

  1. 概述VB.NET Option Strict
  2. 详细描述VB.NET PadLeft方法
  3. 简单分析VB.NET临时文件
  4. 概括VB.NET日期类型的值运算
  5. 浅析VB.NET Imports语句

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...