给你一个已通过的例子:
Option ExplicitConst NCBASTAT = &H33
Const NCBNAMSZ = 16
Const HEAP_ZERO_MEMORY = &H8
Const HEAP_GENERATE_EXCEPTIONS = &H4
Const NCBRESET = &H32
    
Private Type NCB
    ncb_command As Byte 'Integer
    ncb_retcode As Byte 'Integer
    ncb_lsn As Byte 'Integer
    ncb_num As Byte 'Integer
    ncb_buffer As Long  'String
    ncb_length As Integer
    ncb_callname As String * NCBNAMSZ
    ncb_name As String * NCBNAMSZ
    ncb_rto As Byte 'Integer
    ncb_sto As Byte 'Integer
    ncb_post As Long
    ncb_lana_num As Byte    'Integer
    ncb_cmd_cplt As Byte    'Integer
    ncb_reserve(10) As Byte ' Reserved, must be 0
    ncb_event As Long
End TypePrivate Type ADAPTER_STATUS
    adapter_address(5) As Byte  'String * 6
    rev_major As Byte   'Integer
    reserved0 As Byte   'Integer
    adapter_type As Byte    'Integer
    rev_minor As Byte   'Integer
    duration As Integer
    frmr_recv As Integer
    frmr_xmit As Integer
    iframe_recv_err As Integer
    xmit_aborts As Integer
    xmit_success As Long
    recv_success As Long
    iframe_xmit_err As Integer
    recv_buff_unavail As Integer
    t1_timeouts As Integer
    ti_timeouts As Integer
    Reserved1 As Long
    free_ncbs As Integer
    max_cfg_ncbs As Integer
    max_ncbs As Integer
    xmit_buf_unavail As Integer
    max_dgram_size As Integer
    pending_sess As Integer
    max_cfg_sess As Integer
    max_sess As Integer
    max_sess_pkt_size As Integer
    name_count As Integer
End TypePrivate Type NAME_BUFFER
    name  As String * NCBNAMSZ
    name_num As Integer
    name_flags As Integer
End TypePrivate Type ASTAT
    adapt As ADAPTER_STATUS
    NameBuff(30) As NAME_BUFFER
End TypePrivate Declare Function Netbios Lib "netapi32.dll" (pncb As NCB) As Byte
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As LongPublic Function GetMACAddress() As String
    Dim myNcb As NCB
    Dim bRet As Byte, i As Integer, str As String, MACAddress As String
    myNcb.ncb_callname = NCBRESET
    bRet = Netbios(myNcb)
    myNcb.ncb_command = NCBASTAT
    myNcb.ncb_lana_num = 0
    myNcb.ncb_callname = "*         "
    Dim myASTAT As ASTAT, tempASTAT As ASTAT
    Dim pASTAT As Long
    myNcb.ncb_length = Len(myASTAT)
    pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS _
        Or HEAP_ZERO_MEMORY, myNcb.ncb_length)
    If pASTAT = 0 Then
        Exit Function
    End If
    myNcb.ncb_buffer = pASTAT
    bRet = Netbios(myNcb)
    CopyMemory myASTAT, myNcb.ncb_buffer, Len(myASTAT)
    MACAddress = ""
    For i = 0 To 5
        str = CStr(Hex(myASTAT.adapt.adapter_address(i)))
        MACAddress = MACAddress & IIf(Len(str) = 1, "0" & str, str) & " "
    Next i
    HeapFree GetProcessHeap(), 0, pASTAT
    GetMACAddress = MACAddress
End FunctionPrivate Sub Form_Load()
    Form1.AutoRedraw = True
    Dim MACAddress As String
    MACAddress = GetMACAddress
    Print MACAddress
End Sub

解决方案 »

  1.   

    谢谢!我的例子也能通过,我要的是Long类型的MAC地址,是不是数据库设计有问题?老板要MAC地址作Primary Key,是Number类型的!
      

  2.   

    mac address is 6 bytes ,so u can use char(6)
      

  3.   

    干嘛不用byte数组呢,long只有4字节,而MAC有6字节,用LONG当然会溢出了。
      

  4.   

    唉,这个设计是不是很失败?数据库表的Primary Key用Number,那么,6个字节的MAC地址存到数据库里也会溢出吧?
    再说,如果客户机没有网卡呢?用什么好的办法可以唯一标志用户的PC呢?IP地址有可能是虚拟的局域网地址?
      

  5.   

    或者你可以用double类型,是8字节的,存进去就不会溢出了。
    我可以告诉你一种专用软件的方法。在安装时,用随机数和本身内置的KEY产生一ID(这个用户是不知道的),在安装时需要和服务器联系,服务器验证后将该ID存到数据库上,以后升级时就先发送ID(用户同样不知道),服务器检查数据库有该ID且该ID创建的时间不超过一年,则允许升级,没有,则不允许。