VB.NET获取网卡地址代码实现过程
创始人
2024-06-20 20:21:47
0

VB.NET开发语言是一款面向对象的功能强大的编程语言。它的应用可以帮助开发人员实现许多功能需求。比如VB.NET获取网卡地址等。下面就会针对这一方法进行一个详细的解读,希望对大家有所帮助。#t#

程序语言: 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. Private Type ADAPTER_STATUS   
  25. adapter_address(5) As Byte 'As String * 6   
  26. rev_major As Byte 'Integer   
  27. reserved0 As Byte 'Integer   
  28. adapter_type As Byte 'Integer   
  29. rev_minor As Byte 'Integer   
  30. duration As Integer   
  31. frmr_recv As Integer   
  32. frmr_xmit As Integer   
  33. iframe_recv_err As Integer   
  34. xmit_aborts As Integer   
  35. xmit_success As Long   
  36. recv_success As Long   
  37. iframe_xmit_err As Integer   
  38. recv_buff_unavail As Integer   
  39. t1_timeouts As Integer   
  40. ti_timeouts As Integer   
  41. Reserved1 As Long   
  42. free_ncbs As Integer   
  43. max_cfg_ncbs As Integer   
  44. max_ncbs As Integer   
  45. xmit_buf_unavail As Integer   
  46. max_dgram_size As Integer   
  47. pending_sess As Integer   
  48. max_cfg_sess As Integer   
  49. max_sess As Integer   
  50. max_sess_pkt_size As Integer   
  51. name_count As Integer   
  52. End Type   
  53. Private Type NAME_BUFFER   
  54. name As String * NCBNAMSZ   
  55. name_num As Integer   
  56. name_flags As Integer   
  57. End Type   
  58. Private Type ASTAT   
  59. adapt As ADAPTER_STATUS   
  60. NameBuff(30) As NAME_BUFFER   
  61. End Type   
  62. Private Declare Function Netbios 
    Lib "netapi32.dll" (pncb As NCB) As Byte   
  63. Private Declare Sub CopyMemory Lib 
    "kernel32" Alias "RtlMoveMemory" _   
  64. (hpvDest As Any, ByVal hpvSource 
    As Long, ByVal cbCopy As Long)   
  65. Private Declare Function GetProcessHeap 
    Lib "kernel32"() As Long   
  66. Private Declare Function HeapAlloc
     Lib "kernel32" _   
  67. (ByVal hHeap As Long, ByVal 
    dwFlags As Long, _   
  68. ByVal dwBytes As Long) As Long   
  69. Private Declare Function HeapFree Lib 
    "kernel32" (ByVal hHeap As Long, _   
  70. ByVal dwFlags As Long, lpMem As Any) As Long  

 

把下面的VB.NET获取网卡地址的代码放入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. myNcb.ncb_buffer = pASTAT   
  20. bRet = Netbios(myNcb)   
  21. Debug.Print Err.LastDllError   
  22. CopyMemory myASTAT, myNcb.
    ncb_buffer, Len(myASTAT)   
  23. MsgBox Hex(myASTAT.adapt
    .adapter_address(0)) & " " & _   
  24. Hex(myASTAT.adapt.adapter_
    address(1)) _   
  25. & " " & Hex(myASTAT.adapt
    .adapter_address(2)) & " " _   
  26. & Hex(myASTAT.adapt.adapter_address(3)) _   
  27. & " " & Hex(myASTAT.adapt.
    adapter_address(4)) & " " _   
  28. & Hex(myASTAT.adapt.adapter_address(5))   
  29. HeapFree GetProcessHeap(), 
    0, pASTAT   
  30. End Sub  

 

4)按F5,运行该程序。

5)点击Command1。注意,网卡地址将在一信息框中显示出来。至此,VB.NET获取网卡地址的操作就结束了。

相关内容

热门资讯

如何允许远程连接到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 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...