wang_qiang 发表于 2008-10-31 19:43:00

在XP下用VB6怎样得到网卡的Mac地址

<p>在XP下用VB6怎样得到网卡的Mac地址?现有一段代码,在Win98下好用,但在XP下不好用,不知是什么原因。希望高手指教:</p><p>Option Explicit <br/>Private Const NCBASTAT = &amp;H33 <br/>Private Const NCBNAMSZ = 16 <br/>Private Const HEAP_ZERO_MEMORY = &amp;H8 <br/>Private Const HEAP_GENERATE_EXCEPTIONS = &amp;H4 <br/>Private Const NCBRESET = &amp;H32 <br/>Private Type NCB <br/>  ncb_command As Byte 'Integer <br/>  ncb_retcode As Byte 'Integer <br/>  ncb_lsn As Byte 'Integer <br/>  ncb_num As Byte ' Integer <br/>  ncb_buffer As Long 'String <br/>  ncb_length As Integer <br/>  ncb_callname As String * NCBNAMSZ <br/>  ncb_name As String * NCBNAMSZ <br/>  ncb_rto As Byte 'Integer <br/>  ncb_sto As Byte ' Integer <br/>  ncb_post As Long <br/>  ncb_lana_num As Byte 'Integer <br/>  ncb_cmd_cplt As Byte 'Integer <br/>  ncb_reserve(9) As Byte ' Reserved, must be 0 <br/>  ncb_event As Long <br/>End Type </p><p>Private Type ADAPTER_STATUS <br/>  adapter_address(5) As Byte 'As String * 6 <br/>  rev_major As Byte 'Integer <br/>  reserved0 As Byte 'Integer <br/>  adapter_type As Byte 'Integer <br/>  rev_minor As Byte 'Integer <br/>  duration As Integer <br/>  frmr_recv As Integer <br/>  frmr_xmit As Integer <br/>  iframe_recv_err As Integer <br/>  xmit_aborts As Integer <br/>  xmit_success As Long <br/>  recv_success As Long <br/>  iframe_xmit_err As Integer <br/>  recv_buff_unavail As Integer <br/>  t1_timeouts As Integer <br/>  ti_timeouts As Integer <br/>  Reserved1 As Long <br/>  free_ncbs As Integer <br/>  max_cfg_ncbs As Integer <br/>  max_ncbs As Integer <br/>  xmit_buf_unavail As Integer <br/>  max_dgram_size As Integer <br/>  pending_sess As Integer <br/>  max_cfg_sess As Integer <br/>  max_sess As Integer <br/>  max_sess_pkt_size As Integer <br/>  name_count As Integer <br/>End Type </p><p>Private Type NAME_BUFFER <br/>  name As String * NCBNAMSZ <br/>  name_num As Integer <br/>  name_flags As Integer <br/>End Type </p><p>Private Type ASTAT <br/>  adapt As ADAPTER_STATUS <br/>  NameBuff(30) As NAME_BUFFER <br/>End Type </p><p>Private Declare Function Netbios Lib "netapi32.dll" (pncb As NCB) As Byte <br/>Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ <br/>  (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long) <br/>Private Declare Function GetProcessHeap Lib "kernel32" () As Long <br/>Private Declare Function HeapAlloc Lib "kernel32" _ <br/>  (ByVal hHeap As Long, ByVal dwFlags As Long, _ <br/>  ByVal dwBytes As Long) As Long <br/>Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, _ <br/>  ByVal dwFlags As Long, lpMem As Any) As Long </p><p>把下面的代码放入Command1_Click的事件中: </p><p>Private Sub Command1_Click() <br/>  Dim myNcb As NCB <br/>  Dim bRet As Byte <br/>  myNcb.ncb_command = NCBRESET <br/>  bRet = Netbios(myNcb) <br/>  myNcb.ncb_command = NCBASTAT <br/>  myNcb.ncb_lana_num = 0 <br/>  myNcb.ncb_callname = "*       " <br/>  Dim myASTAT As ASTAT, tempASTAT As ASTAT <br/>  Dim pASTAT As Long <br/>  myNcb.ncb_length = Len(myASTAT) <br/>  Debug.Print Err.LastDllError <br/>  pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS _ <br/>    Or HEAP_ZERO_MEMORY, myNcb.ncb_length) <br/>  If pASTAT = 0 Then <br/>    Debug.Print "memory allcoation failed!" <br/>    Exit Sub <br/>  End If <br/>  myNcb.ncb_buffer = pASTAT <br/>  bRet = Netbios(myNcb) <br/>  Debug.Print Err.LastDllError <br/>  CopyMemory myASTAT, myNcb.ncb_buffer, Len(myASTAT) <br/>  MsgBox Hex(myASTAT.adapt.adapter_address(0)) &amp; " " &amp; _ <br/>    Hex(myASTAT.adapt.adapter_address(1)) _ <br/>    &amp; " " &amp; Hex(myASTAT.adapt.adapter_address(2)) &amp; " " _ <br/>    &amp; Hex(myASTAT.adapt.adapter_address(3)) _ <br/>    &amp; " " &amp; Hex(myASTAT.adapt.adapter_address(4)) &amp; " " _ <br/>    &amp; Hex(myASTAT.adapt.adapter_address(5)) <br/>  HeapFree GetProcessHeap(), 0, pASTAT <br/>End Sub </p><p></p>

