温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VB.NET中如何获取网卡地址

发布时间:2021-07-28 17:54:32 来源:亿速云 阅读:345 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关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获取网卡地址的操作就结束了。

看完上述内容,你们对VB.NET中如何获取网卡地址有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI