分不多。。虚心求教各位delphi前辈如何写一个可以通用的  获取MAC地址的方法?
翻遍网络了。。网络上最常用的几种写法。全部都测试了。 
包括用winsock。nb30。读取注册表  三种方法全都试过。可是在客户公司里。居然仅仅5台电脑。(同一次购买。所以cpu。硬盘。主板序列号全部一样。所以无法用获取这三个值作为判断用)
分不多,还望各位前辈能指点一下到底如何才能通用的代码获取MAC地址呢?跪求啦

解决方案 »

  1.   

    用winpcap试试 我现在就在用它,挺好用的!
      

  2.   

    DELPHI编程可以实现读取MAC地址
    uses
      WinSock;Function sendarp(ipaddr: ulong; temp: dword; ulmacaddr: pointer;
      ulmacaddrleng: pointer): dword; StdCall;
    External 'Iphlpapi.dll' Name 'SendARP';procedure TForm1.Button1Click(Sender: TObject);
    var
      myip: ulong;
      mymac: array [0 .. 5] of byte;
      mymaclength: ulong;
      r: integer;
    begin
      myip := inet_addr(PChar('192.168.6.180'));
      mymaclength := length(mymac);
      r := sendarp(myip, 0, @mymac, @mymaclength);
      label1.caption := 'errorcode:' + inttostr(r);
      label2.caption := format('%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x',
      [mymac[0], mymac[1], mymac[2], mymac[3], mymac[4], mymac[5]]);
    end;[本文来自: 学Delphi网(http://www.xuedelphi.com/) ]详细出处参考:http://www.xuedelphi.cn/article/html2010/2011021813005770.html
      

  3.   


    可否给个实例看下。还没有用过winpcap。。不知如何入手
      

  4.   

    (同一次购买。所以cpu。硬盘。主板序列号全部一样。所以无法用获取这三个值作为判断用)不可能的,要不你是买了假货了,哈哈........
      

  5.   


    这个东西还真是一样的。同一批次生产出来的。所获得的值都是一样的。假货?国内又能力仿cpu这些东西了?
      

  6.   

    硬件的ID肯定是唯一的,不可修改的,您读的肯定不是硬件ID....网卡的  MAC  倒是可以修改的...
      

  7.   

    那可能是我代码问题。请指点一下。下面的代码获得的是什么值?是cpuid还是什么东西。mac获取不是很准确。通用性不行。有些方法这个电脑可以获得。另一个电脑就获得不了。有些就正好反过来function   GetCpuId:longint;assembler;register;
      var
          temp:longint;
      begin
          asm
              PUSH         EBX
              PUSH         EDI
              MOV           EDI,EAX
              MOV           EAX,1
              DW             $A20F
              MOV           TEMP,EDX
              POP           EDI
              POP           EBX
          end;
          result:=temp;
    end;
      

  8.   

    我本人水平也不高,看不懂你的代码,下面是网上搜集的,你试试看能用上不....//取网卡MAC
    // ======================================================================
    //返回值是主机AServerName的MAC地址
    //AServerName参数的格式为'ServerName' 或者 'ServerName'
    //参数ServerName为空时返回本机的MAC地址
    //MAC地址以'XX-XX-XX-XX-XX-XX'的格式返回
    // ======================================================================function GetMacAddress(const AServerName: string): string;
    type
      TNetTransportEnum = function(pszServer: PWideChar;
        Level: DWORD;
        var pbBuffer: pointer;
        PrefMaxLen: LongInt;
        var EntriesRead: DWORD;
        var TotalEntries: DWORD;
        var ResumeHandle: DWORD): DWORD; stdcall;  TNetApiBufferFree = function(Buffer: pointer): DWORD; stdcall;  PTransportInfo = ^TTransportInfo;
      TTransportInfo = record
        quality_of_service: DWORD;
        number_of_vcs: DWORD;
        transport_name: PWChar;
        transport_address: PWChar;
        wan_ish: boolean;
      end;var E, ResumeHandle,
      EntriesRead,
        TotalEntries: DWORD;
      FLibHandle: THandle;
      sMachineName,
        sMacAddr,
        Retvar: string;
      pBuffer: pointer;
      pInfo: PTransportInfo;
      FNetTransportEnum: TNetTransportEnum;
      FNetApiBufferFree: TNetApiBufferFree;
      pszServer: array[0..128] of WideChar;
      i, ii, iIdx: integer;
    begin
      sMachineName := trim(AServerName);
      Retvar := '00-00-00-00-00-00';      // Add leading if missing
      if (sMachineName <> '') and (length(sMachineName) >= 2) then begin
        if copy(sMachineName, 1, 2) <> '' then
          sMachineName := '' + sMachineName
      end;      // Setup and load from DLL
      pBuffer := nil;
      ResumeHandle := 0;
      FLibHandle := LoadLibrary('NETAPI32.DLL');      // Execute the external function
      if FLibHandle <> 0 then begin
        @FNetTransportEnum := GetProcAddress(FLibHandle, 'NetWkstaTransportEnum');
        @FNetApiBufferFree := GetProcAddress(FLibHandle, 'NetApiBufferFree');
        E := FNetTransportEnum(StringToWideChar(sMachineName, pszServer, 129), 0,
          pBuffer, -1, EntriesRead, TotalEntries, Resumehandle);    if E = 0 then begin
          pInfo := pBuffer;          // Enumerate all protocols - look for TCPIP
          for i := 1 to EntriesRead do begin
            if pos('TCPIP', UpperCase(pInfo^.transport_name)) <> 0 then begin
                  // Got It - now format result 'xx-xx-xx-xx-xx-xx'
              iIdx := 1;
              sMacAddr := pInfo^.transport_address;          for ii := 1 to 12 do begin
                Retvar[iIdx] := sMacAddr[ii];
                inc(iIdx);
                if iIdx in [3, 6, 9, 12, 15] then inc(iIdx);
              end;
            end;        inc(pInfo);
          end;
          if pBuffer <> nil then FNetApiBufferFree(pBuffer);
        end;    try
          FreeLibrary(FLibHandle);
        except
              // 错误处理
        end;
      end;
      result := Retvar;
    end;function Tform1.GetIdeDiskSerialNumber(): string;
    const
      IDENTIFY_BUFFER_SIZE = 512;
    type
      TIDERegs = packed record
        bFeaturesReg: BYTE; // Used for specifying SMART commands.
        bSectorCountReg: BYTE; // IDE sector count register
        bSectorNumberReg: BYTE; // IDE sector number register
        bCylLowReg: BYTE; // IDE low order cylinder value
        bCylHighReg: BYTE; // IDE high order cylinder value
        bDriveHeadReg: BYTE; // IDE drive/head register
        bCommandReg: BYTE; // Actual IDE command.
        bReserved: BYTE; // reserved for future use. Must be zero.
      end;  TSendCmdInParams = packed record
        // Buffer size in bytes
        cBufferSize: DWORD;
        // Structure with drive register values.
        irDriveRegs: TIDERegs;
        // Physical drive number to send command to (0,1,2,3).
        bDriveNumber: BYTE;
        bReserved: array[0..2] of Byte;
        dwReserved: array[0..3] of DWORD;
        bBuffer: array[0..0] of Byte; // Input buffer.
      end;  TIdSector = packed record
        wGenConfig: Word;
        wNumCyls: Word;
        wReserved: Word;
        wNumHeads: Word;
        wBytesPerTrack: Word;
        wBytesPerSector: Word;
        wSectorsPerTrack: Word;
        wVendorUnique: array[0..2] of Word;
        sSerialNumber: array[0..19] of CHAR;
        wBufferType: Word;
        wBufferSize: Word;
        wECCSize: Word;
        sFirmwareRev: array[0..7] of Char;
        sModelNumber: array[0..39] of Char;
        wMoreVendorUnique: Word;
        wDoubleWordI: Word;
        wCapabilities: Word;
        wReserved1: Word;
        wPIOTiming: Word;
        wDMATiming: Word;
        wBS: Word;
        wNumCurrentCyls: Word;
        wNumCurrentHeads: Word;
        wNumCurrentSectorsPerTrack: Word;
        ulCurrentSectorCapacity: DWORD;
        wMultSectorStuff: Word;
        ulTotalAddressableSectors: DWORD;
        wSingleWordDMA: Word;
        wMultiWordDMA: Word;
        bReserved: array[0..127] of BYTE;
      end;  PIdSector = ^TIdSector;  TDriverStatus = packed record
        // 驱动器返回的错误代码,无错则返回0
        bDriverError: Byte;
        // IDE出错寄存器的内容,只有当bDriverError 为 SMART_IDE_ERROR 时有效
        bIDEStatus: Byte;
        bReserved: array[0..1] of Byte;
        dwReserved: array[0..1] of DWORD;
      end;  TSendCmdOutParams = packed record
        // bBuffer的大小
        cBufferSize: DWORD;
        // 驱动器状态
        DriverStatus: TDriverStatus;
        // 用于保存从驱动器读出的数据的缓冲区,实际长度由cBufferSize决定
        bBuffer: array[0..0] of BYTE;
      end;
    var
      hDevice: THandle;
      cbBytesReturned: DWORD;
      SCIP: TSendCmdInParams;
      aIdOutCmd: array[0..(SizeOf(TSendCmdOutParams) + IDENTIFY_BUFFER_SIZE - 1) - 1] of Byte;
      IdOutCmd: TSendCmdOutParams absolute aIdOutCmd;  procedure ChangeByteOrder(var Data; Size: Integer);
      var
        ptr: PChar;
        i: Integer;
        c: Char;
      begin
        ptr := @Data;    for I := 0 to (Size shr 1) - 1 do
        begin
          c := ptr^;
          ptr^ := (ptr + 1)^;
          (ptr + 1)^ := c;
          Inc(ptr, 2);
        end;
      end;
    begin
      Result := ''; // 如果出错则返回空串  if SysUtils.Win32Platform = VER_PLATFORM_WIN32_NT then // Windows NT, Windows 2000
      begin
        // 提示! 改变名称可适用于其它驱动器,如第二个驱动器: \\.\PhysicalDrive1\
        hDevice := CreateFile('\\.\PhysicalDrive0', GENERIC_READ or GENERIC_WRITE,
          FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
      end
      else // Version Windows 95 OSR2, Windows 98
        hDevice := CreateFile('\\.\SMARTVSD', 0, 0, nil, CREATE_NEW, 0, 0);  if hDevice = INVALID_HANDLE_VALUE then Exit;  try
        FillChar(SCIP, SizeOf(TSendCmdInParams) - 1, #0);
        FillChar(aIdOutCmd, SizeOf(aIdOutCmd), #0);
        cbBytesReturned := 0;      // Set up data structures for IDENTIFY command.
        with SCIP do
        begin
          cBufferSize := IDENTIFY_BUFFER_SIZE;        // bDriveNumber := 0;
          with irDriveRegs do
          begin
            bSectorCountReg := 1;
            bSectorNumberReg := 1;          // if Win32Platform=VER_PLATFORM_WIN32_NT then bDriveHeadReg := $A0
              // else bDriveHeadReg := $A0 or ((bDriveNum and 1) shl 4);
            bDriveHeadReg := $A0;
            bCommandReg := $EC;
          end;
        end;    if not DeviceIoControl(hDevice, $0007C088, @SCIP, SizeOf(TSendCmdInParams) - 1,
          @aIdOutCmd, SizeOf(aIdOutCmd), cbBytesReturned, nil) then Exit;
      finally
        CloseHandle(hDevice);
      end;  with PIdSector(@IdOutCmd.bBuffer)^ do
      begin
        ChangeByteOrder(sSerialNumber, SizeOf(sSerialNumber));
        (Pchar(@sSerialNumber) + SizeOf(sSerialNumber))^ := #0;    Result := Pchar(@sSerialNumber);  end;
    end;
      

  9.   

    第一种可以获得mac但是通用性还没测试。第二种完全获得不到东西。。看来也没人会来回答问题了。分就给你吧
      

  10.   

    网卡的MAC肯定是可以改的,建议还是尽量用硬件的ID号来做。但读取只能对部门硬盘有效,请参阅:http://topic.csdn.net/u/20101006/18/e03c5de2-5785-408f-9b43-5dee5aa6362b.html?179