wang_qiang 发表于 2008-11-13 20:57:00

<p>自己顶一下。</p><p>领导让我将作好的程序加密后再使用,以控制软件的使用范围。我查了一下资料,觉得用计算机硬件的一些特征码比较好一点,比如MAC地址或CPU序列号,但不知在XP下用VB6怎样实现,望在这方面有研究的前辈指教。</p>

sym123321 发表于 2008-11-16 20:54:00

<p>方法有好几种,不管哪种方法都要注意多网卡的情况。</p><p>1.用VB调用系统的ipconfig.exe /all &gt; c:\ip.txt,得到的信息都在ip.txt文件中了,提取所有Physical Address 字段的最后xx-xx-xx-xx-xx-xx就可以了.</p><p>2.代码如下</p><p>Option Explicit<br/>Dim objSWbemServices As SWbemServices<br/>Dim objSWbemObjectSet As SWbemObjectSet<br/>Dim objSWbemObject As SWbemObject<br/>Private Type NetCard<br/>&nbsp;&nbsp;&nbsp; Name As String<br/>&nbsp;&nbsp;&nbsp; IPAdress As String<br/>&nbsp;&nbsp;&nbsp; IpSubNets As String<br/>&nbsp;&nbsp;&nbsp; IpGateWay As String<br/>&nbsp;&nbsp;&nbsp; DnsString0 As String<br/>&nbsp;&nbsp;&nbsp; DnsString1 As String<br/>&nbsp;&nbsp;&nbsp; MacAdress As String<br/>End Type<br/>Dim MtNetCard() As NetCard<br/>Private Sub Command1_Click()<br/>&nbsp;&nbsp;&nbsp; Dim i As Long<br/>&nbsp;&nbsp;&nbsp; For i = LBound(MtNetCard) To UBound(MtNetCard) - 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text1 = Text1 &amp; "网卡:&nbsp;&nbsp;&nbsp; " &amp; MtNetCard(i).Name &amp; vbNewLine<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Text1 = Text1 &amp; "MAC:&nbsp;&nbsp;&nbsp;&nbsp; " &amp; MtNetCard(i).MacAdress &amp; vbNewLine<br/>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; Erase MtNetCard<br/>End Sub</p><p>Private Sub Form_Load()<br/>&nbsp;&nbsp;&nbsp; ReDim MtNetCard(0) As NetCard<br/>&nbsp;&nbsp;&nbsp; Set objSWbemServices = GetObject("winmgmts:")<br/>&nbsp;&nbsp;&nbsp; Set objSWbemObjectSet = objSWbemServices.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")<br/>&nbsp;&nbsp;&nbsp; For Each objSWbemObject In objSWbemObjectSet<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; On Error Resume Next<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MtNetCard(UBound(MtNetCard)).Name = objSWbemObject.Description&nbsp;&nbsp; '添加本机上已经安装了TCP/IP协议的网卡<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MtNetCard(UBound(MtNetCard)).MacAdress = objSWbemObject.MacAddress(0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReDim Preserve MtNetCard(UBound(MtNetCard) + 1) As NetCard<br/>&nbsp;&nbsp;&nbsp; Next<br/>End Sub</p><p>应该看得懂吧,要一个窗口,一个文本框和一个按钮就可调试了。</p>

xxxtttxxx 发表于 2008-11-16 22:56:00

直接搞硬盘序列号,省事省力,API就可以,方便多了

wang_qiang 发表于 2008-11-17 20:27:00

多谢各位的大力相助
页: [1]
查看完整版本: 在XP下用VB6怎样得到网卡的Mac地址