我在网上找了好多获取MAC的代码,为什么在我的机子上运行,得到的MAC全都是0,我用DOS命令getmac看的是正常的,为什么代码获得的都是0?
是哪里的问题?

解决方案 »

  1.   

    老马今天说他搞了个这东西,不知对你有没帮助.
    http://www.m5home.com/bbs/thread-4352-1-1.html翻译了一个代码,使用GetAdaptersAddresses取得本机所有网卡的MAC地址.有需要的可以下载玩玩.
      

  2.   


    Public Const MAX_HOSTNAME_LEN = 132
    Public Const MAX_DOMAIN_NAME_LEN = 132
    Public Const MAX_SCOPE_ID_LEN = 260
    Public Const MAX_ADAPTER_NAME_LENGTH = 260
    Public Const MAX_ADAPTER_ADDRESS_LENGTH = 8
    Public Const MAX_ADAPTER_DESCRIPTION_LENGTH = 132
    Public Const ERROR_BUFFER_OVERFLOW = 111
    Public Const MIB_IF_TYPE_ETHERNET = 6
    Public Const MIB_IF_TYPE_TOKENRING = 9
    Public Const MIB_IF_TYPE_FDDI = 15
    Public Const MIB_IF_TYPE_PPP = 23
    Public Const MIB_IF_TYPE_LOOPBACK = 24
    Public Const MIB_IF_TYPE_SLIP = 28Type IP_ADDR_STRING
                Next As Long
                IpAddress As String * 16
                IpMask As String * 16
                Context As Long
    End TypeType IP_ADAPTER_INFO
                Next As Long
                ComboIndex As Long
                AdapterName As String * MAX_ADAPTER_NAME_LENGTH
                Description As String * MAX_ADAPTER_DESCRIPTION_LENGTH
                AddressLength As Long
                Address(MAX_ADAPTER_ADDRESS_LENGTH - 1) As Byte
                Index As Long
                Type As Long
                DhcpEnabled As Long
                CurrentIpAddress As Long
                IpAddressList As IP_ADDR_STRING
                GatewayList As IP_ADDR_STRING
                DhcpServer As IP_ADDR_STRING
                HaveWins As Byte
                PrimaryWinsServer As IP_ADDR_STRING
                SecondaryWinsServer As IP_ADDR_STRING
                LeaseObtained As Long
                LeaseExpires As Long
    End TypeType FIXED_INFO
                HostName As String * MAX_HOSTNAME_LEN
                DomainName As String * MAX_DOMAIN_NAME_LEN
                CurrentDnsServer As Long
                DnsServerList As IP_ADDR_STRING
                NodeType As Long
                ScopeId  As String * MAX_SCOPE_ID_LEN
                EnableRouting As Long
                EnableProxy As Long
                EnableDns As Long
    End TypePublic Declare Function GetNetworkParams Lib "IPHlpApi.dll" _
           (FixedInfo As Any, pOutBufLen As Long) As Long
    Public Declare Function GetAdaptersInfo Lib "IPHlpApi.dll" _
           (IpAdapterInfo As Any, pOutBufLen As Long) As Long
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
           (Destination As Any, Source As Any, ByVal Length As Long)Sub main()
        Dim error As Long
        Dim FixedInfoSize As Long
        Dim AdapterInfoSize As Long
        Dim i As Integer
        Dim PhysicalAddress  As String
        Dim NewTime As Date
        Dim AdapterInfo As IP_ADAPTER_INFO
        Dim AddrStr As IP_ADDR_STRING
        Dim FixedInfo As FIXED_INFO
        Dim Buffer As IP_ADDR_STRING
        Dim pAddrStr As Long
        Dim pAdapt As Long
        Dim Buffer2 As IP_ADAPTER_INFO
        Dim FixedInfoBuffer() As Byte
        Dim AdapterInfoBuffer() As Byte    ' Get the main IP configuration information for this machine
        ' using a FIXED_INFO structure.
        FixedInfoSize = 0
        error = GetNetworkParams(ByVal 0&, FixedInfoSize)
        If error <> 0 Then
            If error <> ERROR_BUFFER_OVERFLOW Then
               MsgBox "GetNetworkParams sizing failed with error " & error
               Exit Sub
            End If
        End If
        ReDim FixedInfoBuffer(FixedInfoSize - 1)    error = GetNetworkParams(FixedInfoBuffer(0), FixedInfoSize)
        If error = 0 Then
                CopyMemory FixedInfo, FixedInfoBuffer(0), FixedInfoSize
                MsgBox "主机名称:  " & FixedInfo.HostName
                MsgBox "DNS服务器:  " & FixedInfo.DnsServerList.IpAddress
                pAddrStr = FixedInfo.DnsServerList.Next
                Do While pAddrStr <> 0
                      CopyMemory Buffer, ByVal pAddrStr, LenB(Buffer)
                      MsgBox "DNS Servers:  " & Buffer.IpAddress
                      pAddrStr = Buffer.Next
                Loop            Select Case FixedInfo.NodeType
                           Case 1
                                      MsgBox "网点类型:广播(Broadcast)"
                           Case 2
                                      MsgBox "网点类型:点对点(Peer to peer)"
                           Case 4
                                      MsgBox "网点类型:混合(Mixed)"
                           Case 8
                                      MsgBox "网点类型:混合的(Hybrid)"
                           Case Else
                                      MsgBox "未知的网点类型"
                End Select            MsgBox "NetBIOS 范围 ID:  " & FixedInfo.ScopeId
                If FixedInfo.EnableRouting Then
                           MsgBox "IP 路由激活的 "
                Else
                           MsgBox "IP 路由没有激活的"
                End If
                If FixedInfo.EnableProxy Then
                           MsgBox "WINS 代理服务器激活的"
                Else
                           MsgBox "WINS 代理服务器没有激活的"
                End If
                If FixedInfo.EnableDns Then
                          MsgBox "NetBIOS Resolution Uses DNS "
                Else
                          MsgBox "NetBIOS Resolution Does not use DNS  "
                End If
        Else
                MsgBox "GetNetworkParams failed with error " & error
                Exit Sub
        End If    ' Enumerate all of the adapter specific information using the
        ' IP_ADAPTER_INFO structure.
        ' Note:  IP_ADAPTER_INFO contains a linked list of adapter entries.    AdapterInfoSize = 0
        error = GetAdaptersInfo(ByVal 0&, AdapterInfoSize)
        If error <> 0 Then
            If error <> ERROR_BUFFER_OVERFLOW Then
               MsgBox "GetAdaptersInfo sizing failed with error " & error
               Exit Sub
            End If
        End If
        ReDim AdapterInfoBuffer(AdapterInfoSize - 1)    ' Get actual adapter information
        error = GetAdaptersInfo(AdapterInfoBuffer(0), AdapterInfoSize)
        If error <> 0 Then
           MsgBox "获取适配器信息失败,错误: " & error
           Exit Sub
        End If    ' Allocate memory
         CopyMemory AdapterInfo, AdapterInfoBuffer(0), AdapterInfoSize
        pAdapt = AdapterInfo.Next    Do
         CopyMemory Buffer2, AdapterInfo, AdapterInfoSize
           Select Case Buffer2.Type
                  Case MIB_IF_TYPE_ETHERNET
                       MsgBox "适配器名称: Ethernet adapter "
                  Case MIB_IF_TYPE_TOKENRING
                       MsgBox "适配器名称: Token Ring adapter "
                  Case MIB_IF_TYPE_FDDI
                       MsgBox "适配器名称: FDDI adapter "
                  Case MIB_IF_TYPE_PPP
                       MsgBox "适配器名称: PPP adapter"
                  Case MIB_IF_TYPE_LOOPBACK
                       MsgBox "适配器名称: Loopback adapter "
                  Case MIB_IF_TYPE_SLIP
                       MsgBox "适配器名称: Slip adapter "
                  Case Else
                       MsgBox "适配器名称: Other adapter "
           End Select
           MsgBox "适配器描述: " & Buffer2.Description       PhysicalAddress = ""
           For i = 0 To Buffer2.AddressLength - 1
               PhysicalAddress = PhysicalAddress & Hex(Buffer2.Address(i))
               If i < Buffer2.AddressLength - 1 Then
                  PhysicalAddress = PhysicalAddress & "-"
               End If
           Next
           MsgBox "物理地址(MAC地址): " & PhysicalAddress       If Buffer2.DhcpEnabled Then
              MsgBox "DHCP Enabled "
           Else
              MsgBox "DHCP disabled"
           End If       MsgBox "IP 地址: " & Buffer2.IpAddressList.IpAddress
           MsgBox "子网掩码: " & Buffer2.IpAddressList.IpMask
           pAddrStr = Buffer2.IpAddressList.Next
           Do While pAddrStr <> 0
              CopyMemory Buffer, Buffer2.IpAddressList, LenB(Buffer)
              MsgBox "IP 地址: " & Buffer.IpAddress
              MsgBox "子网掩码: " & Buffer.IpMask
              pAddrStr = Buffer.Next
              If pAddrStr <> 0 Then
                 CopyMemory Buffer2.IpAddressList, ByVal pAddrStr, _
                            LenB(Buffer2.IpAddressList)
              End If
           Loop       MsgBox "默认网关: " & Buffer2.GatewayList.IpAddress
           pAddrStr = Buffer2.GatewayList.Next
           Do While pAddrStr <> 0
              CopyMemory Buffer, Buffer2.GatewayList, LenB(Buffer)
              MsgBox "IP Address: " & Buffer.IpAddress
              pAddrStr = Buffer.Next
              If pAddrStr <> 0 Then
                 CopyMemory Buffer2.GatewayList, ByVal pAddrStr, _
                            LenB(Buffer2.GatewayList)
              End If
           Loop       MsgBox "DHCP Server: " & Buffer2.DhcpServer.IpAddress
           MsgBox "Primary WINS Server: " & _
                  Buffer2.PrimaryWinsServer.IpAddress
           MsgBox "Secondary WINS Server: " & _
                  Buffer2.SecondaryWinsServer.IpAddress       ' Display time.
           NewTime = DateAdd("s", Buffer2.LeaseObtained, #1/1/1970#)
           MsgBox "Lease Obtained: " & _
                  CStr(Format(NewTime, "dddd, mmm d hh:mm:ss yyyy"))       NewTime = DateAdd("s", Buffer2.LeaseExpires, #1/1/1970#)
           MsgBox "Lease Expires :  " & _
                  CStr(Format(NewTime, "dddd, mmm d hh:mm:ss yyyy"))
           pAdapt = Buffer2.Next
           If pAdapt <> 0 Then
               CopyMemory AdapterInfo, ByVal pAdapt, AdapterInfoSize
            End If
          Loop Until pAdapt = 0
    End Sub
      

  3.   

    WMI方法    Dim WMIObj As Object
        Dim NetObj As Object
        Dim NetObject As Object
        Set WMIObj = CreateObject("winmgmts://./root/cimv2")
        Set NetObj = WMIObj.InstancesOf("Win32_NetworkAdapter")
        For Each NetObject In NetObj
            If Len(NetObject.InterfaceIndex & "") > 0 Then
                List1.AddItem NetObject.Name & " [" & NetObject.MACAddress & "]"
            End If
        Next
      

  4.   

    应该可以从WMI的Win32_NetworkAdapter类中获取
      

  5.   

    二楼代码是曾经的一个 Microsoft 工程师的一段过程,很值得参考的
      

  6.   

    http://download.csdn.net/source/2119050